feat: harden cron execution recovery and expose scheduling diagnostics

Reconcile scheduled and system cron triggers, isolate concurrent executions and retain verifiable exit statistics across restarts. Add paged task history, log and script browsing, dashboard timing details and regression coverage.
This commit is contained in:
whyour committed 2026-11-17 01:01:57 +08:00
1 parent 3b6a873520
commit 8a9e70e90b
166 files changed
+18942 -1800

No files matched your search

+18 -10
View File
@@ -1,5 +1,3 @@
const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');
const { readFileSync } = require('fs');
const { join } = require('path');
@@ -9,7 +7,7 @@ class GrpcClient {
'config/grpc',
);
static #loadTlsCredentials() {
static #loadTlsCredentials(grpc) {
try {
return grpc.credentials.createSsl(
readFileSync(join(GrpcClient.#certDir, 'ca.crt')),
@@ -60,15 +58,22 @@ class GrpcClient {
];
#client;
#Metadata;
#closed = false;
#api = {};
constructor() {
this.#initializeClient();
this.#bindMethods();
}
#initializeClient() {
// The panel dependencies resolve as they did before the task's global-path hook.
module.loadingPanelDependencies = true;
try {
// Resolve explicitly: Node can cache a previous task require by parent directory.
const grpc = require(require.resolve('@grpc/grpc-js'));
const protoLoader = require(require.resolve('@grpc/proto-loader'));
this.#Metadata = grpc.Metadata;
const { protoPath, protoOptions, serverAddress, grpcOptions } =
GrpcClient.#config;
@@ -77,28 +82,30 @@ class GrpcClient {
this.#client = new apiProto.Api(
serverAddress,
GrpcClient.#loadTlsCredentials(),
GrpcClient.#loadTlsCredentials(grpc),
grpcOptions,
);
} catch (error) {
console.error('Failed to initialize gRPC client:', error);
process.exit(1);
throw new Error('Failed to initialize gRPC client: ' + error.message, { cause: error });
} finally {
module.loadingPanelDependencies = false;
}
}
#promisifyMethod(methodName) {
const capitalizedMethod =
methodName.charAt(0).toUpperCase() + methodName.slice(1);
const method = this.#client[capitalizedMethod].bind(this.#client);
return async (params = {}) => {
if (this.#closed) throw new Error('Channel has been shut down');
if (!this.#client) this.#initializeClient();
return new Promise((resolve, reject) => {
const metadata = new grpc.Metadata();
const metadata = new this.#Metadata();
const deadline = new Date(
Date.now() + GrpcClient.#config.defaultTimeout,
);
method(params, metadata, { deadline }, (error, response) => {
this.#client[capitalizedMethod](params, metadata, { deadline }, (error, response) => {
if (error) {
return reject(error);
}
@@ -122,6 +129,7 @@ class GrpcClient {
}
close() {
this.#closed = true;
if (this.#client) {
this.#client.close();
this.#client = null;
+26
View File
@@ -0,0 +1,26 @@
// Observe the first executable location in the entry file. Preloads, the TS
// compiler and ESM dependency evaluation can all run before that location.
const { Session, url } = require('inspector');
const { pathToFileURL } = require('url');
const { writeFileSync } = require('fs');
module.exports = function observeScriptStart(entry, marker) {
if (!entry || !marker || url()) return;
const session = new Session();
try {
session.connect();
const target = pathToFileURL(entry).href;
session.post('Debugger.enable');
session.post('Debugger.setBreakpointByUrl', { url: target, lineNumber: 0 }, (err, bp) => {
if (err || !bp) { session.disconnect(); return; }
session.on('Debugger.paused', ({ params }) => {
if (params.hitBreakpoints?.includes(bp.breakpointId)) {
try { writeFileSync(marker, String(Date.now()), { flag: 'wx', mode: 0o600 }); } catch {}
session.disconnect();
} else {
session.post('Debugger.resume');
}
});
});
} catch { try { session.disconnect(); } catch {} }
};
+6
View File
@@ -15,9 +15,11 @@ function preferGlobalNodeModules() {
return;
}
const clientModule = require.cache[require.resolve('./client.js')];
const originalResolveFilename = Module._resolveFilename;
Module._resolveFilename = function (request, parent, isMain, options) {
if (
!clientModule.loadingPanelDependencies &&
!Module.builtinModules.includes(request) &&
!request.startsWith('node:') &&
!request.startsWith('.') &&
@@ -171,3 +173,7 @@ try {
} catch (error) {
console.log(`run builtin code error: `, error, '\n');
}
if (process.env.QL_SCRIPT_ENTRY && process.env.QL_SCRIPT_START_FILE) {
require('./script-start.cjs')(process.env.QL_SCRIPT_ENTRY, process.env.QL_SCRIPT_START_FILE);
}
+25
View File
@@ -141,3 +141,28 @@ try:
builtins.QLAPI = QLAPI
except Exception as error:
print(f"run builtin code error: {error}\n")
# A line event in the entry module occurs after interpreter startup, imports
# performed by this preload, and the configured before hook.
def observe_script_start(target, marker):
if sys.gettrace() is not None:
return
def first_line(frame, event, arg):
if event == 'line':
import time
try:
with open(marker, 'x') as receipt:
receipt.write(str(time.time_ns() // 1000000))
except OSError:
pass
sys.settrace(None)
return first_line
def enter(frame, event, arg):
if event == 'call' and frame.f_code.co_name == '<module>' and os.path.realpath(frame.f_code.co_filename) == target:
return first_line
return None
sys.settrace(enter)
if os.getenv('QL_SCRIPT_ENTRY') and os.getenv('QL_SCRIPT_START_FILE'):
observe_script_start(os.environ['QL_SCRIPT_ENTRY'], os.environ['QL_SCRIPT_START_FILE'])