This commit is contained in:
张成
2026-03-18 16:57:22 +08:00
parent 7b42ee8ef5
commit bc1068ec66
8 changed files with 275 additions and 128 deletions

View File

@@ -1,24 +1,44 @@
import cron from 'node-cron';
import { cron_task_list } from '../config/cron_tasks.js';
import { execute_action_and_record } from './task_executor.js';
import { get_flow_runner } from './flows/flow_registry.js';
const cron_jobs = [];
async function run_cron_task(task) {
if (!task || !task.type) {
throw new Error('cron_task 缺少 type');
}
if (task.type === 'action') {
await execute_action_and_record({
action_name: task.action_name,
action_payload: task.action_payload || {},
source: 'cron'
});
return;
}
if (task.type === 'flow') {
const run_flow = get_flow_runner(task.flow_name);
await run_flow(task.flow_payload || {});
return;
}
throw new Error(`cron_task type 不支持: ${task.type}`);
}
export async function start_all_cron_tasks() {
for (const task of cron_task_list) {
// const job = cron.schedule(task.cron_expression, async () => {
const job = cron.schedule(task.cron_expression, async () => {
try {
await execute_action_and_record({
action_name: task.action_name,
action_payload: task.action_payload || {},
source: 'cron'
});
await run_cron_task(task);
} catch (err) {
// 失败会在 crawl_run_record 落库
// action 内部已记录 crawl_run_recordflow 内部 action 也会记录
}
// });
});
// cron_jobs.push(job);
cron_jobs.push(job);
}
}