Files
mv2_simple_crx/server/services/schedule_loader.js
张成 18aa083c91 1
2026-03-18 17:42:38 +08:00

51 lines
1.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 () => {
try {
await run_cron_task(task);
} catch (err) {
// action 内部已记录 crawl_run_recordflow 内部 action 也会记录
}
// });
// cron_jobs.push(job);
}
}
export function stop_all_cron_tasks() {
for (const job of cron_jobs) {
job.stop();
}
cron_jobs.length = 0;
}