This commit is contained in:
张成
2026-03-18 15:25:34 +08:00
parent 5b671d320b
commit 37e39d35b8
17 changed files with 368 additions and 167 deletions

View File

@@ -1,73 +1 @@
import Router from '@koa/router';
import { schedule_task } from '../models/index.js';
import { safe_json_parse, safe_json_stringify } from '../services/json_utils.js';
export const schedule_task_router = new Router();
schedule_task_router.post('/api/schedule_task/create', async (ctx) => {
const { name, cron_expression, action_name, payload } = ctx.request.body || {};
if (!name || !cron_expression || !action_name) {
ctx.status = 400;
ctx.body = { ok: false, error: '缺少 name/cron_expression/action_name' };
return;
}
const payload_json = payload ? safe_json_stringify(payload) : null;
const row = await schedule_task.create({
name,
cron_expression,
action_name,
payload_json,
enabled: true
});
ctx.body = { ok: true, data: { id: row.id } };
});
schedule_task_router.post('/api/schedule_task/list', async (ctx) => {
const rows = await schedule_task.findAll({ order: [['id', 'desc']] });
ctx.body = {
ok: true,
data: rows.map((r) => ({
id: r.id,
name: r.name,
cron_expression: r.cron_expression,
action_name: r.action_name,
payload: safe_json_parse(r.payload_json),
enabled: r.enabled,
last_run_at: r.last_run_at
}))
};
});
schedule_task_router.post('/api/schedule_task/set_enabled', async (ctx) => {
const { id, enabled } = ctx.request.body || {};
if (!id || typeof enabled !== 'boolean') {
ctx.status = 400;
ctx.body = { ok: false, error: '缺少 id/enabled(boolean)' };
return;
}
const row = await schedule_task.findByPk(id);
if (!row) {
ctx.status = 404;
ctx.body = { ok: false, error: '任务不存在' };
return;
}
row.enabled = enabled;
await row.save();
ctx.body = { ok: true };
});
schedule_task_router.post('/api/schedule_task/delete', async (ctx) => {
const { id } = ctx.request.body || {};
if (!id) {
ctx.status = 400;
ctx.body = { ok: false, error: '缺少 id' };
return;
}
await schedule_task.destroy({ where: { id } });
ctx.body = { ok: true };
});
// 已废弃:按需求改为写死定时任务(见 config/cron_tasks.js