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