@kne/fastify-task 3.0.0-alpha.3 → 3.0.0-alpha.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +548 -274
- package/libs/controllers/main.js +4 -0
- package/libs/controllers/statistics.js +5 -2
- package/libs/models/task.js +5 -0
- package/libs/services/main.js +67 -13
- package/libs/services/statistics.js +490 -336
- package/package.json +3 -2
package/libs/controllers/main.js
CHANGED
|
@@ -52,6 +52,10 @@ module.exports = fp(async (fastify, options) => {
|
|
|
52
52
|
maxRetries: {
|
|
53
53
|
type: 'number',
|
|
54
54
|
description: '最大自动重试次数,默认0'
|
|
55
|
+
},
|
|
56
|
+
timeout: {
|
|
57
|
+
type: 'number',
|
|
58
|
+
description: '任务超时时间(分钟),0表示不超时,默认60分钟'
|
|
55
59
|
}
|
|
56
60
|
},
|
|
57
61
|
required: ['type', 'targetId', 'targetType']
|
|
@@ -36,15 +36,18 @@ module.exports = fp(async (fastify, options) => {
|
|
|
36
36
|
query: {
|
|
37
37
|
type: 'object',
|
|
38
38
|
properties: {
|
|
39
|
+
range: { type: 'string', default: '7d', description: '时间范围: 7d=近7天, 1m=近1个月, 3m=近3个月, 1y=近1年' },
|
|
39
40
|
timezone: { type: 'string', description: '时区,如 Asia/Shanghai,默认服务器时区' },
|
|
41
|
+
type: { type: 'string', description: '按任务类型筛选' },
|
|
42
|
+
runnerType: { type: 'string', description: '按执行方式筛选 manual / system' },
|
|
40
43
|
interval: { type: 'integer', minimum: 1, default: 5, description: '推送间隔时间(秒),最小1秒,默认5秒' }
|
|
41
44
|
}
|
|
42
45
|
}
|
|
43
46
|
}
|
|
44
47
|
},
|
|
45
48
|
async (request, reply) => {
|
|
46
|
-
const { timezone, interval } = request.query;
|
|
47
|
-
await services.sseStatistics({ timezone, interval }, reply);
|
|
49
|
+
const { range = '7d', timezone, type, runnerType, interval } = request.query;
|
|
50
|
+
await services.sseStatistics({ range, timezone, type, runnerType, interval }, reply);
|
|
48
51
|
}
|
|
49
52
|
);
|
|
50
53
|
});
|
package/libs/models/task.js
CHANGED
|
@@ -43,6 +43,11 @@ module.exports = ({ DataTypes, definePrimaryType, options }) => {
|
|
|
43
43
|
comment: '最大重试次数,0表示不自动重试',
|
|
44
44
|
defaultValue: 0
|
|
45
45
|
},
|
|
46
|
+
timeout: {
|
|
47
|
+
type: DataTypes.INTEGER,
|
|
48
|
+
comment: '任务超时时间(分钟),0表示不超时,默认60分钟',
|
|
49
|
+
defaultValue: 60
|
|
50
|
+
},
|
|
46
51
|
startTime: {
|
|
47
52
|
type: DataTypes.DATE,
|
|
48
53
|
comment: '任务最早执行时间',
|
package/libs/services/main.js
CHANGED
|
@@ -32,8 +32,8 @@ module.exports = fp(async (fastify, options) => {
|
|
|
32
32
|
hasTiming = totalTime > 0;
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
const
|
|
36
|
-
const channel = `${task.type}:${task.runnerType || 'manual'}:${
|
|
35
|
+
const completedHour = new Date(completedAt).getHours();
|
|
36
|
+
const channel = `${task.type}:${task.runnerType || 'manual'}:${completedHour}`;
|
|
37
37
|
const status = task.status || 'unknown';
|
|
38
38
|
|
|
39
39
|
const data = { total: 1, [status]: 1 };
|
|
@@ -67,7 +67,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
67
67
|
return signature === expectedSignature;
|
|
68
68
|
};
|
|
69
69
|
|
|
70
|
-
const create = async ({ userId, input, type, targetId, targetType, runnerType, delay = 0, scriptName, priority = 0, parentTaskId, maxRetries = 0, options: currentOptions }) => {
|
|
70
|
+
const create = async ({ userId, input, type, targetId, targetType, runnerType, delay = 0, scriptName, priority = 0, parentTaskId, maxRetries = 0, timeout = 60, options: currentOptions }) => {
|
|
71
71
|
if (typeof delay !== 'number' || delay < 0) {
|
|
72
72
|
throw new Error('delay 必须为非负数');
|
|
73
73
|
}
|
|
@@ -77,10 +77,13 @@ module.exports = fp(async (fastify, options) => {
|
|
|
77
77
|
if (typeof maxRetries !== 'number' || !Number.isInteger(maxRetries) || maxRetries < 0) {
|
|
78
78
|
throw new Error('maxRetries 必须为非负整数');
|
|
79
79
|
}
|
|
80
|
+
if (typeof timeout !== 'number' || !Number.isInteger(timeout) || timeout < 0) {
|
|
81
|
+
throw new Error('timeout 必须为非负整数');
|
|
82
|
+
}
|
|
80
83
|
if (typeof options.task[type] !== 'function') {
|
|
81
84
|
throw new Error('未找到合法的任务声明');
|
|
82
85
|
}
|
|
83
|
-
|
|
86
|
+
const task = await models.task.create({
|
|
84
87
|
userId,
|
|
85
88
|
input,
|
|
86
89
|
type,
|
|
@@ -90,22 +93,23 @@ module.exports = fp(async (fastify, options) => {
|
|
|
90
93
|
priority,
|
|
91
94
|
parentTaskId,
|
|
92
95
|
maxRetries,
|
|
96
|
+
timeout,
|
|
93
97
|
startTime: delay > 0 ? new Date(Date.now() + 1000 * delay) : new Date(),
|
|
94
98
|
scriptName,
|
|
95
99
|
options: currentOptions
|
|
96
100
|
});
|
|
101
|
+
return task;
|
|
97
102
|
};
|
|
98
103
|
|
|
99
104
|
const resetAll = async () => {
|
|
105
|
+
const runningTasks = await models.task.findAll({
|
|
106
|
+
where: { status: 'running' },
|
|
107
|
+
attributes: ['type', 'runnerType'],
|
|
108
|
+
raw: true
|
|
109
|
+
});
|
|
100
110
|
await models.task.update(
|
|
101
|
-
{
|
|
102
|
-
|
|
103
|
-
},
|
|
104
|
-
{
|
|
105
|
-
where: {
|
|
106
|
-
status: 'running'
|
|
107
|
-
}
|
|
108
|
-
}
|
|
111
|
+
{ status: 'pending' },
|
|
112
|
+
{ where: { status: 'running' } }
|
|
109
113
|
);
|
|
110
114
|
};
|
|
111
115
|
|
|
@@ -308,7 +312,42 @@ module.exports = fp(async (fastify, options) => {
|
|
|
308
312
|
}
|
|
309
313
|
};
|
|
310
314
|
|
|
315
|
+
/** 检查超时任务,将超时的 running/waiting 任务标记为 failed */
|
|
316
|
+
const checkTimeout = async () => {
|
|
317
|
+
const now = new Date();
|
|
318
|
+
const timedOutTasks = await models.task.findAll({
|
|
319
|
+
where: {
|
|
320
|
+
status: { [Op.in]: ['running', 'waiting'] },
|
|
321
|
+
timeout: { [Op.gt]: 0 },
|
|
322
|
+
startedAt: { [Op.ne]: null }
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
const tasksToFail = timedOutTasks.filter(task => {
|
|
327
|
+
const elapsed = now.getTime() - new Date(task.startedAt).getTime();
|
|
328
|
+
return elapsed > task.timeout * 60 * 1000;
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
if (tasksToFail.length === 0) return;
|
|
332
|
+
|
|
333
|
+
fastify.log.info(`检测到 ${tasksToFail.length} 个超时任务`);
|
|
334
|
+
|
|
335
|
+
for (const task of tasksToFail) {
|
|
336
|
+
const elapsed = now.getTime() - new Date(task.startedAt).getTime();
|
|
337
|
+
const prevStatus = task.status;
|
|
338
|
+
await task.update({
|
|
339
|
+
status: 'failed',
|
|
340
|
+
error: `任务超时:已执行 ${Math.round(elapsed / 60000)} 分钟,超过设定的 ${task.timeout} 分钟超时时间`,
|
|
341
|
+
completedAt: now
|
|
342
|
+
});
|
|
343
|
+
collectTaskStatistics(task);
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
|
|
311
347
|
const runner = async () => {
|
|
348
|
+
// 检查超时任务
|
|
349
|
+
await checkTimeout();
|
|
350
|
+
|
|
312
351
|
const runningTaskCount = await models.task.count({
|
|
313
352
|
where: {
|
|
314
353
|
runnerType: 'system',
|
|
@@ -395,6 +434,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
395
434
|
|
|
396
435
|
const complete = async ({ id, userId, output, ...props }) => {
|
|
397
436
|
const task = await detail({ id });
|
|
437
|
+
const prevStatus = task.status;
|
|
398
438
|
if (props.status === 'success') {
|
|
399
439
|
try {
|
|
400
440
|
await options.task[task.type]({ task, result: output });
|
|
@@ -434,14 +474,28 @@ module.exports = fp(async (fastify, options) => {
|
|
|
434
474
|
};
|
|
435
475
|
|
|
436
476
|
const waitingComplete = async ({ id, pollInterval = 1000, maxPollTimes = 20 }) => {
|
|
437
|
-
|
|
477
|
+
let task = await detail({ id });
|
|
438
478
|
|
|
439
479
|
if (task.status === 'pending') {
|
|
440
480
|
// 标记为最高优先级,确保立即执行(即使因重试等重新入队也会被优先处理)
|
|
441
481
|
await task.update({ priority: Number.MAX_SAFE_INTEGER });
|
|
442
482
|
await processSystemTask(task);
|
|
483
|
+
// processSystemTask 完成后重新加载任务状态
|
|
484
|
+
await task.reload();
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
// 立即检查是否已到达终态
|
|
488
|
+
if (task.status === 'success') {
|
|
489
|
+
return task.output;
|
|
490
|
+
}
|
|
491
|
+
if (task.status === 'failed') {
|
|
492
|
+
throw new Error(task.error || '任务失败');
|
|
493
|
+
}
|
|
494
|
+
if (task.status === 'canceled') {
|
|
495
|
+
throw new Error(task.error || '任务取消');
|
|
443
496
|
}
|
|
444
497
|
|
|
498
|
+
// 任务仍在运行中,开始轮询
|
|
445
499
|
let pollCount = 0;
|
|
446
500
|
return await new Promise((resolve, reject) => {
|
|
447
501
|
const executePolling = async () => {
|