@kne/fastify-task 3.0.0-alpha.7 → 3.0.0-alpha.9
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 +454 -21
- package/index.js +1 -1
- package/libs/controllers/main.js +0 -3
- package/libs/helpers/task-service-context.js +556 -0
- package/libs/models/task.js +1 -1
- package/libs/services/append.js +45 -0
- package/libs/services/callback.js +109 -0
- package/libs/services/executor.js +10 -0
- package/libs/services/main.js +7 -776
- package/libs/services/scheduler.js +230 -0
- package/libs/services/statistics.js +2 -4
- package/libs/services/task.js +224 -0
- package/package.json +2 -2
package/libs/services/main.js
CHANGED
|
@@ -1,780 +1,11 @@
|
|
|
1
1
|
const fp = require('fastify-plugin');
|
|
2
|
-
const
|
|
3
|
-
const fs = require('fs-extra');
|
|
4
|
-
const crypto = require('node:crypto');
|
|
2
|
+
const getTaskServiceContext = require('../helpers/task-service-context');
|
|
5
3
|
|
|
6
4
|
module.exports = fp(async (fastify, options) => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
if (task.reload && typeof task.reload === 'function') {
|
|
14
|
-
await task.reload();
|
|
15
|
-
}
|
|
16
|
-
const completedAt = task.completedAt || new Date();
|
|
17
|
-
const createdAt = task.createdAt;
|
|
18
|
-
const startedAt = task.startedAt;
|
|
19
|
-
let waitingTime = 0;
|
|
20
|
-
let executionTime = 0;
|
|
21
|
-
let totalTime = 0;
|
|
22
|
-
let hasTiming = false;
|
|
23
|
-
|
|
24
|
-
if (createdAt && completedAt) {
|
|
25
|
-
totalTime = new Date(completedAt).getTime() - new Date(createdAt).getTime();
|
|
26
|
-
if (startedAt) {
|
|
27
|
-
waitingTime = new Date(startedAt).getTime() - new Date(createdAt).getTime();
|
|
28
|
-
executionTime = new Date(completedAt).getTime() - new Date(startedAt).getTime();
|
|
29
|
-
} else {
|
|
30
|
-
executionTime = totalTime;
|
|
31
|
-
}
|
|
32
|
-
hasTiming = totalTime > 0;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const completedHour = new Date(completedAt).getHours();
|
|
36
|
-
const channel = `${task.type}:${task.runnerType || 'manual'}:${completedHour}`;
|
|
37
|
-
const status = task.status || 'unknown';
|
|
38
|
-
|
|
39
|
-
const data = { total: 1, [status]: 1 };
|
|
40
|
-
const unit = { total: 'count', [status]: 'count' };
|
|
41
|
-
if (hasTiming) {
|
|
42
|
-
Object.assign(data, { waitingTime, executionTime, totalTime });
|
|
43
|
-
Object.assign(unit, { waitingTime: 'ms', executionTime: 'ms', totalTime: 'ms' });
|
|
44
|
-
}
|
|
45
|
-
fastify[`${options.name}Statistics`].services
|
|
46
|
-
.collect({
|
|
47
|
-
channel,
|
|
48
|
-
data,
|
|
49
|
-
unit,
|
|
50
|
-
time: completedAt
|
|
51
|
-
})
|
|
52
|
-
.catch(e => {
|
|
53
|
-
fastify.log.error(`采集任务统计数据失败: ${e.message}`);
|
|
54
|
-
});
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
const generateSignature = ({ secret, id, data }) => {
|
|
58
|
-
const dataToSign = `${id}|${typeof data === 'string' ? data : JSON.stringify(data)}`;
|
|
59
|
-
const hmac = crypto.createHmac('sha256', secret);
|
|
60
|
-
hmac.update(dataToSign);
|
|
61
|
-
return hmac.digest('hex');
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
const verifySignature = ({ secret, id, data, signature }) => {
|
|
65
|
-
if (!secret) return true;
|
|
66
|
-
const expectedSignature = generateSignature({ secret, id, data });
|
|
67
|
-
return signature === expectedSignature;
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
const create = async ({ userId, input, type, targetId, targetType, runnerType, delay = 0, scriptName, priority = 0, parentTaskId, maxRetries = 0, timeout = 60 * 60 * 1000, options: currentOptions }) => {
|
|
71
|
-
if (typeof delay !== 'number' || delay < 0) {
|
|
72
|
-
throw new Error('delay 必须为非负数');
|
|
73
|
-
}
|
|
74
|
-
if (typeof priority !== 'number' || !Number.isInteger(priority)) {
|
|
75
|
-
throw new Error('priority 必须为整数');
|
|
76
|
-
}
|
|
77
|
-
if (typeof maxRetries !== 'number' || !Number.isInteger(maxRetries) || maxRetries < 0) {
|
|
78
|
-
throw new Error('maxRetries 必须为非负整数');
|
|
79
|
-
}
|
|
80
|
-
if (typeof timeout !== 'number' || !Number.isInteger(timeout) || timeout < 0) {
|
|
81
|
-
throw new Error('timeout 必须为非负整数');
|
|
82
|
-
}
|
|
83
|
-
if (typeof options.task[type] !== 'function') {
|
|
84
|
-
throw new Error('未找到合法的任务声明');
|
|
85
|
-
}
|
|
86
|
-
const task = await models.task.create({
|
|
87
|
-
userId,
|
|
88
|
-
input,
|
|
89
|
-
type,
|
|
90
|
-
targetId,
|
|
91
|
-
targetType,
|
|
92
|
-
runnerType,
|
|
93
|
-
priority,
|
|
94
|
-
parentTaskId,
|
|
95
|
-
maxRetries,
|
|
96
|
-
timeout,
|
|
97
|
-
startTime: delay > 0 ? new Date(Date.now() + 1000 * delay) : new Date(),
|
|
98
|
-
scriptName,
|
|
99
|
-
options: currentOptions
|
|
100
|
-
});
|
|
101
|
-
return task;
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
const getRunningRecoveryBefore = () => {
|
|
105
|
-
const recoverAfter = Number.isInteger(options.recoverRunningTaskAfter) && options.recoverRunningTaskAfter >= 0 ? options.recoverRunningTaskAfter : 30 * 60 * 1000;
|
|
106
|
-
return new Date(Date.now() - recoverAfter);
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
const getRunningResetWhere = ({ staleOnly = false, before } = {}) => {
|
|
110
|
-
const where = { status: 'running' };
|
|
111
|
-
if (staleOnly) {
|
|
112
|
-
where[Op.or] = [{ startedAt: { [Op.lte]: before || getRunningRecoveryBefore() } }, { startedAt: null }];
|
|
113
|
-
}
|
|
114
|
-
return where;
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
const resetAll = async (props = {}) => {
|
|
118
|
-
const [affectedCount] = await models.task.update({ status: 'pending' }, { where: getRunningResetWhere(props) });
|
|
119
|
-
return affectedCount;
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
const executor = async ({ type, scriptName, ...props }) => {
|
|
123
|
-
const scriptFile = `${scriptName || options.scriptName}.js`;
|
|
124
|
-
let taskModulePath = null;
|
|
125
|
-
for (const dir of options.dirs) {
|
|
126
|
-
const candidate = path.resolve(dir, type, scriptFile);
|
|
127
|
-
if (await fs.exists(candidate)) {
|
|
128
|
-
taskModulePath = candidate;
|
|
129
|
-
break;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
if (!taskModulePath) {
|
|
133
|
-
throw new Error(`未匹配到任务执行器:${type}/${scriptFile},已搜索目录:${options.dirs.join(',')}`);
|
|
134
|
-
}
|
|
135
|
-
// 清除 require 缓存,确保运行期间模块更新后能加载最新版本
|
|
136
|
-
delete require.cache[taskModulePath];
|
|
137
|
-
return await require(taskModulePath)(fastify, options, {
|
|
138
|
-
...props,
|
|
139
|
-
updateProgress: async progress => {
|
|
140
|
-
typeof props?.task?.update === 'function' &&
|
|
141
|
-
(await props.task.update({
|
|
142
|
-
progress: progress
|
|
143
|
-
}));
|
|
144
|
-
},
|
|
145
|
-
polling: async (callback, currentOptions) => {
|
|
146
|
-
let pollCount = 0;
|
|
147
|
-
const maxPollTimes = currentOptions?.maxPollTimes || options.maxPollTimes;
|
|
148
|
-
const pollInterval = currentOptions?.pollInterval || options.pollInterval;
|
|
149
|
-
return await new Promise((resolve, reject) => {
|
|
150
|
-
const executePolling = async () => {
|
|
151
|
-
try {
|
|
152
|
-
pollCount++;
|
|
153
|
-
if (pollCount > maxPollTimes) {
|
|
154
|
-
reject(new Error(`轮询超时(${maxPollTimes}次),任务未完成`));
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
const pollingResult = Object.assign({}, await callback());
|
|
159
|
-
const { result, data, message, progress } = pollingResult;
|
|
160
|
-
|
|
161
|
-
if (typeof props?.task?.update === 'function') {
|
|
162
|
-
await props.task.reload();
|
|
163
|
-
await props.task.update(
|
|
164
|
-
Object.assign(
|
|
165
|
-
{},
|
|
166
|
-
{
|
|
167
|
-
pollCount: props.task.pollCount + 1,
|
|
168
|
-
pollResults: [...props.task.pollResults, Object.assign({}, pollingResult, { time: new Date() })]
|
|
169
|
-
},
|
|
170
|
-
Number.isInteger(progress) ? { progress } : {}
|
|
171
|
-
)
|
|
172
|
-
);
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
if (result === 'failed') {
|
|
176
|
-
reject(new Error(`任务处理失败:${message}`));
|
|
177
|
-
return;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
if (result === 'success') {
|
|
181
|
-
resolve(data);
|
|
182
|
-
return;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
// 任务未完成,等待pollInterval后继续执行
|
|
186
|
-
setTimeout(executePolling, pollInterval);
|
|
187
|
-
} catch (e) {
|
|
188
|
-
reject(e);
|
|
189
|
-
}
|
|
190
|
-
};
|
|
191
|
-
// 开始第一次轮询
|
|
192
|
-
setTimeout(executePolling, pollInterval);
|
|
193
|
-
});
|
|
194
|
-
},
|
|
195
|
-
next: async context => {
|
|
196
|
-
if (typeof props?.task?.update === 'function') {
|
|
197
|
-
await props.task.update({ context, status: 'waiting' });
|
|
198
|
-
}
|
|
199
|
-
return false;
|
|
200
|
-
}
|
|
201
|
-
});
|
|
202
|
-
};
|
|
203
|
-
|
|
204
|
-
const processNext = async ({ id, signature, result: resultStr }) => {
|
|
205
|
-
const task = await detail({ id });
|
|
206
|
-
if (task.status !== 'waiting') {
|
|
207
|
-
throw new Error('当前任务状态不允许执行Next操作');
|
|
208
|
-
}
|
|
209
|
-
if (task.context?.secret && !verifySignature({ secret: task.context.secret, id, data: resultStr, signature })) {
|
|
210
|
-
throw new Error('签名验证失败');
|
|
211
|
-
}
|
|
212
|
-
const result = JSON.parse(resultStr);
|
|
213
|
-
if (result.code !== 0) {
|
|
214
|
-
await task.update({
|
|
215
|
-
status: 'failed',
|
|
216
|
-
error: result,
|
|
217
|
-
completedAt: new Date()
|
|
218
|
-
});
|
|
219
|
-
collectTaskStatistics(task);
|
|
220
|
-
return;
|
|
221
|
-
}
|
|
222
|
-
await options.task[task.type]({ task, result: result.data, context: task.context });
|
|
223
|
-
await task.update({
|
|
224
|
-
status: 'success',
|
|
225
|
-
output: result,
|
|
226
|
-
progress: 100,
|
|
227
|
-
completedAt: new Date()
|
|
228
|
-
});
|
|
229
|
-
collectTaskStatistics(task);
|
|
230
|
-
};
|
|
231
|
-
|
|
232
|
-
const processSystemTask = async (task, { claimed = false } = {}) => {
|
|
233
|
-
if (!claimed) {
|
|
234
|
-
await task.update({
|
|
235
|
-
status: 'running',
|
|
236
|
-
startedAt: new Date(),
|
|
237
|
-
progress: 0,
|
|
238
|
-
error: null,
|
|
239
|
-
output: null,
|
|
240
|
-
pollCount: 0,
|
|
241
|
-
pollResults: [],
|
|
242
|
-
completedAt: null,
|
|
243
|
-
context: {}
|
|
244
|
-
});
|
|
245
|
-
}
|
|
246
|
-
const addLog = props => {
|
|
247
|
-
return log(Object.assign({}, props, { taskId: task.id }));
|
|
248
|
-
};
|
|
249
|
-
|
|
250
|
-
// #3: 任务超时自动失败
|
|
251
|
-
const createTimeout = ms => new Promise((_, reject) => setTimeout(() => reject(new Error(`任务执行超时(${ms}ms)`)), ms));
|
|
252
|
-
|
|
253
|
-
const executorPromise = executor({ type: task.type, scriptName: task.scriptName, task, log: addLog });
|
|
254
|
-
const racePromise = options.taskTimeout > 0 ? Promise.race([executorPromise, createTimeout(options.taskTimeout)]) : executorPromise;
|
|
255
|
-
|
|
256
|
-
return racePromise
|
|
257
|
-
.then(async result => {
|
|
258
|
-
if (result === false) {
|
|
259
|
-
return;
|
|
260
|
-
}
|
|
261
|
-
await options.task[task.type]({ task, result, context: task.context });
|
|
262
|
-
await task.update({
|
|
263
|
-
status: 'success',
|
|
264
|
-
output: result,
|
|
265
|
-
progress: 100,
|
|
266
|
-
completedAt: new Date()
|
|
267
|
-
});
|
|
268
|
-
collectTaskStatistics(task);
|
|
269
|
-
// #2: 任务依赖/链式执行 — 父任务成功后激活子任务
|
|
270
|
-
await triggerChildTasks(task);
|
|
271
|
-
})
|
|
272
|
-
.catch(async e => {
|
|
273
|
-
// #4: 重试策略 — 判断是否自动重试
|
|
274
|
-
const currentRetryCount = (task.retryCount || 0) + 1;
|
|
275
|
-
const maxRetries = task.maxRetries || 0;
|
|
276
|
-
if (currentRetryCount < maxRetries) {
|
|
277
|
-
// 指数退避:baseDelay * 2^retryCount
|
|
278
|
-
const baseDelay = options.retryBaseDelay || 5000;
|
|
279
|
-
const backoffDelay = baseDelay * Math.pow(2, currentRetryCount - 1);
|
|
280
|
-
await task.update({
|
|
281
|
-
status: 'pending',
|
|
282
|
-
retryCount: currentRetryCount,
|
|
283
|
-
error: (e.stack || '').replaceAll(process.cwd(), '/server'),
|
|
284
|
-
startTime: new Date(Date.now() + backoffDelay),
|
|
285
|
-
completedAt: null
|
|
286
|
-
});
|
|
287
|
-
fastify.log.info(`任务 ${task.id} 第 ${currentRetryCount} 次重试,${backoffDelay}ms 后执行`);
|
|
288
|
-
} else {
|
|
289
|
-
await task.update({
|
|
290
|
-
status: 'failed',
|
|
291
|
-
error: (e.stack || '').replaceAll(process.cwd(), '/server'),
|
|
292
|
-
completedAt: new Date(),
|
|
293
|
-
retryCount: currentRetryCount
|
|
294
|
-
});
|
|
295
|
-
collectTaskStatistics(task);
|
|
296
|
-
}
|
|
297
|
-
});
|
|
298
|
-
};
|
|
299
|
-
|
|
300
|
-
// #2: 触发子任务(父任务成功后激活 pending 的子任务)
|
|
301
|
-
const triggerChildTasks = async parentTask => {
|
|
302
|
-
const childTasks = await models.task.findAll({
|
|
303
|
-
where: {
|
|
304
|
-
parentTaskId: parentTask.id,
|
|
305
|
-
status: 'pending'
|
|
306
|
-
}
|
|
307
|
-
});
|
|
308
|
-
if (childTasks.length === 0) return;
|
|
309
|
-
|
|
310
|
-
fastify.log.info(`父任务 ${parentTask.id} 完成,激活 ${childTasks.length} 个子任务`);
|
|
311
|
-
for (const child of childTasks) {
|
|
312
|
-
if (child.runnerType === 'system') {
|
|
313
|
-
await processSystemTask(child);
|
|
314
|
-
}
|
|
315
|
-
// manual 类型子任务保持 pending,等待手动执行
|
|
316
|
-
}
|
|
317
|
-
};
|
|
318
|
-
|
|
319
|
-
/** 检查超时任务,将超时的 running/waiting 任务标记为 failed */
|
|
320
|
-
const checkTimeout = async () => {
|
|
321
|
-
const now = new Date();
|
|
322
|
-
const timedOutTasks = await models.task.findAll({
|
|
323
|
-
where: {
|
|
324
|
-
status: { [Op.in]: ['running', 'waiting'] },
|
|
325
|
-
timeout: { [Op.gt]: 0 },
|
|
326
|
-
startedAt: { [Op.ne]: null }
|
|
327
|
-
}
|
|
328
|
-
});
|
|
329
|
-
|
|
330
|
-
const tasksToFail = timedOutTasks.filter(task => {
|
|
331
|
-
const elapsed = now.getTime() - new Date(task.startedAt).getTime();
|
|
332
|
-
return elapsed > task.timeout;
|
|
333
|
-
});
|
|
334
|
-
|
|
335
|
-
if (tasksToFail.length === 0) return;
|
|
336
|
-
|
|
337
|
-
fastify.log.info(`检测到 ${tasksToFail.length} 个超时任务`);
|
|
338
|
-
|
|
339
|
-
for (const task of tasksToFail) {
|
|
340
|
-
const elapsed = now.getTime() - new Date(task.startedAt).getTime();
|
|
341
|
-
const prevStatus = task.status;
|
|
342
|
-
await task.update({
|
|
343
|
-
status: 'failed',
|
|
344
|
-
error: `任务超时:已执行 ${elapsed}ms,超过设定的 ${task.timeout}ms 超时时间`,
|
|
345
|
-
completedAt: now
|
|
346
|
-
});
|
|
347
|
-
collectTaskStatistics(task);
|
|
348
|
-
}
|
|
349
|
-
};
|
|
350
|
-
|
|
351
|
-
const claimPendingTasks = async limit => {
|
|
352
|
-
if (limit <= 0) return [];
|
|
353
|
-
const now = new Date();
|
|
354
|
-
const pendingTasks = await models.task.findAll({
|
|
355
|
-
where: {
|
|
356
|
-
runnerType: 'system',
|
|
357
|
-
status: 'pending',
|
|
358
|
-
startTime: {
|
|
359
|
-
[Op.lte]: now
|
|
360
|
-
}
|
|
361
|
-
},
|
|
362
|
-
order: [
|
|
363
|
-
['priority', 'DESC'],
|
|
364
|
-
['startTime', 'ASC']
|
|
365
|
-
],
|
|
366
|
-
limit: limit
|
|
367
|
-
});
|
|
368
|
-
|
|
369
|
-
const claimedTasks = [];
|
|
370
|
-
for (const task of pendingTasks) {
|
|
371
|
-
const [affectedCount] = await models.task.update(
|
|
372
|
-
{
|
|
373
|
-
status: 'running',
|
|
374
|
-
startedAt: now,
|
|
375
|
-
progress: 0,
|
|
376
|
-
error: null,
|
|
377
|
-
output: null,
|
|
378
|
-
pollCount: 0,
|
|
379
|
-
pollResults: [],
|
|
380
|
-
completedAt: null,
|
|
381
|
-
context: {}
|
|
382
|
-
},
|
|
383
|
-
{
|
|
384
|
-
where: {
|
|
385
|
-
id: task.id,
|
|
386
|
-
runnerType: 'system',
|
|
387
|
-
status: 'pending',
|
|
388
|
-
startTime: {
|
|
389
|
-
[Op.lte]: now
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
);
|
|
394
|
-
if (affectedCount === 1) {
|
|
395
|
-
const claimedTask = await models.task.findByPk(task.id);
|
|
396
|
-
if (claimedTask) {
|
|
397
|
-
claimedTasks.push(claimedTask);
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
return claimedTasks;
|
|
402
|
-
};
|
|
403
|
-
|
|
404
|
-
const runner = async () => {
|
|
405
|
-
// 检查超时任务
|
|
406
|
-
await checkTimeout();
|
|
407
|
-
|
|
408
|
-
const runningTaskCount = await models.task.count({
|
|
409
|
-
where: {
|
|
410
|
-
runnerType: 'system',
|
|
411
|
-
status: 'running'
|
|
412
|
-
}
|
|
413
|
-
});
|
|
414
|
-
const limit = options.limit - runningTaskCount;
|
|
415
|
-
if (limit <= 0) {
|
|
416
|
-
fastify.log.info(`当前运行中的系统任务数(${runningTaskCount})已达上限(${options.limit}),暂不执行新任务`);
|
|
417
|
-
return;
|
|
418
|
-
}
|
|
419
|
-
const pendingTasks = await claimPendingTasks(limit);
|
|
420
|
-
|
|
421
|
-
if (pendingTasks.length > 0) {
|
|
422
|
-
fastify.log.info(`本轮执行 ${pendingTasks.length} 个待处理的系统任务`);
|
|
423
|
-
await Promise.all(pendingTasks.map(async task => processSystemTask(task, { claimed: true })));
|
|
424
|
-
}
|
|
425
|
-
};
|
|
426
|
-
|
|
427
|
-
const detail = async ({ id }) => {
|
|
428
|
-
const task = await models.task.findByPk(id);
|
|
429
|
-
if (!task) {
|
|
430
|
-
throw new Error('任务不存在');
|
|
431
|
-
}
|
|
432
|
-
return task;
|
|
433
|
-
};
|
|
434
|
-
|
|
435
|
-
const cancel = async ({ id, targetId, targetType, type }) => {
|
|
436
|
-
if (!id && !(targetId && targetType && type)) {
|
|
437
|
-
throw new Error('必须提供 id 或 targetId+targetType+type');
|
|
438
|
-
}
|
|
439
|
-
if (targetId && targetType && type) {
|
|
440
|
-
const bulkWhere = {
|
|
441
|
-
targetId,
|
|
442
|
-
targetType,
|
|
443
|
-
type,
|
|
444
|
-
status: {
|
|
445
|
-
[Op.in]: ['pending', 'running']
|
|
446
|
-
}
|
|
447
|
-
};
|
|
448
|
-
const tasksToCancel = await models.task.findAll({ where: bulkWhere, raw: true });
|
|
449
|
-
const completedAt = new Date();
|
|
450
|
-
const [affectedCount] = await models.task.update(
|
|
451
|
-
{
|
|
452
|
-
status: 'canceled',
|
|
453
|
-
completedAt
|
|
454
|
-
},
|
|
455
|
-
{
|
|
456
|
-
where: bulkWhere
|
|
457
|
-
}
|
|
458
|
-
);
|
|
459
|
-
for (const row of tasksToCancel) {
|
|
460
|
-
collectTaskStatistics(Object.assign({}, row, { status: 'canceled', completedAt }));
|
|
461
|
-
}
|
|
462
|
-
return affectedCount;
|
|
463
|
-
}
|
|
464
|
-
if (id) {
|
|
465
|
-
const task = await detail({ id });
|
|
466
|
-
if (!['pending', 'running'].includes(task.status)) {
|
|
467
|
-
return;
|
|
468
|
-
}
|
|
469
|
-
await task.update({
|
|
470
|
-
status: 'canceled',
|
|
471
|
-
completedAt: new Date()
|
|
472
|
-
});
|
|
473
|
-
collectTaskStatistics(task);
|
|
474
|
-
}
|
|
475
|
-
};
|
|
476
|
-
|
|
477
|
-
const complete = async ({ id, userId, output, ...props }) => {
|
|
478
|
-
const task = await detail({ id });
|
|
479
|
-
const prevStatus = task.status;
|
|
480
|
-
if (props.status === 'success') {
|
|
481
|
-
try {
|
|
482
|
-
await options.task[task.type]({ task, result: output });
|
|
483
|
-
await task.update({
|
|
484
|
-
status: 'success',
|
|
485
|
-
output,
|
|
486
|
-
progress: 100,
|
|
487
|
-
completedAt: new Date(),
|
|
488
|
-
startedAt: task.startedAt || task.createdAt,
|
|
489
|
-
completedUserId: userId
|
|
490
|
-
});
|
|
491
|
-
collectTaskStatistics(task);
|
|
492
|
-
} catch (e) {
|
|
493
|
-
await task.update(
|
|
494
|
-
Object.assign({}, props, {
|
|
495
|
-
status: 'failed',
|
|
496
|
-
output,
|
|
497
|
-
error: (e.stack || '').replaceAll(process.cwd(), '/server'),
|
|
498
|
-
completedAt: new Date(),
|
|
499
|
-
startedAt: task.startedAt || task.createdAt
|
|
500
|
-
})
|
|
501
|
-
);
|
|
502
|
-
collectTaskStatistics(task);
|
|
503
|
-
throw e;
|
|
504
|
-
}
|
|
505
|
-
} else {
|
|
506
|
-
await task.update(
|
|
507
|
-
Object.assign({}, props, {
|
|
508
|
-
status: 'failed',
|
|
509
|
-
completedAt: new Date(),
|
|
510
|
-
startedAt: task.startedAt || task.createdAt,
|
|
511
|
-
completedUserId: userId
|
|
512
|
-
})
|
|
513
|
-
);
|
|
514
|
-
collectTaskStatistics(task);
|
|
515
|
-
}
|
|
516
|
-
};
|
|
517
|
-
|
|
518
|
-
const waitingComplete = async ({ id, pollInterval = 1000, maxPollTimes = 20 }) => {
|
|
519
|
-
let task = await detail({ id });
|
|
520
|
-
|
|
521
|
-
if (task.status === 'pending') {
|
|
522
|
-
// 标记为最高优先级,确保立即执行(即使因重试等重新入队也会被优先处理)
|
|
523
|
-
await task.update({ priority: Number.MAX_SAFE_INTEGER });
|
|
524
|
-
await processSystemTask(task);
|
|
525
|
-
// processSystemTask 完成后重新加载任务状态
|
|
526
|
-
await task.reload();
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
// 立即检查是否已到达终态
|
|
530
|
-
if (task.status === 'success') {
|
|
531
|
-
return task.output;
|
|
532
|
-
}
|
|
533
|
-
if (task.status === 'failed') {
|
|
534
|
-
throw new Error(task.error || '任务失败');
|
|
535
|
-
}
|
|
536
|
-
if (task.status === 'canceled') {
|
|
537
|
-
throw new Error(task.error || '任务取消');
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
// 任务仍在运行中,开始轮询
|
|
541
|
-
let pollCount = 0;
|
|
542
|
-
return await new Promise((resolve, reject) => {
|
|
543
|
-
const executePolling = async () => {
|
|
544
|
-
try {
|
|
545
|
-
pollCount++;
|
|
546
|
-
if (pollCount > maxPollTimes) {
|
|
547
|
-
reject(new Error('任务超时'));
|
|
548
|
-
return;
|
|
549
|
-
}
|
|
550
|
-
|
|
551
|
-
await task.reload();
|
|
552
|
-
const { status, output, error } = task;
|
|
553
|
-
|
|
554
|
-
if (status === 'success') {
|
|
555
|
-
resolve(output);
|
|
556
|
-
return;
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
if (status === 'failed' || status === 'canceled') {
|
|
560
|
-
reject(new Error(error || `任务${status === 'failed' ? '失败' : '取消'}`));
|
|
561
|
-
return;
|
|
562
|
-
}
|
|
563
|
-
|
|
564
|
-
// 任务未完成,等待pollInterval后继续执行
|
|
565
|
-
setTimeout(executePolling, pollInterval);
|
|
566
|
-
} catch (e) {
|
|
567
|
-
reject(e);
|
|
568
|
-
}
|
|
569
|
-
};
|
|
570
|
-
// 开始第一次轮询
|
|
571
|
-
setTimeout(executePolling, pollInterval);
|
|
572
|
-
});
|
|
573
|
-
};
|
|
574
|
-
|
|
575
|
-
const getTimeQuery = fieldValue => {
|
|
576
|
-
const { startTime, endTime } = fieldValue;
|
|
577
|
-
if (!!startTime && !!endTime) {
|
|
578
|
-
// 两个日期都有值,使用between
|
|
579
|
-
return {
|
|
580
|
-
[Op.between]: [new Date(startTime), new Date(endTime)]
|
|
581
|
-
};
|
|
582
|
-
} else if (!!startTime) {
|
|
583
|
-
// 只有开始日期,大于等于
|
|
584
|
-
return {
|
|
585
|
-
[Op.gte]: new Date(startTime)
|
|
586
|
-
};
|
|
587
|
-
} else if (!!endTime) {
|
|
588
|
-
// 只有结束日期,小于等于
|
|
589
|
-
return {
|
|
590
|
-
[Op.lte]: new Date(endTime)
|
|
591
|
-
};
|
|
592
|
-
}
|
|
593
|
-
};
|
|
594
|
-
|
|
595
|
-
const list = async ({ filter, perPage = 20, currentPage = 1, sort }) => {
|
|
596
|
-
const whereQuery = {};
|
|
597
|
-
|
|
598
|
-
['id', 'targetId', 'type', 'status', 'runnerType'].forEach(key => {
|
|
599
|
-
if (filter && filter[key]) {
|
|
600
|
-
whereQuery[key] = filter[key];
|
|
601
|
-
}
|
|
602
|
-
});
|
|
603
|
-
|
|
604
|
-
// 处理targetName模糊匹配,查询input.name字段
|
|
605
|
-
if (filter && filter.targetName) {
|
|
606
|
-
whereQuery['input.name'] = {
|
|
607
|
-
[Op.like]: `%${filter.targetName}%`
|
|
608
|
-
};
|
|
609
|
-
}
|
|
610
|
-
|
|
611
|
-
// 处理createdAt日期范围查询
|
|
612
|
-
if (filter && filter.createdAt) {
|
|
613
|
-
whereQuery.createdAt = getTimeQuery(filter.createdAt);
|
|
614
|
-
}
|
|
615
|
-
|
|
616
|
-
// 处理completedAt日期范围查询
|
|
617
|
-
if (filter && filter.completedAt) {
|
|
618
|
-
whereQuery.completedAt = getTimeQuery(filter.completedAt);
|
|
619
|
-
}
|
|
620
|
-
|
|
621
|
-
// 处理sort排序:支持多字段排序
|
|
622
|
-
let order = [['createdAt', 'DESC']];
|
|
623
|
-
|
|
624
|
-
if (sort && Object.keys(sort).length > 0) {
|
|
625
|
-
order = Object.entries(sort).map(([key, direction]) => [key, direction]);
|
|
626
|
-
}
|
|
627
|
-
|
|
628
|
-
const { rows, count } = await models.task.findAndCountAll({
|
|
629
|
-
where: Object.assign({}, whereQuery),
|
|
630
|
-
offset: perPage * (currentPage - 1),
|
|
631
|
-
limit: perPage,
|
|
632
|
-
order: order
|
|
633
|
-
});
|
|
634
|
-
|
|
635
|
-
return {
|
|
636
|
-
pageData: rows,
|
|
637
|
-
totalCount: count
|
|
638
|
-
};
|
|
639
|
-
};
|
|
640
|
-
|
|
641
|
-
const retryFunc = async ({ id }) => {
|
|
642
|
-
const task = await detail({ id });
|
|
643
|
-
if (['failed', 'canceled'].indexOf(task.status) === -1) {
|
|
644
|
-
throw new Error('只有失败或取消的任务允许重试');
|
|
645
|
-
}
|
|
646
|
-
await task.update({
|
|
647
|
-
status: 'pending',
|
|
648
|
-
completedAt: null,
|
|
649
|
-
completedUserId: null,
|
|
650
|
-
retryCount: 0,
|
|
651
|
-
error: null
|
|
652
|
-
});
|
|
653
|
-
};
|
|
654
|
-
|
|
655
|
-
const retry = async ({ id, taskIds }) => {
|
|
656
|
-
if (id) {
|
|
657
|
-
await retryFunc({ id });
|
|
658
|
-
}
|
|
659
|
-
if (taskIds && taskIds.length > 0) {
|
|
660
|
-
for (const taskId of taskIds) {
|
|
661
|
-
await retryFunc({ id: taskId });
|
|
662
|
-
}
|
|
663
|
-
}
|
|
664
|
-
};
|
|
665
|
-
|
|
666
|
-
const log = async ({ id, taskId, data, message = '' }) => {
|
|
667
|
-
const targetId = id || taskId;
|
|
668
|
-
const task = await detail({ id: targetId });
|
|
669
|
-
|
|
670
|
-
const currentOptions = task.options || {};
|
|
671
|
-
const logs = (currentOptions.logs || []).slice(0);
|
|
672
|
-
logs.splice(0, 0, {
|
|
673
|
-
data,
|
|
674
|
-
message,
|
|
675
|
-
time: new Date()
|
|
676
|
-
});
|
|
677
|
-
if (logs.length > 100) {
|
|
678
|
-
logs.splice(0, logs.length - 100);
|
|
679
|
-
}
|
|
680
|
-
await task.update({
|
|
681
|
-
options: {
|
|
682
|
-
...currentOptions,
|
|
683
|
-
logs
|
|
684
|
-
}
|
|
685
|
-
});
|
|
686
|
-
|
|
687
|
-
return task;
|
|
688
|
-
};
|
|
689
|
-
|
|
690
|
-
const logWithSignature = async ({ id, taskId, data, message = '', signature }) => {
|
|
691
|
-
const targetId = id || taskId;
|
|
692
|
-
const task = await detail({ id: targetId });
|
|
693
|
-
|
|
694
|
-
if (task.context?.secret && !verifySignature({ secret: task.context.secret, id: targetId, data: { data, message }, signature })) {
|
|
695
|
-
throw new Error('签名验证失败');
|
|
696
|
-
}
|
|
697
|
-
|
|
698
|
-
return log({ id: targetId, data, message });
|
|
699
|
-
};
|
|
700
|
-
|
|
701
|
-
const callback = async ({ id, code, data, message }) => {
|
|
702
|
-
await detail({ id });
|
|
703
|
-
|
|
704
|
-
const result = { code, data, message };
|
|
705
|
-
await log({ id, message: '回调结果', data: JSON.stringify(result) });
|
|
706
|
-
const input = Object.assign(
|
|
707
|
-
{},
|
|
708
|
-
{ id },
|
|
709
|
-
code === 0
|
|
710
|
-
? {
|
|
711
|
-
status: 'success',
|
|
712
|
-
output: data
|
|
713
|
-
}
|
|
714
|
-
: {
|
|
715
|
-
status: 'failed',
|
|
716
|
-
output: data,
|
|
717
|
-
error: message
|
|
718
|
-
}
|
|
719
|
-
);
|
|
720
|
-
await complete(input);
|
|
721
|
-
};
|
|
722
|
-
|
|
723
|
-
const callbackWithSignature = async ({ id, code, data, message, signature }) => {
|
|
724
|
-
const task = await detail({ id });
|
|
725
|
-
|
|
726
|
-
if (task.context?.secret && !verifySignature({ secret: task.context.secret, id, data: { code, data, message }, signature })) {
|
|
727
|
-
throw new Error('签名验证失败');
|
|
728
|
-
}
|
|
729
|
-
|
|
730
|
-
return callback({ id, code, data, message });
|
|
731
|
-
};
|
|
732
|
-
|
|
733
|
-
const append = async ({ dirs, tasks }) => {
|
|
734
|
-
const result = { dirs: [], tasks: [] };
|
|
735
|
-
if (Array.isArray(dirs)) {
|
|
736
|
-
for (const dir of dirs) {
|
|
737
|
-
if (!options.dirs.includes(dir)) {
|
|
738
|
-
if (!(await fs.exists(dir))) {
|
|
739
|
-
console.warn(`append 目录不存在:${dir},仍会添加但运行时可能无法匹配任务执行器`);
|
|
740
|
-
}
|
|
741
|
-
options.dirs.push(dir);
|
|
742
|
-
result.dirs.push(dir);
|
|
743
|
-
}
|
|
744
|
-
}
|
|
745
|
-
}
|
|
746
|
-
if (tasks && typeof tasks === 'object') {
|
|
747
|
-
Object.entries(tasks).forEach(([type, handler]) => {
|
|
748
|
-
if (typeof handler !== 'function') {
|
|
749
|
-
throw new Error(`任务 ${type} 的 handler 必须是一个函数`);
|
|
750
|
-
}
|
|
751
|
-
if (typeof options.task[type] !== 'function') {
|
|
752
|
-
options.task[type] = handler;
|
|
753
|
-
result.tasks.push(type);
|
|
754
|
-
}
|
|
755
|
-
});
|
|
756
|
-
}
|
|
757
|
-
return result;
|
|
758
|
-
};
|
|
759
|
-
|
|
760
|
-
Object.assign(fastify[options.name].services, {
|
|
761
|
-
create,
|
|
762
|
-
detail,
|
|
763
|
-
list,
|
|
764
|
-
complete,
|
|
765
|
-
cancel,
|
|
766
|
-
runner,
|
|
767
|
-
resetAll,
|
|
768
|
-
claimPendingTasks,
|
|
769
|
-
retry,
|
|
770
|
-
log,
|
|
771
|
-
logWithSignature,
|
|
772
|
-
callback,
|
|
773
|
-
callbackWithSignature,
|
|
774
|
-
executor,
|
|
775
|
-
processNext,
|
|
776
|
-
processSystemTask,
|
|
777
|
-
waitingComplete,
|
|
778
|
-
append
|
|
779
|
-
});
|
|
5
|
+
getTaskServiceContext(fastify, options);
|
|
6
|
+
await fastify.register(require('./executor'), options);
|
|
7
|
+
await fastify.register(require('./scheduler'), options);
|
|
8
|
+
await fastify.register(require('./task'), options);
|
|
9
|
+
await fastify.register(require('./callback'), options);
|
|
10
|
+
await fastify.register(require('./append'), options);
|
|
780
11
|
});
|