@kne/fastify-task 3.0.0-alpha.4 → 3.0.0-alpha.6
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/index.js +4 -35
- package/libs/controllers/main.js +1 -0
- package/libs/services/main.js +329 -226
- package/libs/services/statistics.js +73 -44
- package/libs/utils/cache-manager.js +162 -0
- package/libs/utils/config.js +326 -0
- package/libs/utils/errors.js +191 -0
- package/libs/utils/modules/task-executor.js +196 -0
- package/libs/utils/modules/task-processor.js +185 -0
- package/libs/utils/security-validator.js +203 -0
- package/package.json +3 -2
package/libs/services/main.js
CHANGED
|
@@ -1,12 +1,49 @@
|
|
|
1
1
|
const fp = require('fastify-plugin');
|
|
2
|
-
const path = require('node:path');
|
|
3
2
|
const fs = require('fs-extra');
|
|
4
|
-
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');
|
|
5
18
|
|
|
6
19
|
module.exports = fp(async (fastify, options) => {
|
|
7
20
|
const { models } = fastify[options.name];
|
|
8
21
|
const { Op } = fastify.sequelize.Sequelize;
|
|
9
22
|
|
|
23
|
+
// 确保模块能够正确访问 SecurityValidator
|
|
24
|
+
const securityValidator = SecurityValidator;
|
|
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
|
+
|
|
10
47
|
/** 通过 @kne/fastify-statistics 采集任务完成数据 */
|
|
11
48
|
const collectTaskStatistics = async task => {
|
|
12
49
|
// 确保 task 数据是最新的(Sequelize update 后实例可能未完全同步)
|
|
@@ -33,7 +70,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
33
70
|
}
|
|
34
71
|
|
|
35
72
|
const completedHour = new Date(completedAt).getHours();
|
|
36
|
-
const channel = `${task.type}:${task.runnerType ||
|
|
73
|
+
const channel = `${task.type}:${task.runnerType || CONFIG_CONSTANTS.RUNNER_TYPES.MANUAL}:${completedHour}`;
|
|
37
74
|
const status = task.status || 'unknown';
|
|
38
75
|
|
|
39
76
|
const data = { total: 1, [status]: 1 };
|
|
@@ -42,63 +79,79 @@ module.exports = fp(async (fastify, options) => {
|
|
|
42
79
|
Object.assign(data, { waitingTime, executionTime, totalTime });
|
|
43
80
|
Object.assign(unit, { waitingTime: 'ms', executionTime: 'ms', totalTime: 'ms' });
|
|
44
81
|
}
|
|
45
|
-
|
|
46
|
-
|
|
82
|
+
|
|
83
|
+
try {
|
|
84
|
+
await fastify[`${options.name}Statistics`].services.collect({
|
|
47
85
|
channel,
|
|
48
86
|
data,
|
|
49
87
|
unit,
|
|
50
88
|
time: completedAt
|
|
51
|
-
})
|
|
52
|
-
.catch(e => {
|
|
53
|
-
fastify.log.error(`采集任务统计数据失败: ${e.message}`);
|
|
54
89
|
});
|
|
90
|
+
} catch (error) {
|
|
91
|
+
fastify.log.error(`采集任务统计数据失败: ${error.message}`, { taskId: task.id, error: error.stack });
|
|
92
|
+
// 不抛出错误,避免影响主流程
|
|
93
|
+
}
|
|
55
94
|
};
|
|
56
95
|
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
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
|
+
});
|
|
63
105
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
106
|
+
priority = SecurityValidator.validateNumber(priority, {
|
|
107
|
+
min: CONFIG_CONSTANTS.PRIORITY_MIN,
|
|
108
|
+
max: CONFIG_CONSTANTS.PRIORITY_MAX,
|
|
109
|
+
integer: true
|
|
110
|
+
});
|
|
69
111
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
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
|
+
}
|
|
102
155
|
};
|
|
103
156
|
|
|
104
157
|
const resetAll = async () => {
|
|
@@ -114,113 +167,59 @@ module.exports = fp(async (fastify, options) => {
|
|
|
114
167
|
};
|
|
115
168
|
|
|
116
169
|
const executor = async ({ type, scriptName, ...props }) => {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
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);
|
|
124
179
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
(await props.task.update({
|
|
136
|
-
progress: progress
|
|
137
|
-
}));
|
|
138
|
-
},
|
|
139
|
-
polling: async (callback, currentOptions) => {
|
|
140
|
-
let pollCount = 0;
|
|
141
|
-
const maxPollTimes = currentOptions?.maxPollTimes || options.maxPollTimes;
|
|
142
|
-
const pollInterval = currentOptions?.pollInterval || options.pollInterval;
|
|
143
|
-
return await new Promise((resolve, reject) => {
|
|
144
|
-
const executePolling = async () => {
|
|
145
|
-
try {
|
|
146
|
-
pollCount++;
|
|
147
|
-
if (pollCount > maxPollTimes) {
|
|
148
|
-
reject(new Error(`轮询超时(${maxPollTimes}次),任务未完成`));
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
const pollingResult = Object.assign({}, await callback());
|
|
153
|
-
const { result, data, message, progress } = pollingResult;
|
|
154
|
-
|
|
155
|
-
if (typeof props?.task?.update === 'function') {
|
|
156
|
-
await props.task.reload();
|
|
157
|
-
await props.task.update(
|
|
158
|
-
Object.assign(
|
|
159
|
-
{},
|
|
160
|
-
{
|
|
161
|
-
pollCount: props.task.pollCount + 1,
|
|
162
|
-
pollResults: [...props.task.pollResults, Object.assign({}, pollingResult, { time: new Date() })]
|
|
163
|
-
},
|
|
164
|
-
Number.isInteger(progress) ? { progress } : {}
|
|
165
|
-
)
|
|
166
|
-
);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
if (result === 'failed') {
|
|
170
|
-
reject(new Error(`任务处理失败:${message}`));
|
|
171
|
-
return;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
if (result === 'success') {
|
|
175
|
-
resolve(data);
|
|
176
|
-
return;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
// 任务未完成,等待pollInterval后继续执行
|
|
180
|
-
setTimeout(executePolling, pollInterval);
|
|
181
|
-
} catch (e) {
|
|
182
|
-
reject(e);
|
|
183
|
-
}
|
|
184
|
-
};
|
|
185
|
-
// 开始第一次轮询
|
|
186
|
-
setTimeout(executePolling, pollInterval);
|
|
187
|
-
});
|
|
188
|
-
},
|
|
189
|
-
next: async context => {
|
|
190
|
-
if (typeof props?.task?.update === 'function') {
|
|
191
|
-
await props.task.update({ context, status: 'waiting' });
|
|
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, '签名验证失败');
|
|
192
190
|
}
|
|
193
|
-
return false;
|
|
194
191
|
}
|
|
195
|
-
});
|
|
196
|
-
};
|
|
197
192
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
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 });
|
|
208
210
|
await task.update({
|
|
209
|
-
status:
|
|
210
|
-
|
|
211
|
+
status: CONFIG_CONSTANTS.TASK_STATUSES.SUCCESS,
|
|
212
|
+
output: result,
|
|
213
|
+
progress: 100,
|
|
211
214
|
completedAt: new Date()
|
|
212
215
|
});
|
|
213
216
|
collectTaskStatistics(task);
|
|
214
|
-
|
|
217
|
+
} catch (error) {
|
|
218
|
+
if (error instanceof TaskStatusError || error instanceof TaskValidationError) {
|
|
219
|
+
throw error;
|
|
220
|
+
}
|
|
221
|
+
throw new TaskExecutionError(id, error, { operation: 'processNext' });
|
|
215
222
|
}
|
|
216
|
-
await options.task[task.type]({ task, result: result.data, context: task.context });
|
|
217
|
-
await task.update({
|
|
218
|
-
status: 'success',
|
|
219
|
-
output: result,
|
|
220
|
-
progress: 100,
|
|
221
|
-
completedAt: new Date()
|
|
222
|
-
});
|
|
223
|
-
collectTaskStatistics(task);
|
|
224
223
|
};
|
|
225
224
|
|
|
226
225
|
const processSystemTask = async task => {
|
|
@@ -289,6 +288,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
289
288
|
retryCount: currentRetryCount
|
|
290
289
|
});
|
|
291
290
|
collectTaskStatistics(task);
|
|
291
|
+
await handleError({ task, error: e.message || e, type: currentRetryCount > 1 ? 'retry_exhausted' : 'execution' });
|
|
292
292
|
}
|
|
293
293
|
});
|
|
294
294
|
};
|
|
@@ -341,6 +341,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
341
341
|
completedAt: now
|
|
342
342
|
});
|
|
343
343
|
collectTaskStatistics(task);
|
|
344
|
+
await handleError({ task, error: task.error, type: 'timeout' });
|
|
344
345
|
}
|
|
345
346
|
};
|
|
346
347
|
|
|
@@ -348,45 +349,86 @@ module.exports = fp(async (fastify, options) => {
|
|
|
348
349
|
// 检查超时任务
|
|
349
350
|
await checkTimeout();
|
|
350
351
|
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
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
|
+
|
|
358
379
|
if (limit <= 0) {
|
|
359
|
-
fastify.log.info(`当前运行中的系统任务数(${runningTaskCount})已达上限(${
|
|
380
|
+
fastify.log.info(`当前运行中的系统任务数(${runningTaskCount})已达上限(${config.limit}),暂不执行新任务`);
|
|
360
381
|
return;
|
|
361
382
|
}
|
|
362
|
-
// #1: 按优先级降序 + startTime 升序获取待处理任务
|
|
363
|
-
const pendingTasks = await models.task.findAll({
|
|
364
|
-
where: {
|
|
365
|
-
runnerType: 'system',
|
|
366
|
-
status: 'pending',
|
|
367
|
-
startTime: {
|
|
368
|
-
[Op.lte]: new Date()
|
|
369
|
-
}
|
|
370
|
-
},
|
|
371
|
-
order: [
|
|
372
|
-
['priority', 'DESC'],
|
|
373
|
-
['startTime', 'ASC']
|
|
374
|
-
],
|
|
375
|
-
limit: limit
|
|
376
|
-
});
|
|
377
383
|
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
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
|
+
}));
|
|
382
408
|
}
|
|
383
409
|
};
|
|
384
410
|
|
|
385
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
|
+
|
|
386
424
|
const task = await models.task.findByPk(id);
|
|
387
425
|
if (!task) {
|
|
388
|
-
throw new
|
|
426
|
+
throw new TaskNotFoundError(id);
|
|
389
427
|
}
|
|
428
|
+
|
|
429
|
+
// 缓存任务数据(短时间缓存,因为状态会变化)
|
|
430
|
+
cacheManager.set(cacheKey, task, 60000); // 1分钟缓存
|
|
431
|
+
|
|
390
432
|
return task;
|
|
391
433
|
};
|
|
392
434
|
|
|
@@ -458,6 +500,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
458
500
|
})
|
|
459
501
|
);
|
|
460
502
|
collectTaskStatistics(task);
|
|
503
|
+
await handleError({ task, error: e.message || e, type: 'execution' });
|
|
461
504
|
throw e;
|
|
462
505
|
}
|
|
463
506
|
} else {
|
|
@@ -470,6 +513,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
470
513
|
})
|
|
471
514
|
);
|
|
472
515
|
collectTaskStatistics(task);
|
|
516
|
+
await handleError({ task, error: props.error || '任务标记为失败', type: 'callback' });
|
|
473
517
|
}
|
|
474
518
|
};
|
|
475
519
|
|
|
@@ -622,70 +666,129 @@ module.exports = fp(async (fastify, options) => {
|
|
|
622
666
|
};
|
|
623
667
|
|
|
624
668
|
const log = async ({ id, taskId, data, message = '' }) => {
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
time: new Date()
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
...
|
|
641
|
-
logs
|
|
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];
|
|
642
685
|
}
|
|
643
|
-
});
|
|
644
686
|
|
|
645
|
-
|
|
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
|
+
}
|
|
646
704
|
};
|
|
647
705
|
|
|
648
706
|
const logWithSignature = async ({ id, taskId, data, message = '', signature }) => {
|
|
649
|
-
|
|
650
|
-
|
|
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
|
+
}
|
|
651
722
|
|
|
652
|
-
|
|
653
|
-
|
|
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' });
|
|
654
729
|
}
|
|
655
|
-
|
|
656
|
-
return log({ id: targetId, data, message });
|
|
657
730
|
};
|
|
658
731
|
|
|
659
732
|
const callback = async ({ id, code, data, message }) => {
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
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
|
+
}
|
|
679
767
|
};
|
|
680
768
|
|
|
681
769
|
const callbackWithSignature = async ({ id, code, data, message, signature }) => {
|
|
682
|
-
|
|
770
|
+
try {
|
|
771
|
+
const task = await detail({ id });
|
|
683
772
|
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
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
|
+
}
|
|
687
784
|
|
|
688
|
-
|
|
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
|
+
}
|
|
689
792
|
};
|
|
690
793
|
|
|
691
794
|
const append = async ({ dirs, tasks }) => {
|