@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.
@@ -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 crypto = require('node:crypto');
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 || 'manual'}:${completedHour}`;
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
- fastify[`${options.name}Statistics`].services
46
- .collect({
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 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
- };
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
- const verifySignature = ({ secret, id, data, signature }) => {
65
- if (!secret) return true;
66
- const expectedSignature = generateSignature({ secret, id, data });
67
- return signature === expectedSignature;
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
- const create = async ({ userId, input, type, targetId, targetType, runnerType, delay = 0, scriptName, priority = 0, parentTaskId, maxRetries = 0, timeout = 60, options: currentOptions }) => {
71
- if (typeof delay !== 'number' || delay < 0) {
72
- throw new Error('delay 必须为非负数');
73
- }
74
- if (typeof priority !== 'number' || !Number.isInteger(priority)) {
75
- throw new Error('priority 必须为整数');
76
- }
77
- if (typeof maxRetries !== 'number' || !Number.isInteger(maxRetries) || maxRetries < 0) {
78
- throw new Error('maxRetries 必须为非负整数');
79
- }
80
- if (typeof timeout !== 'number' || !Number.isInteger(timeout) || timeout < 0) {
81
- throw new Error('timeout 必须为非负整数');
82
- }
83
- if (typeof options.task[type] !== 'function') {
84
- throw new Error('未找到合法的任务声明');
85
- }
86
- const task = await models.task.create({
87
- userId,
88
- input,
89
- type,
90
- targetId,
91
- targetType,
92
- runnerType,
93
- priority,
94
- parentTaskId,
95
- maxRetries,
96
- timeout,
97
- startTime: delay > 0 ? new Date(Date.now() + 1000 * delay) : new Date(),
98
- scriptName,
99
- options: currentOptions
100
- });
101
- return task;
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
- const scriptFile = `${scriptName || options.scriptName}.js`;
118
- let taskModulePath = null;
119
- for (const dir of options.dirs) {
120
- const candidate = path.resolve(dir, type, scriptFile);
121
- if (await fs.exists(candidate)) {
122
- taskModulePath = candidate;
123
- break;
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
- if (!taskModulePath) {
127
- throw new Error(`未匹配到任务执行器:${type}/${scriptFile},已搜索目录:${options.dirs.join(',')}`);
128
- }
129
- // 清除 require 缓存,确保运行期间模块更新后能加载最新版本
130
- delete require.cache[taskModulePath];
131
- return await require(taskModulePath)(fastify, options, {
132
- ...props,
133
- updateProgress: async progress => {
134
- typeof props?.task?.update === 'function' &&
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
- const processNext = async ({ id, signature, result: resultStr }) => {
199
- const task = await detail({ id });
200
- if (task.status !== 'waiting') {
201
- throw new Error('当前任务状态不允许执行Next操作');
202
- }
203
- if (task.context?.secret && !verifySignature({ secret: task.context.secret, id, data: resultStr, signature })) {
204
- throw new Error('签名验证失败');
205
- }
206
- const result = JSON.parse(resultStr);
207
- if (result.code !== 0) {
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: 'failed',
210
- error: result,
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
- return;
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
- const runningTaskCount = await models.task.count({
352
- where: {
353
- runnerType: 'system',
354
- status: 'running'
355
- }
356
- });
357
- const limit = options.limit - runningTaskCount;
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})已达上限(${options.limit}),暂不执行新任务`);
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
- if (pendingTasks.length > 0) {
379
- fastify.log.info(`本轮执行 ${pendingTasks.length} 个待处理的系统任务`);
380
- // #8: await processSystemTask 返回的 Promise,防止同任务重复执行
381
- await Promise.all(pendingTasks.map(async task => processSystemTask(task)));
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 Error('任务不存在');
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
- const targetId = id || taskId;
626
- const task = await detail({ id: targetId });
627
-
628
- const currentOptions = task.options || {};
629
- const logs = (currentOptions.logs || []).slice(0);
630
- logs.splice(0, 0, {
631
- data,
632
- message,
633
- time: new Date()
634
- });
635
- if (logs.length > 100) {
636
- logs.splice(0, logs.length - 100);
637
- }
638
- await task.update({
639
- options: {
640
- ...currentOptions,
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
- return task;
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
- const targetId = id || taskId;
650
- const task = await detail({ id: targetId });
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
- if (task.context?.secret && !verifySignature({ secret: task.context.secret, id: targetId, data: { data, message }, signature })) {
653
- throw new Error('签名验证失败');
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
- await detail({ id });
661
-
662
- const result = { code, data, message };
663
- await log({ id, message: '回调结果', data: JSON.stringify(result) });
664
- const input = Object.assign(
665
- {},
666
- { id },
667
- code === 0
668
- ? {
669
- status: 'success',
670
- output: data
671
- }
672
- : {
673
- status: 'failed',
674
- output: data,
675
- error: message
676
- }
677
- );
678
- await complete(input);
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
- const task = await detail({ id });
770
+ try {
771
+ const task = await detail({ id });
683
772
 
684
- if (task.context?.secret && !verifySignature({ secret: task.context.secret, id, data: { code, data, message }, signature })) {
685
- throw new Error('签名验证失败');
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
- return callback({ id, code, data, message });
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 }) => {