Files
qinglong/back/cron/scheduling/systemCron.ts
T
whyour c5458596c8 feat(cron): expose run receipts and modularize execution
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.
2026-11-22 12:24:29 +08:00

101 lines
3.3 KiB
TypeScript

import type { Crontab } from '../../data/cron';
import { CrontabModel } from '../../data/cron';
import type { Logger } from 'winston';
import config from '../../config/index';
import { execFileSync } from 'child_process';
import cronClient from '../../schedule/client';
import {
confirmSystemPlan,
stageSystemPlan,
suspendSystemPlan,
SystemRule,
} from './systemScheduleAudit';
import {
isNodeCron,
isSpecialSchedule,
makeCommand,
schedulerMode,
SchedulerMode,
} from './cronScheduling';
import { shellQuote } from '../../shared/shellQuote';
import { writeFileWithLock } from '../../shared/utils';
import { asError } from '../../shared/childProcess';
import { t } from '../../shared/i18n';
// Publication is called under the service's scheduler mutation lock. Staging,
// installation and confirmation keep their existing order and failure semantics.
export class SystemCronAdapter {
constructor(private logger: Logger) {}
async publish(
crons: Crontab[],
mode: () => SchedulerMode = schedulerMode,
command: (cron: Crontab) => string = makeCommand,
) {
const rules: SystemRule[] = [];
var crontab_string = '';
crons.forEach((tab) => {
if (
tab.isDisabled === 1 ||
isNodeCron(tab) ||
isSpecialSchedule(tab.schedule)
) {
crontab_string += '# ';
crontab_string += tab.schedule;
crontab_string += ' ';
crontab_string += command(tab);
crontab_string += '\n';
} else {
crontab_string += tab.schedule;
crontab_string += ' ';
rules.push({
id: tab.id!,
schedule: tab.schedule!,
name: tab.name || tab.command,
});
crontab_string += `QL_SYSTEM_DISPATCHED=$(date +\\%s) QL_SYSTEM_RULE=${shellQuote(
tab.schedule!,
)} ID=${
tab.id
} QL_TRIGGER_SOURCE=system bash "$QL_DIR/shell/system-cron.sh" ${shellQuote(
command(tab),
)}`;
crontab_string += '\n';
}
});
try {
const system = mode() === 'system';
const db = CrontabModel.sequelize!;
const plan = system ? await stageSystemPlan(db, rules) : undefined;
if (!system) await suspendSystemPlan(db);
await writeFileWithLock(config.crontabFile, crontab_string);
if (plan !== undefined) {
execFileSync('crontab', [config.crontabFile]);
await confirmSystemPlan(db, plan);
}
} catch (error) {
this.logger.error(
'[crontab] Failed to publish crontab:',
asError(error).message,
);
cronClient.readiness.invalidate();
if (process.env.SERVICE_TYPE === 'grpc')
process.send?.('crontab-install-failed');
// A fresh host may have no installed crontab at all. A previous boot's
// saved flag does not confirm any portable rule in the failed snapshot.
if (mode() === 'system') {
const ids = crons
.filter((row) => !isNodeCron(row) && !isSpecialSchedule(row.schedule))
.map((row) => row.id!);
if (ids.length)
await CrontabModel.update({ saved: false }, { where: { id: ids } });
}
throw Object.assign(
new Error(t('系统定时规则安装失败,旧规则可能仍在运行,正在重试')),
{ status: 503 },
);
}
}
}