@kne/fastify-task 3.0.0-alpha.6 → 3.0.0-alpha.7

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.
@@ -1,49 +1,12 @@
1
1
  const fp = require('fastify-plugin');
2
+ const path = require('node:path');
2
3
  const fs = require('fs-extra');
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');
4
+ const crypto = require('node:crypto');
18
5
 
19
6
  module.exports = fp(async (fastify, options) => {
20
7
  const { models } = fastify[options.name];
21
8
  const { Op } = fastify.sequelize.Sequelize;
22
9
 
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
-
47
10
  /** 通过 @kne/fastify-statistics 采集任务完成数据 */
48
11
  const collectTaskStatistics = async task => {
49
12
  // 确保 task 数据是最新的(Sequelize update 后实例可能未完全同步)
@@ -70,7 +33,7 @@ module.exports = fp(async (fastify, options) => {
70
33
  }
71
34
 
72
35
  const completedHour = new Date(completedAt).getHours();
73
- const channel = `${task.type}:${task.runnerType || CONFIG_CONSTANTS.RUNNER_TYPES.MANUAL}:${completedHour}`;
36
+ const channel = `${task.type}:${task.runnerType || 'manual'}:${completedHour}`;
74
37
  const status = task.status || 'unknown';
75
38
 
76
39
  const data = { total: 1, [status]: 1 };
@@ -79,174 +42,216 @@ module.exports = fp(async (fastify, options) => {
79
42
  Object.assign(data, { waitingTime, executionTime, totalTime });
80
43
  Object.assign(unit, { waitingTime: 'ms', executionTime: 'ms', totalTime: 'ms' });
81
44
  }
82
-
83
- try {
84
- await fastify[`${options.name}Statistics`].services.collect({
45
+ fastify[`${options.name}Statistics`].services
46
+ .collect({
85
47
  channel,
86
48
  data,
87
49
  unit,
88
50
  time: completedAt
51
+ })
52
+ .catch(e => {
53
+ fastify.log.error(`采集任务统计数据失败: ${e.message}`);
89
54
  });
90
- } catch (error) {
91
- fastify.log.error(`采集任务统计数据失败: ${error.message}`, { taskId: task.id, error: error.stack });
92
- // 不抛出错误,避免影响主流程
93
- }
94
55
  };
95
56
 
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
- });
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
+ };
147
63
 
148
- return task;
149
- } catch (error) {
150
- if (error instanceof TaskValidationError) {
151
- throw error;
152
- }
153
- throw new TaskExecutionError(null, error, { operation: 'create' });
154
- }
64
+ const verifySignature = ({ secret, id, data, signature }) => {
65
+ if (!secret) return true;
66
+ const expectedSignature = generateSignature({ secret, id, data });
67
+ return signature === expectedSignature;
155
68
  };
156
69
 
157
- const resetAll = async () => {
158
- const runningTasks = await models.task.findAll({
159
- where: { status: 'running' },
160
- attributes: ['type', 'runnerType'],
161
- raw: true
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
162
100
  });
163
- await models.task.update(
164
- { status: 'pending' },
165
- { where: { status: 'running' } }
166
- );
101
+ return task;
167
102
  };
168
103
 
169
- const executor = async ({ type, scriptName, ...props }) => {
170
- return await taskExecutor.execute({ type, scriptName, ...props });
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);
171
107
  };
172
108
 
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
- }
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
+ };
180
116
 
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
- }
117
+ const resetAll = async (props = {}) => {
118
+ const [affectedCount] = await models.task.update({ status: 'pending' }, { where: getRunningResetWhere(props) });
119
+ return affectedCount;
120
+ };
192
121
 
193
- const result = SecurityValidator.safeJSONParse(resultStr);
194
- if (!result || typeof result !== 'object') {
195
- throw new TaskValidationError('result', resultStr, '结果必须是有效的JSON对象');
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;
196
130
  }
197
-
198
- if (result.code !== 0) {
199
- await task.update({
200
- status: CONFIG_CONSTANTS.TASK_STATUSES.FAILED,
201
- error: result,
202
- completedAt: new Date()
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);
203
193
  });
204
- collectTaskStatistics(task);
205
- await handleError({ task, error: result, type: 'callback' });
206
- return;
194
+ },
195
+ next: async context => {
196
+ if (typeof props?.task?.update === 'function') {
197
+ await props.task.update({ context, status: 'waiting' });
198
+ }
199
+ return false;
207
200
  }
201
+ });
202
+ };
208
203
 
209
- await config.task[task.type]({ task, result: result.data, context: task.context });
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) {
210
214
  await task.update({
211
- status: CONFIG_CONSTANTS.TASK_STATUSES.SUCCESS,
212
- output: result,
213
- progress: 100,
215
+ status: 'failed',
216
+ error: result,
214
217
  completedAt: new Date()
215
218
  });
216
219
  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' });
220
+ return;
222
221
  }
223
- };
224
-
225
- const processSystemTask = async task => {
222
+ await options.task[task.type]({ task, result: result.data, context: task.context });
226
223
  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: {}
224
+ status: 'success',
225
+ output: result,
226
+ progress: 100,
227
+ completedAt: new Date()
236
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
+ }
237
246
  const addLog = props => {
238
247
  return log(Object.assign({}, props, { taskId: task.id }));
239
248
  };
240
249
 
241
250
  // #3: 任务超时自动失败
242
- const createTimeout = ms => new Promise((_, reject) =>
243
- setTimeout(() => reject(new Error(`任务执行超时(${ms}ms)`)), ms)
244
- );
251
+ const createTimeout = ms => new Promise((_, reject) => setTimeout(() => reject(new Error(`任务执行超时(${ms}ms)`)), ms));
245
252
 
246
253
  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;
254
+ const racePromise = options.taskTimeout > 0 ? Promise.race([executorPromise, createTimeout(options.taskTimeout)]) : executorPromise;
250
255
 
251
256
  return racePromise
252
257
  .then(async result => {
@@ -288,7 +293,6 @@ module.exports = fp(async (fastify, options) => {
288
293
  retryCount: currentRetryCount
289
294
  });
290
295
  collectTaskStatistics(task);
291
- await handleError({ task, error: e.message || e, type: currentRetryCount > 1 ? 'retry_exhausted' : 'execution' });
292
296
  }
293
297
  });
294
298
  };
@@ -325,7 +329,7 @@ module.exports = fp(async (fastify, options) => {
325
329
 
326
330
  const tasksToFail = timedOutTasks.filter(task => {
327
331
  const elapsed = now.getTime() - new Date(task.startedAt).getTime();
328
- return elapsed > task.timeout * 60 * 1000;
332
+ return elapsed > task.timeout;
329
333
  });
330
334
 
331
335
  if (tasksToFail.length === 0) return;
@@ -337,98 +341,94 @@ module.exports = fp(async (fastify, options) => {
337
341
  const prevStatus = task.status;
338
342
  await task.update({
339
343
  status: 'failed',
340
- error: `任务超时:已执行 ${Math.round(elapsed / 60000)} 分钟,超过设定的 ${task.timeout} 分钟超时时间`,
344
+ error: `任务超时:已执行 ${elapsed}ms,超过设定的 ${task.timeout}ms 超时时间`,
341
345
  completedAt: now
342
346
  });
343
347
  collectTaskStatistics(task);
344
- await handleError({ task, error: task.error, type: 'timeout' });
345
348
  }
346
349
  };
347
350
 
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
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
358
360
  }
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
- }
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: {}
367
382
  },
368
- order: [
369
- ['priority', 'DESC'],
370
- ['startTime', 'ASC']
371
- ],
372
- limit: config.limit
373
- })
374
- ]);
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
+ };
375
403
 
376
- const runningTaskCount = runningTasks;
377
- const limit = config.limit - runningTaskCount;
404
+ const runner = async () => {
405
+ // 检查超时任务
406
+ await checkTimeout();
378
407
 
408
+ const runningTaskCount = await models.task.count({
409
+ where: {
410
+ runnerType: 'system',
411
+ status: 'running'
412
+ }
413
+ });
414
+ const limit = options.limit - runningTaskCount;
379
415
  if (limit <= 0) {
380
- fastify.log.info(`当前运行中的系统任务数(${runningTaskCount})已达上限(${config.limit}),暂不执行新任务`);
416
+ fastify.log.info(`当前运行中的系统任务数(${runningTaskCount})已达上限(${options.limit}),暂不执行新任务`);
381
417
  return;
382
418
  }
419
+ const pendingTasks = await claimPendingTasks(limit);
383
420
 
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
- }));
421
+ if (pendingTasks.length > 0) {
422
+ fastify.log.info(`本轮执行 ${pendingTasks.length} 个待处理的系统任务`);
423
+ await Promise.all(pendingTasks.map(async task => processSystemTask(task, { claimed: true })));
408
424
  }
409
425
  };
410
426
 
411
427
  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
428
  const task = await models.task.findByPk(id);
425
429
  if (!task) {
426
- throw new TaskNotFoundError(id);
430
+ throw new Error('任务不存在');
427
431
  }
428
-
429
- // 缓存任务数据(短时间缓存,因为状态会变化)
430
- cacheManager.set(cacheKey, task, 60000); // 1分钟缓存
431
-
432
432
  return task;
433
433
  };
434
434
 
@@ -500,7 +500,6 @@ module.exports = fp(async (fastify, options) => {
500
500
  })
501
501
  );
502
502
  collectTaskStatistics(task);
503
- await handleError({ task, error: e.message || e, type: 'execution' });
504
503
  throw e;
505
504
  }
506
505
  } else {
@@ -513,7 +512,6 @@ module.exports = fp(async (fastify, options) => {
513
512
  })
514
513
  );
515
514
  collectTaskStatistics(task);
516
- await handleError({ task, error: props.error || '任务标记为失败', type: 'callback' });
517
515
  }
518
516
  };
519
517
 
@@ -666,129 +664,70 @@ module.exports = fp(async (fastify, options) => {
666
664
  };
667
665
 
668
666
  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];
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
685
684
  }
685
+ });
686
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
- }
687
+ return task;
704
688
  };
705
689
 
706
690
  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
- }
691
+ const targetId = id || taskId;
692
+ const task = await detail({ id: targetId });
722
693
 
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' });
694
+ if (task.context?.secret && !verifySignature({ secret: task.context.secret, id: targetId, data: { data, message }, signature })) {
695
+ throw new Error('签名验证失败');
729
696
  }
697
+
698
+ return log({ id: targetId, data, message });
730
699
  };
731
700
 
732
701
  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
- }
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);
767
721
  };
768
722
 
769
723
  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
- }
724
+ const task = await detail({ id });
784
725
 
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' });
726
+ if (task.context?.secret && !verifySignature({ secret: task.context.secret, id, data: { code, data, message }, signature })) {
727
+ throw new Error('签名验证失败');
791
728
  }
729
+
730
+ return callback({ id, code, data, message });
792
731
  };
793
732
 
794
733
  const append = async ({ dirs, tasks }) => {
@@ -826,6 +765,7 @@ module.exports = fp(async (fastify, options) => {
826
765
  cancel,
827
766
  runner,
828
767
  resetAll,
768
+ claimPendingTasks,
829
769
  retry,
830
770
  log,
831
771
  logWithSignature,