@kne/fastify-task 3.0.0-alpha.6 → 3.0.0-alpha.8
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 +841 -314
- package/index.js +47 -6
- package/libs/controllers/main.js +1 -2
- package/libs/helpers/task-service-context.js +558 -0
- package/libs/models/task.js +7 -3
- 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 -836
- package/libs/services/scheduler.js +230 -0
- package/libs/services/statistics.js +18 -27
- package/libs/services/task.js +230 -0
- package/package.json +2 -2
- package/libs/utils/cache-manager.js +0 -162
- package/libs/utils/config.js +0 -326
- package/libs/utils/errors.js +0 -191
- package/libs/utils/modules/task-executor.js +0 -196
- package/libs/utils/modules/task-processor.js +0 -185
- package/libs/utils/security-validator.js +0 -203
package/libs/services/main.js
CHANGED
|
@@ -1,840 +1,11 @@
|
|
|
1
1
|
const fp = require('fastify-plugin');
|
|
2
|
-
const
|
|
3
|
-
const SecurityValidator = require('../utils/security-validator');
|
|
4
|
-
const {
|
|
5
|
-
TaskNotFoundError,
|
|
6
|
-
TaskStatusError,
|
|
7
|
-
TaskValidationError,
|
|
8
|
-
TaskExecutionError
|
|
9
|
-
} = require('../utils/errors');
|
|
10
|
-
const {
|
|
11
|
-
CONFIG_CONSTANTS,
|
|
12
|
-
getConfigManager
|
|
13
|
-
} = require('../utils/config');
|
|
14
|
-
const {
|
|
15
|
-
getCacheManager
|
|
16
|
-
} = require('../utils/cache-manager');
|
|
17
|
-
const TaskExecutor = require('../utils/modules/task-executor');
|
|
2
|
+
const getTaskServiceContext = require('../helpers/task-service-context');
|
|
18
3
|
|
|
19
4
|
module.exports = fp(async (fastify, options) => {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
// 初始化配置管理器
|
|
27
|
-
const configManager = getConfigManager(options);
|
|
28
|
-
const config = configManager.getAll();
|
|
29
|
-
|
|
30
|
-
// 初始化缓存管理器
|
|
31
|
-
const cacheManager = getCacheManager(300000); // 5分钟缓存
|
|
32
|
-
|
|
33
|
-
// 初始化模块化组件
|
|
34
|
-
const taskExecutor = new TaskExecutor(fastify, options, models, securityValidator, cacheManager);
|
|
35
|
-
|
|
36
|
-
// 统一调用 errorHandler
|
|
37
|
-
const handleError = async ({ task, error, type }) => {
|
|
38
|
-
if (typeof options.errorHandler === 'function') {
|
|
39
|
-
try {
|
|
40
|
-
await options.errorHandler({ task, error, type });
|
|
41
|
-
} catch (handlerError) {
|
|
42
|
-
fastify.log.error(`errorHandler 执行失败: ${handlerError.message}`);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
/** 通过 @kne/fastify-statistics 采集任务完成数据 */
|
|
48
|
-
const collectTaskStatistics = async task => {
|
|
49
|
-
// 确保 task 数据是最新的(Sequelize update 后实例可能未完全同步)
|
|
50
|
-
if (task.reload && typeof task.reload === 'function') {
|
|
51
|
-
await task.reload();
|
|
52
|
-
}
|
|
53
|
-
const completedAt = task.completedAt || new Date();
|
|
54
|
-
const createdAt = task.createdAt;
|
|
55
|
-
const startedAt = task.startedAt;
|
|
56
|
-
let waitingTime = 0;
|
|
57
|
-
let executionTime = 0;
|
|
58
|
-
let totalTime = 0;
|
|
59
|
-
let hasTiming = false;
|
|
60
|
-
|
|
61
|
-
if (createdAt && completedAt) {
|
|
62
|
-
totalTime = new Date(completedAt).getTime() - new Date(createdAt).getTime();
|
|
63
|
-
if (startedAt) {
|
|
64
|
-
waitingTime = new Date(startedAt).getTime() - new Date(createdAt).getTime();
|
|
65
|
-
executionTime = new Date(completedAt).getTime() - new Date(startedAt).getTime();
|
|
66
|
-
} else {
|
|
67
|
-
executionTime = totalTime;
|
|
68
|
-
}
|
|
69
|
-
hasTiming = totalTime > 0;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
const completedHour = new Date(completedAt).getHours();
|
|
73
|
-
const channel = `${task.type}:${task.runnerType || CONFIG_CONSTANTS.RUNNER_TYPES.MANUAL}:${completedHour}`;
|
|
74
|
-
const status = task.status || 'unknown';
|
|
75
|
-
|
|
76
|
-
const data = { total: 1, [status]: 1 };
|
|
77
|
-
const unit = { total: 'count', [status]: 'count' };
|
|
78
|
-
if (hasTiming) {
|
|
79
|
-
Object.assign(data, { waitingTime, executionTime, totalTime });
|
|
80
|
-
Object.assign(unit, { waitingTime: 'ms', executionTime: 'ms', totalTime: 'ms' });
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
try {
|
|
84
|
-
await fastify[`${options.name}Statistics`].services.collect({
|
|
85
|
-
channel,
|
|
86
|
-
data,
|
|
87
|
-
unit,
|
|
88
|
-
time: completedAt
|
|
89
|
-
});
|
|
90
|
-
} catch (error) {
|
|
91
|
-
fastify.log.error(`采集任务统计数据失败: ${error.message}`, { taskId: task.id, error: error.stack });
|
|
92
|
-
// 不抛出错误,避免影响主流程
|
|
93
|
-
}
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
const create = async ({ userId, input, type, targetId, targetType, runnerType, delay = 0, scriptName, priority = 0, parentTaskId, maxRetries = 0, timeout = 60, options: currentOptions }) => {
|
|
97
|
-
try {
|
|
98
|
-
// 参数验证
|
|
99
|
-
delay = SecurityValidator.validateNumber(delay, {
|
|
100
|
-
min: CONFIG_CONSTANTS.DELAY_MIN,
|
|
101
|
-
max: CONFIG_CONSTANTS.DELAY_MAX,
|
|
102
|
-
integer: true,
|
|
103
|
-
required: false
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
priority = SecurityValidator.validateNumber(priority, {
|
|
107
|
-
min: CONFIG_CONSTANTS.PRIORITY_MIN,
|
|
108
|
-
max: CONFIG_CONSTANTS.PRIORITY_MAX,
|
|
109
|
-
integer: true
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
maxRetries = SecurityValidator.validateNumber(maxRetries, {
|
|
113
|
-
min: 0,
|
|
114
|
-
max: CONFIG_CONSTANTS.RETRY_MAX_COUNT,
|
|
115
|
-
integer: true
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
timeout = SecurityValidator.validateNumber(timeout, {
|
|
119
|
-
min: CONFIG_CONSTANTS.TASK_TIMEOUT_MIN,
|
|
120
|
-
max: CONFIG_CONSTANTS.TASK_TIMEOUT_MAX / (60 * 1000), // 转换为分钟
|
|
121
|
-
integer: true
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
// 任务类型验证
|
|
125
|
-
SecurityValidator.validateTaskType(type, Object.keys(options.task || {}));
|
|
126
|
-
|
|
127
|
-
// 检查任务处理器是否存在
|
|
128
|
-
if (typeof options.task[type] !== 'function') {
|
|
129
|
-
throw new TaskValidationError('type', type, '未找到合法的任务声明');
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
const task = await models.task.create({
|
|
133
|
-
userId,
|
|
134
|
-
input,
|
|
135
|
-
type,
|
|
136
|
-
targetId,
|
|
137
|
-
targetType,
|
|
138
|
-
runnerType,
|
|
139
|
-
priority,
|
|
140
|
-
parentTaskId,
|
|
141
|
-
maxRetries,
|
|
142
|
-
timeout,
|
|
143
|
-
startTime: delay > 0 ? new Date(Date.now() + 1000 * delay) : new Date(),
|
|
144
|
-
scriptName,
|
|
145
|
-
options: currentOptions
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
return task;
|
|
149
|
-
} catch (error) {
|
|
150
|
-
if (error instanceof TaskValidationError) {
|
|
151
|
-
throw error;
|
|
152
|
-
}
|
|
153
|
-
throw new TaskExecutionError(null, error, { operation: 'create' });
|
|
154
|
-
}
|
|
155
|
-
};
|
|
156
|
-
|
|
157
|
-
const resetAll = async () => {
|
|
158
|
-
const runningTasks = await models.task.findAll({
|
|
159
|
-
where: { status: 'running' },
|
|
160
|
-
attributes: ['type', 'runnerType'],
|
|
161
|
-
raw: true
|
|
162
|
-
});
|
|
163
|
-
await models.task.update(
|
|
164
|
-
{ status: 'pending' },
|
|
165
|
-
{ where: { status: 'running' } }
|
|
166
|
-
);
|
|
167
|
-
};
|
|
168
|
-
|
|
169
|
-
const executor = async ({ type, scriptName, ...props }) => {
|
|
170
|
-
return await taskExecutor.execute({ type, scriptName, ...props });
|
|
171
|
-
};
|
|
172
|
-
|
|
173
|
-
const processNext = async ({ id, signature, result: resultStr }) => {
|
|
174
|
-
try {
|
|
175
|
-
const task = await detail({ id });
|
|
176
|
-
|
|
177
|
-
if (task.status !== CONFIG_CONSTANTS.TASK_STATUSES.WAITING) {
|
|
178
|
-
throw new TaskStatusError(id, task.status, CONFIG_CONSTANTS.TASK_STATUSES.WAITING);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
// 签名验证
|
|
182
|
-
if (task.context?.secret && config.security.enableSignature) {
|
|
183
|
-
if (!SecurityValidator.verifySignature({
|
|
184
|
-
secret: task.context.secret,
|
|
185
|
-
id,
|
|
186
|
-
data: resultStr,
|
|
187
|
-
signature
|
|
188
|
-
})) {
|
|
189
|
-
throw new TaskValidationError('signature', signature, '签名验证失败');
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
const result = SecurityValidator.safeJSONParse(resultStr);
|
|
194
|
-
if (!result || typeof result !== 'object') {
|
|
195
|
-
throw new TaskValidationError('result', resultStr, '结果必须是有效的JSON对象');
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
if (result.code !== 0) {
|
|
199
|
-
await task.update({
|
|
200
|
-
status: CONFIG_CONSTANTS.TASK_STATUSES.FAILED,
|
|
201
|
-
error: result,
|
|
202
|
-
completedAt: new Date()
|
|
203
|
-
});
|
|
204
|
-
collectTaskStatistics(task);
|
|
205
|
-
await handleError({ task, error: result, type: 'callback' });
|
|
206
|
-
return;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
await config.task[task.type]({ task, result: result.data, context: task.context });
|
|
210
|
-
await task.update({
|
|
211
|
-
status: CONFIG_CONSTANTS.TASK_STATUSES.SUCCESS,
|
|
212
|
-
output: result,
|
|
213
|
-
progress: 100,
|
|
214
|
-
completedAt: new Date()
|
|
215
|
-
});
|
|
216
|
-
collectTaskStatistics(task);
|
|
217
|
-
} catch (error) {
|
|
218
|
-
if (error instanceof TaskStatusError || error instanceof TaskValidationError) {
|
|
219
|
-
throw error;
|
|
220
|
-
}
|
|
221
|
-
throw new TaskExecutionError(id, error, { operation: 'processNext' });
|
|
222
|
-
}
|
|
223
|
-
};
|
|
224
|
-
|
|
225
|
-
const processSystemTask = async task => {
|
|
226
|
-
await task.update({
|
|
227
|
-
status: 'running',
|
|
228
|
-
startedAt: new Date(),
|
|
229
|
-
progress: 0,
|
|
230
|
-
error: null,
|
|
231
|
-
output: null,
|
|
232
|
-
pollCount: 0,
|
|
233
|
-
pollResults: [],
|
|
234
|
-
completedAt: null,
|
|
235
|
-
context: {}
|
|
236
|
-
});
|
|
237
|
-
const addLog = props => {
|
|
238
|
-
return log(Object.assign({}, props, { taskId: task.id }));
|
|
239
|
-
};
|
|
240
|
-
|
|
241
|
-
// #3: 任务超时自动失败
|
|
242
|
-
const createTimeout = ms => new Promise((_, reject) =>
|
|
243
|
-
setTimeout(() => reject(new Error(`任务执行超时(${ms}ms)`)), ms)
|
|
244
|
-
);
|
|
245
|
-
|
|
246
|
-
const executorPromise = executor({ type: task.type, scriptName: task.scriptName, task, log: addLog });
|
|
247
|
-
const racePromise = options.taskTimeout > 0
|
|
248
|
-
? Promise.race([executorPromise, createTimeout(options.taskTimeout)])
|
|
249
|
-
: executorPromise;
|
|
250
|
-
|
|
251
|
-
return racePromise
|
|
252
|
-
.then(async result => {
|
|
253
|
-
if (result === false) {
|
|
254
|
-
return;
|
|
255
|
-
}
|
|
256
|
-
await options.task[task.type]({ task, result, context: task.context });
|
|
257
|
-
await task.update({
|
|
258
|
-
status: 'success',
|
|
259
|
-
output: result,
|
|
260
|
-
progress: 100,
|
|
261
|
-
completedAt: new Date()
|
|
262
|
-
});
|
|
263
|
-
collectTaskStatistics(task);
|
|
264
|
-
// #2: 任务依赖/链式执行 — 父任务成功后激活子任务
|
|
265
|
-
await triggerChildTasks(task);
|
|
266
|
-
})
|
|
267
|
-
.catch(async e => {
|
|
268
|
-
// #4: 重试策略 — 判断是否自动重试
|
|
269
|
-
const currentRetryCount = (task.retryCount || 0) + 1;
|
|
270
|
-
const maxRetries = task.maxRetries || 0;
|
|
271
|
-
if (currentRetryCount < maxRetries) {
|
|
272
|
-
// 指数退避:baseDelay * 2^retryCount
|
|
273
|
-
const baseDelay = options.retryBaseDelay || 5000;
|
|
274
|
-
const backoffDelay = baseDelay * Math.pow(2, currentRetryCount - 1);
|
|
275
|
-
await task.update({
|
|
276
|
-
status: 'pending',
|
|
277
|
-
retryCount: currentRetryCount,
|
|
278
|
-
error: (e.stack || '').replaceAll(process.cwd(), '/server'),
|
|
279
|
-
startTime: new Date(Date.now() + backoffDelay),
|
|
280
|
-
completedAt: null
|
|
281
|
-
});
|
|
282
|
-
fastify.log.info(`任务 ${task.id} 第 ${currentRetryCount} 次重试,${backoffDelay}ms 后执行`);
|
|
283
|
-
} else {
|
|
284
|
-
await task.update({
|
|
285
|
-
status: 'failed',
|
|
286
|
-
error: (e.stack || '').replaceAll(process.cwd(), '/server'),
|
|
287
|
-
completedAt: new Date(),
|
|
288
|
-
retryCount: currentRetryCount
|
|
289
|
-
});
|
|
290
|
-
collectTaskStatistics(task);
|
|
291
|
-
await handleError({ task, error: e.message || e, type: currentRetryCount > 1 ? 'retry_exhausted' : 'execution' });
|
|
292
|
-
}
|
|
293
|
-
});
|
|
294
|
-
};
|
|
295
|
-
|
|
296
|
-
// #2: 触发子任务(父任务成功后激活 pending 的子任务)
|
|
297
|
-
const triggerChildTasks = async parentTask => {
|
|
298
|
-
const childTasks = await models.task.findAll({
|
|
299
|
-
where: {
|
|
300
|
-
parentTaskId: parentTask.id,
|
|
301
|
-
status: 'pending'
|
|
302
|
-
}
|
|
303
|
-
});
|
|
304
|
-
if (childTasks.length === 0) return;
|
|
305
|
-
|
|
306
|
-
fastify.log.info(`父任务 ${parentTask.id} 完成,激活 ${childTasks.length} 个子任务`);
|
|
307
|
-
for (const child of childTasks) {
|
|
308
|
-
if (child.runnerType === 'system') {
|
|
309
|
-
await processSystemTask(child);
|
|
310
|
-
}
|
|
311
|
-
// manual 类型子任务保持 pending,等待手动执行
|
|
312
|
-
}
|
|
313
|
-
};
|
|
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
|
-
await handleError({ task, error: task.error, type: 'timeout' });
|
|
345
|
-
}
|
|
346
|
-
};
|
|
347
|
-
|
|
348
|
-
const runner = async () => {
|
|
349
|
-
// 检查超时任务
|
|
350
|
-
await checkTimeout();
|
|
351
|
-
|
|
352
|
-
// 批量查询运行中和待处理的任务,减少数据库往返
|
|
353
|
-
const [runningTasks, pendingTasks] = await Promise.all([
|
|
354
|
-
models.task.count({
|
|
355
|
-
where: {
|
|
356
|
-
runnerType: CONFIG_CONSTANTS.RUNNER_TYPES.SYSTEM,
|
|
357
|
-
status: CONFIG_CONSTANTS.TASK_STATUSES.RUNNING
|
|
358
|
-
}
|
|
359
|
-
}),
|
|
360
|
-
models.task.findAll({
|
|
361
|
-
where: {
|
|
362
|
-
runnerType: CONFIG_CONSTANTS.RUNNER_TYPES.SYSTEM,
|
|
363
|
-
status: CONFIG_CONSTANTS.TASK_STATUSES.PENDING,
|
|
364
|
-
startTime: {
|
|
365
|
-
[Op.lte]: new Date()
|
|
366
|
-
}
|
|
367
|
-
},
|
|
368
|
-
order: [
|
|
369
|
-
['priority', 'DESC'],
|
|
370
|
-
['startTime', 'ASC']
|
|
371
|
-
],
|
|
372
|
-
limit: config.limit
|
|
373
|
-
})
|
|
374
|
-
]);
|
|
375
|
-
|
|
376
|
-
const runningTaskCount = runningTasks;
|
|
377
|
-
const limit = config.limit - runningTaskCount;
|
|
378
|
-
|
|
379
|
-
if (limit <= 0) {
|
|
380
|
-
fastify.log.info(`当前运行中的系统任务数(${runningTaskCount})已达上限(${config.limit}),暂不执行新任务`);
|
|
381
|
-
return;
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
// 取出可以执行的任务
|
|
385
|
-
const tasksToExecute = pendingTasks.slice(0, limit);
|
|
386
|
-
|
|
387
|
-
if (tasksToExecute.length > 0) {
|
|
388
|
-
fastify.log.info(`本轮执行 ${tasksToExecute.length} 个待处理的系统任务`);
|
|
389
|
-
|
|
390
|
-
// 批量更新任务状态为 running,防止重复执行
|
|
391
|
-
const taskIds = tasksToExecute.map(task => task.id);
|
|
392
|
-
await models.task.update(
|
|
393
|
-
{ status: CONFIG_CONSTANTS.TASK_STATUSES.RUNNING, startedAt: new Date() },
|
|
394
|
-
{ where: { id: { [Op.in]: taskIds } } }
|
|
395
|
-
);
|
|
396
|
-
|
|
397
|
-
// 清除相关缓存
|
|
398
|
-
taskIds.forEach(id => cacheManager.delete(`task:${id}`));
|
|
399
|
-
|
|
400
|
-
// 并行执行任务
|
|
401
|
-
await Promise.all(tasksToExecute.map(async task => {
|
|
402
|
-
try {
|
|
403
|
-
await processSystemTask(task);
|
|
404
|
-
} catch (error) {
|
|
405
|
-
fastify.log.error(`任务执行失败: ${task.id}`, { error: error.message });
|
|
406
|
-
}
|
|
407
|
-
}));
|
|
408
|
-
}
|
|
409
|
-
};
|
|
410
|
-
|
|
411
|
-
const detail = async ({ id }) => {
|
|
412
|
-
if (!id) {
|
|
413
|
-
throw new TaskValidationError('id', id, '任务ID不能为空');
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
// 使用缓存优化查询
|
|
417
|
-
const cacheKey = `task:${id}`;
|
|
418
|
-
const cachedTask = cacheManager.get(cacheKey);
|
|
419
|
-
|
|
420
|
-
if (cachedTask) {
|
|
421
|
-
return cachedTask;
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
const task = await models.task.findByPk(id);
|
|
425
|
-
if (!task) {
|
|
426
|
-
throw new TaskNotFoundError(id);
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
// 缓存任务数据(短时间缓存,因为状态会变化)
|
|
430
|
-
cacheManager.set(cacheKey, task, 60000); // 1分钟缓存
|
|
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
|
-
await handleError({ task, error: e.message || e, type: 'execution' });
|
|
504
|
-
throw e;
|
|
505
|
-
}
|
|
506
|
-
} else {
|
|
507
|
-
await task.update(
|
|
508
|
-
Object.assign({}, props, {
|
|
509
|
-
status: 'failed',
|
|
510
|
-
completedAt: new Date(),
|
|
511
|
-
startedAt: task.startedAt || task.createdAt,
|
|
512
|
-
completedUserId: userId
|
|
513
|
-
})
|
|
514
|
-
);
|
|
515
|
-
collectTaskStatistics(task);
|
|
516
|
-
await handleError({ task, error: props.error || '任务标记为失败', type: 'callback' });
|
|
517
|
-
}
|
|
518
|
-
};
|
|
519
|
-
|
|
520
|
-
const waitingComplete = async ({ id, pollInterval = 1000, maxPollTimes = 20 }) => {
|
|
521
|
-
let task = await detail({ id });
|
|
522
|
-
|
|
523
|
-
if (task.status === 'pending') {
|
|
524
|
-
// 标记为最高优先级,确保立即执行(即使因重试等重新入队也会被优先处理)
|
|
525
|
-
await task.update({ priority: Number.MAX_SAFE_INTEGER });
|
|
526
|
-
await processSystemTask(task);
|
|
527
|
-
// processSystemTask 完成后重新加载任务状态
|
|
528
|
-
await task.reload();
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
// 立即检查是否已到达终态
|
|
532
|
-
if (task.status === 'success') {
|
|
533
|
-
return task.output;
|
|
534
|
-
}
|
|
535
|
-
if (task.status === 'failed') {
|
|
536
|
-
throw new Error(task.error || '任务失败');
|
|
537
|
-
}
|
|
538
|
-
if (task.status === 'canceled') {
|
|
539
|
-
throw new Error(task.error || '任务取消');
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
// 任务仍在运行中,开始轮询
|
|
543
|
-
let pollCount = 0;
|
|
544
|
-
return await new Promise((resolve, reject) => {
|
|
545
|
-
const executePolling = async () => {
|
|
546
|
-
try {
|
|
547
|
-
pollCount++;
|
|
548
|
-
if (pollCount > maxPollTimes) {
|
|
549
|
-
reject(new Error('任务超时'));
|
|
550
|
-
return;
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
await task.reload();
|
|
554
|
-
const { status, output, error } = task;
|
|
555
|
-
|
|
556
|
-
if (status === 'success') {
|
|
557
|
-
resolve(output);
|
|
558
|
-
return;
|
|
559
|
-
}
|
|
560
|
-
|
|
561
|
-
if (status === 'failed' || status === 'canceled') {
|
|
562
|
-
reject(new Error(error || `任务${status === 'failed' ? '失败' : '取消'}`));
|
|
563
|
-
return;
|
|
564
|
-
}
|
|
565
|
-
|
|
566
|
-
// 任务未完成,等待pollInterval后继续执行
|
|
567
|
-
setTimeout(executePolling, pollInterval);
|
|
568
|
-
} catch (e) {
|
|
569
|
-
reject(e);
|
|
570
|
-
}
|
|
571
|
-
};
|
|
572
|
-
// 开始第一次轮询
|
|
573
|
-
setTimeout(executePolling, pollInterval);
|
|
574
|
-
});
|
|
575
|
-
};
|
|
576
|
-
|
|
577
|
-
const getTimeQuery = fieldValue => {
|
|
578
|
-
const { startTime, endTime } = fieldValue;
|
|
579
|
-
if (!!startTime && !!endTime) {
|
|
580
|
-
// 两个日期都有值,使用between
|
|
581
|
-
return {
|
|
582
|
-
[Op.between]: [new Date(startTime), new Date(endTime)]
|
|
583
|
-
};
|
|
584
|
-
} else if (!!startTime) {
|
|
585
|
-
// 只有开始日期,大于等于
|
|
586
|
-
return {
|
|
587
|
-
[Op.gte]: new Date(startTime)
|
|
588
|
-
};
|
|
589
|
-
} else if (!!endTime) {
|
|
590
|
-
// 只有结束日期,小于等于
|
|
591
|
-
return {
|
|
592
|
-
[Op.lte]: new Date(endTime)
|
|
593
|
-
};
|
|
594
|
-
}
|
|
595
|
-
};
|
|
596
|
-
|
|
597
|
-
const list = async ({ filter, perPage = 20, currentPage = 1, sort }) => {
|
|
598
|
-
const whereQuery = {};
|
|
599
|
-
|
|
600
|
-
['id', 'targetId', 'type', 'status', 'runnerType'].forEach(key => {
|
|
601
|
-
if (filter && filter[key]) {
|
|
602
|
-
whereQuery[key] = filter[key];
|
|
603
|
-
}
|
|
604
|
-
});
|
|
605
|
-
|
|
606
|
-
// 处理targetName模糊匹配,查询input.name字段
|
|
607
|
-
if (filter && filter.targetName) {
|
|
608
|
-
whereQuery['input.name'] = {
|
|
609
|
-
[Op.like]: `%${filter.targetName}%`
|
|
610
|
-
};
|
|
611
|
-
}
|
|
612
|
-
|
|
613
|
-
// 处理createdAt日期范围查询
|
|
614
|
-
if (filter && filter.createdAt) {
|
|
615
|
-
whereQuery.createdAt = getTimeQuery(filter.createdAt);
|
|
616
|
-
}
|
|
617
|
-
|
|
618
|
-
// 处理completedAt日期范围查询
|
|
619
|
-
if (filter && filter.completedAt) {
|
|
620
|
-
whereQuery.completedAt = getTimeQuery(filter.completedAt);
|
|
621
|
-
}
|
|
622
|
-
|
|
623
|
-
// 处理sort排序:支持多字段排序
|
|
624
|
-
let order = [['createdAt', 'DESC']];
|
|
625
|
-
|
|
626
|
-
if (sort && Object.keys(sort).length > 0) {
|
|
627
|
-
order = Object.entries(sort).map(([key, direction]) => [key, direction]);
|
|
628
|
-
}
|
|
629
|
-
|
|
630
|
-
const { rows, count } = await models.task.findAndCountAll({
|
|
631
|
-
where: Object.assign({}, whereQuery),
|
|
632
|
-
offset: perPage * (currentPage - 1),
|
|
633
|
-
limit: perPage,
|
|
634
|
-
order: order
|
|
635
|
-
});
|
|
636
|
-
|
|
637
|
-
return {
|
|
638
|
-
pageData: rows,
|
|
639
|
-
totalCount: count
|
|
640
|
-
};
|
|
641
|
-
};
|
|
642
|
-
|
|
643
|
-
const retryFunc = async ({ id }) => {
|
|
644
|
-
const task = await detail({ id });
|
|
645
|
-
if (['failed', 'canceled'].indexOf(task.status) === -1) {
|
|
646
|
-
throw new Error('只有失败或取消的任务允许重试');
|
|
647
|
-
}
|
|
648
|
-
await task.update({
|
|
649
|
-
status: 'pending',
|
|
650
|
-
completedAt: null,
|
|
651
|
-
completedUserId: null,
|
|
652
|
-
retryCount: 0,
|
|
653
|
-
error: null
|
|
654
|
-
});
|
|
655
|
-
};
|
|
656
|
-
|
|
657
|
-
const retry = async ({ id, taskIds }) => {
|
|
658
|
-
if (id) {
|
|
659
|
-
await retryFunc({ id });
|
|
660
|
-
}
|
|
661
|
-
if (taskIds && taskIds.length > 0) {
|
|
662
|
-
for (const taskId of taskIds) {
|
|
663
|
-
await retryFunc({ id: taskId });
|
|
664
|
-
}
|
|
665
|
-
}
|
|
666
|
-
};
|
|
667
|
-
|
|
668
|
-
const log = async ({ id, taskId, data, message = '' }) => {
|
|
669
|
-
try {
|
|
670
|
-
const targetId = id || taskId;
|
|
671
|
-
const task = await detail({ id: targetId });
|
|
672
|
-
|
|
673
|
-
const currentOptions = task.options || {};
|
|
674
|
-
const logs = currentOptions.logs || [];
|
|
675
|
-
|
|
676
|
-
// 性能优化:只在日志超过限制时才进行截断
|
|
677
|
-
const newLog = { data, message, time: new Date() };
|
|
678
|
-
let limitedLogs;
|
|
679
|
-
|
|
680
|
-
if (logs.length >= CONFIG_CONSTANTS.LOGS_MAX_SIZE) {
|
|
681
|
-
// 使用更高效的数组操作
|
|
682
|
-
limitedLogs = [...logs.slice(-CONFIG_CONSTANTS.LOGS_MAX_SIZE + 1), newLog];
|
|
683
|
-
} else {
|
|
684
|
-
limitedLogs = [...logs, newLog];
|
|
685
|
-
}
|
|
686
|
-
|
|
687
|
-
await task.update({
|
|
688
|
-
options: {
|
|
689
|
-
...currentOptions,
|
|
690
|
-
logs: limitedLogs
|
|
691
|
-
}
|
|
692
|
-
});
|
|
693
|
-
|
|
694
|
-
// 更新缓存
|
|
695
|
-
cacheManager.set(`task:${targetId}`, task, 60000);
|
|
696
|
-
|
|
697
|
-
return task;
|
|
698
|
-
} catch (error) {
|
|
699
|
-
if (error instanceof TaskNotFoundError || error instanceof TaskValidationError) {
|
|
700
|
-
throw error;
|
|
701
|
-
}
|
|
702
|
-
throw new TaskExecutionError(id || taskId, error, { operation: 'log' });
|
|
703
|
-
}
|
|
704
|
-
};
|
|
705
|
-
|
|
706
|
-
const logWithSignature = async ({ id, taskId, data, message = '', signature }) => {
|
|
707
|
-
try {
|
|
708
|
-
const targetId = id || taskId;
|
|
709
|
-
const task = await detail({ id: targetId });
|
|
710
|
-
|
|
711
|
-
// 签名验证
|
|
712
|
-
if (task.context?.secret && config.security.enableSignature) {
|
|
713
|
-
if (!SecurityValidator.verifySignature({
|
|
714
|
-
secret: task.context.secret,
|
|
715
|
-
id: targetId,
|
|
716
|
-
data: { data, message },
|
|
717
|
-
signature
|
|
718
|
-
})) {
|
|
719
|
-
throw new TaskValidationError('signature', signature, '签名验证失败');
|
|
720
|
-
}
|
|
721
|
-
}
|
|
722
|
-
|
|
723
|
-
return log({ id: targetId, data, message });
|
|
724
|
-
} catch (error) {
|
|
725
|
-
if (error instanceof TaskValidationError) {
|
|
726
|
-
throw error;
|
|
727
|
-
}
|
|
728
|
-
throw new TaskExecutionError(id || taskId, error, { operation: 'logWithSignature' });
|
|
729
|
-
}
|
|
730
|
-
};
|
|
731
|
-
|
|
732
|
-
const callback = async ({ id, code, data, message }) => {
|
|
733
|
-
try {
|
|
734
|
-
// 参数验证
|
|
735
|
-
const validatedCode = SecurityValidator.validateNumber(code, {
|
|
736
|
-
min: 0,
|
|
737
|
-
max: Number.MAX_SAFE_INTEGER,
|
|
738
|
-
integer: true
|
|
739
|
-
});
|
|
740
|
-
|
|
741
|
-
await detail({ id });
|
|
742
|
-
|
|
743
|
-
const result = { code: validatedCode, data, message };
|
|
744
|
-
await log({ id, message: '回调结果', data: JSON.stringify(result) });
|
|
745
|
-
|
|
746
|
-
const input = Object.assign(
|
|
747
|
-
{},
|
|
748
|
-
{ id },
|
|
749
|
-
validatedCode === 0
|
|
750
|
-
? {
|
|
751
|
-
status: CONFIG_CONSTANTS.TASK_STATUSES.SUCCESS,
|
|
752
|
-
output: data
|
|
753
|
-
}
|
|
754
|
-
: {
|
|
755
|
-
status: CONFIG_CONSTANTS.TASK_STATUSES.FAILED,
|
|
756
|
-
output: data,
|
|
757
|
-
error: message
|
|
758
|
-
}
|
|
759
|
-
);
|
|
760
|
-
await complete(input);
|
|
761
|
-
} catch (error) {
|
|
762
|
-
if (error instanceof TaskNotFoundError || error instanceof TaskValidationError) {
|
|
763
|
-
throw error;
|
|
764
|
-
}
|
|
765
|
-
throw new TaskExecutionError(id, error, { operation: 'callback' });
|
|
766
|
-
}
|
|
767
|
-
};
|
|
768
|
-
|
|
769
|
-
const callbackWithSignature = async ({ id, code, data, message, signature }) => {
|
|
770
|
-
try {
|
|
771
|
-
const task = await detail({ id });
|
|
772
|
-
|
|
773
|
-
// 签名验证
|
|
774
|
-
if (task.context?.secret && config.security.enableSignature) {
|
|
775
|
-
if (!SecurityValidator.verifySignature({
|
|
776
|
-
secret: task.context.secret,
|
|
777
|
-
id,
|
|
778
|
-
data: { code, data, message },
|
|
779
|
-
signature
|
|
780
|
-
})) {
|
|
781
|
-
throw new TaskValidationError('signature', signature, '签名验证失败');
|
|
782
|
-
}
|
|
783
|
-
}
|
|
784
|
-
|
|
785
|
-
return callback({ id, code, data, message });
|
|
786
|
-
} catch (error) {
|
|
787
|
-
if (error instanceof TaskValidationError) {
|
|
788
|
-
throw error;
|
|
789
|
-
}
|
|
790
|
-
throw new TaskExecutionError(id, error, { operation: 'callbackWithSignature' });
|
|
791
|
-
}
|
|
792
|
-
};
|
|
793
|
-
|
|
794
|
-
const append = async ({ dirs, tasks }) => {
|
|
795
|
-
const result = { dirs: [], tasks: [] };
|
|
796
|
-
if (Array.isArray(dirs)) {
|
|
797
|
-
for (const dir of dirs) {
|
|
798
|
-
if (!options.dirs.includes(dir)) {
|
|
799
|
-
if (!(await fs.exists(dir))) {
|
|
800
|
-
console.warn(`append 目录不存在:${dir},仍会添加但运行时可能无法匹配任务执行器`);
|
|
801
|
-
}
|
|
802
|
-
options.dirs.push(dir);
|
|
803
|
-
result.dirs.push(dir);
|
|
804
|
-
}
|
|
805
|
-
}
|
|
806
|
-
}
|
|
807
|
-
if (tasks && typeof tasks === 'object') {
|
|
808
|
-
Object.entries(tasks).forEach(([type, handler]) => {
|
|
809
|
-
if (typeof handler !== 'function') {
|
|
810
|
-
throw new Error(`任务 ${type} 的 handler 必须是一个函数`);
|
|
811
|
-
}
|
|
812
|
-
if (typeof options.task[type] !== 'function') {
|
|
813
|
-
options.task[type] = handler;
|
|
814
|
-
result.tasks.push(type);
|
|
815
|
-
}
|
|
816
|
-
});
|
|
817
|
-
}
|
|
818
|
-
return result;
|
|
819
|
-
};
|
|
820
|
-
|
|
821
|
-
Object.assign(fastify[options.name].services, {
|
|
822
|
-
create,
|
|
823
|
-
detail,
|
|
824
|
-
list,
|
|
825
|
-
complete,
|
|
826
|
-
cancel,
|
|
827
|
-
runner,
|
|
828
|
-
resetAll,
|
|
829
|
-
retry,
|
|
830
|
-
log,
|
|
831
|
-
logWithSignature,
|
|
832
|
-
callback,
|
|
833
|
-
callbackWithSignature,
|
|
834
|
-
executor,
|
|
835
|
-
processNext,
|
|
836
|
-
processSystemTask,
|
|
837
|
-
waitingComplete,
|
|
838
|
-
append
|
|
839
|
-
});
|
|
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);
|
|
840
11
|
});
|