-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathcron-tasker.js
More file actions
338 lines (295 loc) · 12.1 KB
/
cron-tasker.js
File metadata and controls
338 lines (295 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/**
* 定时任务管理器模块
* 负责管理和执行定时任务脚本,支持cron表达式调度
* @module cron-tasker
*/
// cron-tasker.js(已修复)
import path from 'path';
import {readdir, stat} from 'fs/promises';
import {pathToFileURL} from 'url';
import {CronJob} from 'cron';
import {validateBasicAuth} from "../utils/api_validate.js"; // 官方 cron
import {toBeijingTime} from "../utils/datetime-format.js"
// 排除的脚本文件列表
const scripts_exclude = ['moontv.mjs', 'kzz.mjs'];
// 是否启用定时任务功能
const enable_tasker = Number(process.env.ENABLE_TASKER) || 0;
/**
* 定时任务管理器插件
* @param {Object} fastify - Fastify实例
* @param {Object} options - 插件选项
* @param {Function} done - 完成回调
*/
export default (fastify, options, done) => {
// 配置对象
const config = {
scriptsDir: path.join(options.rootDir, 'scripts/cron'),
};
// 任务注册表
const taskRegistry = new Map();
/**
* 格式化任务对象,用于API返回
* @param {Object} task - 任务对象
* @returns {Object} 格式化后的任务信息
*/
const format_task_object = (task) => {
return {
name: task.name,
path: task.path,
schedule: task.schedule,
lastRun: toBeijingTime(task.lastRun),
nextRun: toBeijingTime(task.nextRun),
status: task.status,
// cronTask: task.cronTask,
}
};
/**
* 从CronJob对象获取下次运行时间
* @param {Object} job - CronJob实例
* @returns {Date|null} 下次运行时间
*/
function getNextRunFromJob(job) {
try {
if (!job) return null;
// 处理nextDate方法
if (typeof job.nextDate === 'function') {
const nd = job.nextDate();
if (!nd) return null;
if (nd instanceof Date) return nd;
if (typeof nd.toJSDate === 'function') return nd.toJSDate();
if (typeof nd.toDate === 'function') return nd.toDate();
if (typeof nd.toISOString === 'function') return new Date(nd.toISOString());
return new Date(nd);
}
// 处理nextDates方法
if (typeof job.nextDates === 'function') {
const arr = job.nextDates(1);
const nd = Array.isArray(arr) ? arr[0] : arr;
if (!nd) return null;
if (nd instanceof Date) return nd;
if (typeof nd.toJSDate === 'function') return nd.toJSDate();
if (typeof nd.toDate === 'function') return nd.toDate();
if (typeof nd.toISOString === 'function') return new Date(nd.toISOString());
return new Date(nd);
}
return null;
} catch (err) {
return null;
}
}
/**
* 注册单个脚本文件为定时任务
* @param {string} scriptPath - 脚本文件路径
*/
async function registerScript(scriptPath) {
try {
fastify.log.info(`📝 Registering script: ${scriptPath}`);
const scriptUrl = pathToFileURL(scriptPath).href;
const module = await import(scriptUrl);
const script = module.default || module;
if (typeof script.run !== 'function') {
fastify.log.warn(`⚠️ Script ${scriptPath} does not export a 'run' function`);
return;
}
const scriptName = path.basename(scriptPath, path.extname(scriptPath));
const taskInfo = {
name: scriptName,
path: scriptPath,
run: script.run,
schedule: script.schedule || null,
lastRun: null,
nextRun: null,
status: 'pending',
cronTask: null
};
taskRegistry.set(scriptName, taskInfo);
if (script.schedule) {
// 如果 schedule 是字符串(简洁写法)
if (typeof script.schedule === 'string') {
fastify.log.info(`⏰ Scheduling ${scriptName} with cron: ${script.schedule}`);
// 支持 runOnInit 与 timezone
const timezone = (script.schedule && script.timezone) || undefined;
const runOnInit = !!(script.runOnInit); // 不常用,通常传 object 形式时才会出现
const startImmediately = true; // 默认启动
const job = new CronJob(
script.schedule,
async () => {
await runTask(scriptName);
},
null, // onComplete
startImmediately,// start
timezone, // timeZone
fastify, // context
runOnInit // runOnInit
);
taskInfo.cronTask = job;
taskInfo.nextRun = getNextRunFromJob(job);
// 如果 schedule 是对象(支持更多选项)
} else if (typeof script.schedule === 'object' && script.schedule.cron) {
const userOpts = script.schedule.options || {};
const cronParams = {
cronTime: script.schedule.cron,
onTick: async () => {
await runTask(scriptName);
},
start: (typeof userOpts.scheduled !== 'undefined') ? !!userOpts.scheduled : true,
timeZone: script.schedule.timezone || userOpts.timeZone || 'UTC',
context: fastify,
runOnInit: (typeof script.schedule.runOnInit !== 'undefined') ? !!script.schedule.runOnInit : !!userOpts.runOnInit,
name: scriptName,
...userOpts
};
fastify.log.info(`⏰ Scheduling ${scriptName} with cron: ${script.schedule.cron} (timezone: ${cronParams.timeZone})`);
// 使用 from() 并让 cronParams.start/runOnInit 决定初次行为;不要再额外 start()/stop()
const job = CronJob.from(cronParams);
taskInfo.cronTask = job;
taskInfo.nextRun = getNextRunFromJob(job);
} else if (typeof script.schedule === 'function') {
fastify.log.info(`⏰ Registering custom scheduler for ${scriptName}`);
const updateNextRun = (date) => {
taskInfo.nextRun = date;
};
script.schedule(async () => {
await runTask(scriptName);
}, fastify, updateNextRun);
}
// **重要**:不再在这里手动调用 runTask(scriptName) 来处理 runOnInit
// 如果 script.schedule.runOnInit 为 true,我们把它传给 CronJob 让库负责首次触发,
// 否则如果你希望手动触发,可把 runOnInit 设为 false 并在脚本或外部触发。
} else {
fastify.log.info(`ℹ️ No schedule defined for ${scriptName}, manual execution only`);
}
taskInfo.status = 'registered';
} catch (err) {
fastify.log.error(`❌ Error registering script ${scriptPath}: ${err.message}`);
}
}
/**
* 执行指定的定时任务
* @param {string} taskName - 任务名称
*/
async function runTask(taskName) {
const task = taskRegistry.get(taskName);
if (!task) {
fastify.log.error(`❌ Task not found: ${taskName}`);
return;
}
try {
task.lastRun = new Date();
task.status = 'running';
fastify.log.info(`🚀 Starting task: ${taskName}`);
await task.run(fastify);
task.status = 'success';
fastify.log.info(`✅ Task completed: ${taskName}`);
} catch (err) {
task.status = 'failed';
fastify.log.error(`❌ Task failed: ${taskName} ${err.message}`);
}
if (task.cronTask) {
task.nextRun = getNextRunFromJob(task.cronTask);
}
}
/**
* 注册所有脚本目录下的定时任务脚本
*/
async function registerAllScripts() {
try {
fastify.log.info('📂 Loading scripts...');
const files = await readdir(config.scriptsDir);
for (const file of files) {
const filePath = path.join(config.scriptsDir, file);
const fileStat = await stat(filePath);
if (fileStat.isFile() && ['.mjs', '.js'].includes(path.extname(file)) && !scripts_exclude.includes(file)) {
await registerScript(filePath);
}
}
fastify.log.info(`✅ Registered ${taskRegistry.size} tasks`);
} catch (err) {
fastify.log.error(`❌ Error loading scripts:${err.message}`);
}
}
// API端点定义
/**
* 立即执行任务的API端点
* GET /execute-now/:taskName? - 执行指定任务或所有任务
*/
fastify.get('/execute-now/:taskName?', {preHandler: validateBasicAuth}, async (request, reply) => {
const {taskName} = request.params;
if (taskName) {
// 执行单个任务
if (!taskRegistry.has(taskName)) {
return reply.status(404).send({
error: 'Task not found',
availableTasks: [...taskRegistry.keys()]
});
}
await runTask(taskName);
return {
message: `Task "${taskName}" executed manually`,
status: 'success',
task: format_task_object(taskRegistry.get(taskName))
};
}
// 执行所有任务
for (const taskName of taskRegistry.keys()) {
await runTask(taskName);
}
return {
message: 'All tasks executed manually',
status: 'success',
tasks: [...taskRegistry.values()].map(t => t.name)
};
});
/**
* 获取所有任务列表的API端点
* GET /tasks - 返回所有注册的任务信息
*/
fastify.get('/tasks', {preHandler: validateBasicAuth}, async (request, reply) => {
const tasks = [...taskRegistry.values()].map(task => (format_task_object(task)));
return tasks;
});
/**
* 获取指定任务信息的API端点
* GET /tasks/:taskName - 返回指定任务的详细信息
*/
fastify.get('/tasks/:taskName', {preHandler: validateBasicAuth}, async (request, reply) => {
const {taskName} = request.params;
if (!taskRegistry.has(taskName)) {
return reply.status(404).send({
error: 'Task not found',
availableTasks: [...taskRegistry.keys()]
});
}
const task = taskRegistry.get(taskName);
return format_task_object(task);
});
/**
* 应用关闭时的清理钩子
* 停止所有正在运行的定时任务
*/
fastify.addHook('onClose', async () => {
fastify.log.info('🛑 Stopping all scheduled tasks...');
for (const task of taskRegistry.values()) {
if (task.cronTask && typeof task.cronTask.stop === 'function') {
try {
task.cronTask.stop();
fastify.log.info(`⏹️ Stopped task: ${task.name}`);
} catch (e) {
fastify.log.error(`❌ Failed stopping task ${task.name}: ${e.message}`);
}
}
}
taskRegistry.clear();
});
// 插件初始化
// 如果启用了定时任务功能,则注册所有脚本
(async () => {
try {
if (enable_tasker) await registerAllScripts();
done();
} catch (err) {
fastify.log.error(`❌ Failed to register scripts:${err.message}`);
done(err);
}
})();
};