mirror of
https://github.com/whyour/qinglong.git
synced 2026-11-30 16:27:21 +08:00
Return committed trigger IDs and add CLI wait and trigger-specific logs while preserving API, CLI, transaction, deduplication and unknown-result semantics. Move scheduling, lifecycle, settlement, queries and legacy adapters into back/cron; remove obsolete internal import paths and circular dependencies. Apply project formatting across the repository, cover CJS/MJS in formatting hooks, add prettier:check and require formatting checks in AGENTS.md. Validation: backend build; 1398 backend/frontend tests passed (17 platform skips); 327 CLI tests passed (6 platform skips); 670 files pass format checks.
62 lines
2.1 KiB
TypeScript
62 lines
2.1 KiB
TypeScript
import { join } from 'path';
|
|
import { existsSync } from 'fs';
|
|
import { receiptPath } from '../settlement/exitReceipt.cjs';
|
|
import { spawn } from 'cross-spawn';
|
|
import type { ChildProcessWithoutNullStreams } from 'child_process';
|
|
import type { Readable } from 'stream';
|
|
|
|
// A managed command waits on its parent's pipe. EOF before authorization exits
|
|
// without interpreting the command, even when the worker is killed by SIGKILL.
|
|
export function spawnCronCommand(
|
|
command: string,
|
|
triggerId?: string,
|
|
onCommandStart?: (at: number) => void,
|
|
outputPath?: string,
|
|
) {
|
|
if (!triggerId) return spawn(command, { shell: '/bin/bash' });
|
|
const sourceScript = join(__dirname, '../../../shell/cron-command.py');
|
|
const supervisor = existsSync(sourceScript)
|
|
? sourceScript
|
|
: join(process.env.QL_DIR!, 'shell/cron-command.py');
|
|
const args = [
|
|
'-I',
|
|
'-S',
|
|
supervisor,
|
|
command,
|
|
String(process.pid),
|
|
receiptPath(triggerId) || '',
|
|
process.execPath,
|
|
join(__dirname, '../../shared/managedExit.cjs'),
|
|
];
|
|
if (onCommandStart) args.push('report-start');
|
|
if (outputPath) args.push('log-output', outputPath);
|
|
const child = spawn('python3', args, {
|
|
env: { ...process.env, QL_TRIGGER_ID: triggerId },
|
|
stdio: onCommandStart ? ['pipe', 'pipe', 'pipe', 'pipe'] : 'pipe',
|
|
}) as ChildProcessWithoutNullStreams;
|
|
if (onCommandStart && child.stdio[3]) {
|
|
const timing = child.stdio[3] as Readable;
|
|
let stamp = '';
|
|
timing.setEncoding('ascii').on('data', (chunk) => {
|
|
stamp += chunk;
|
|
});
|
|
timing.once('end', () => {
|
|
if (/^\d{13}$/.test(stamp)) onCommandStart(Number(stamp));
|
|
});
|
|
timing.on('error', () => {}); // Missing telemetry must not change the command result.
|
|
}
|
|
// A concurrent stop can close the pipe before the authorization write.
|
|
child.stdin.on('error', () => {});
|
|
return child;
|
|
}
|
|
|
|
export function releaseCronCommand(
|
|
child: ChildProcessWithoutNullStreams,
|
|
): Promise<void> {
|
|
return new Promise((resolve, reject) =>
|
|
child.stdin.end('run\n', (error?: Error) =>
|
|
error ? reject(error) : resolve(),
|
|
),
|
|
);
|
|
}
|