@kne/fastify-task 1.0.9 → 1.1.1

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 CHANGED
@@ -1,11 +1,8 @@
1
-
2
1
  # fastify-task
3
2
 
4
-
5
3
  ### 描述
6
4
 
7
- 用于编排任务,追踪任务执行状态,结果
8
-
5
+ 一个 Fastify 插件,用于任务编排、追踪任务执行状态和结果。支持系统自动执行和手动执行两种模式,提供完整的任务生命周期管理
9
6
 
10
7
  ### 安装
11
8
 
@@ -13,15 +10,264 @@
13
10
  npm i --save @kne/fastify-task
14
11
  ```
15
12
 
16
- ### 示例
13
+ ### 概述
14
+
15
+ @kne/fastify-task 是一个 Fastify 插件,用于任务编排、追踪任务执行状态和结果。支持系统自动执行和手动执行两种模式,提供完整的任务生命周期管理。
16
+
17
+ ### 主要特性
18
+
19
+ - **任务创建与管理**:支持创建、取消、重试、完成任务
20
+ - **多种执行模式**:支持系统自动执行(system)和手动执行(manual)
21
+ - **定时任务调度**:基于 cron 表达式的定时任务执行
22
+ - **任务状态追踪**:实时追踪任务进度、轮询结果
23
+ - **异步回调支持**:支持外部系统回调通知任务完成
24
+ - **日志记录**:支持任务执行日志记录,最多保留100条
25
+
26
+ ### 使用场景
17
27
 
18
- #### 示例代码
28
+ - 文档导出、数据迁移等异步处理任务
29
+ - 第三方 API 调用后异步回调处理
30
+ - 定时批量数据处理
31
+ - 需要追踪进度和状态的长时任务
19
32
 
33
+ ### 任务状态流转
20
34
 
35
+ | 状态 | 说明 |
36
+ |----------|--------|
37
+ | pending | 待执行 |
38
+ | running | 执行中 |
39
+ | waiting | 等待外部回调 |
40
+ | success | 执行成功 |
41
+ | failed | 执行失败 |
42
+ | canceled | 已取消 |
43
+
44
+ ### 示例
21
45
 
22
46
  ### API
23
47
 
24
- | 属性名 | 说明 | 类型 | 默认值 |
25
- |-----|----|----|-----|
26
- | | | | |
48
+ ### 插件配置
49
+
50
+ | 属性名 | 类型 | 默认值 | 说明 |
51
+ |-------------------|----------|----------------|----------------------|
52
+ | dbTableNamePrefix | string | 't_' | 数据库表名前缀 |
53
+ | prefix | string | '/api/task' | API 路由前缀 |
54
+ | name | string | 'task' | 命名空间名称 |
55
+ | limit | number | 10 | 并发执行任务数上限 |
56
+ | dir | string | 'libs/tasks' | 任务脚本目录 |
57
+ | cronTime | string | '*/10 * * * *' | cron 执行周期,设为 null 禁用 |
58
+ | scriptName | string | 'index' | 默认任务脚本名称 |
59
+ | maxPollTimes | number | 20 | 最大轮询次数 |
60
+ | pollInterval | number | 10000 | 轮询间隔(毫秒) |
61
+ | getUserModel | function | - | 获取用户模型的函数 |
62
+ | getAuthenticate | function | - | 获取认证中间件的函数 |
63
+ | task | object | {} | 任务类型处理函数映射 |
64
+
65
+ ### HTTP API
66
+
67
+ #### 获取任务列表
68
+
69
+ **GET** `{prefix}/list`
70
+
71
+ | 参数名 | 类型 | 默认值 | 说明 |
72
+ |------------------------------|--------|-----|------------|
73
+ | perPage | number | 20 | 每页数量 |
74
+ | currentPage | number | 1 | 当前页码 |
75
+ | filter.id | string | - | 任务ID |
76
+ | filter.targetId | string | - | 目标对象ID |
77
+ | filter.targetName | string | - | 目标名称(模糊匹配) |
78
+ | filter.type | string | - | 任务类型 |
79
+ | filter.status | string | - | 任务状态 |
80
+ | filter.runnerType | string | - | 执行者类型 |
81
+ | filter.createdAt.startTime | string | - | 创建时间起始 |
82
+ | filter.createdAt.endTime | string | - | 创建时间结束 |
83
+ | filter.completedAt.startTime | string | - | 完成时间起始 |
84
+ | filter.completedAt.endTime | string | - | 完成时间结束 |
85
+ | sort | object | - | 排序规则 |
86
+
87
+ **返回值**
88
+
89
+ | 属性名 | 类型 | 说明 |
90
+ |------------|--------|------|
91
+ | pageData | array | 任务列表 |
92
+ | totalCount | number | 总数量 |
93
+
94
+ #### 手动完成任务
95
+
96
+ **POST** `{prefix}/complete`
97
+
98
+ | 参数名 | 类型 | 必填 | 说明 |
99
+ |--------|--------|----|-------------------|
100
+ | id | string | 是 | 任务ID |
101
+ | status | string | 是 | 状态:success/failed |
102
+ | error | string | 否 | 错误信息 |
103
+ | msg | string | 否 | 消息 |
104
+ | output | object | 否 | 输出数据 |
105
+
106
+ #### 取消任务
107
+
108
+ **POST** `{prefix}/cancel`
109
+
110
+ | 参数名 | 类型 | 必填 | 说明 |
111
+ |-----|--------|----|------|
112
+ | id | string | 是 | 任务ID |
113
+
114
+ #### 重试任务
115
+
116
+ **POST** `{prefix}/retry`
117
+
118
+ | 参数名 | 类型 | 必填 | 说明 |
119
+ |---------|----------|----|--------|
120
+ | id | string | 否 | 单个任务ID |
121
+ | taskIds | string[] | 否 | 任务ID数组 |
122
+
123
+ #### 处理任务 Next
124
+
125
+ **POST** `{prefix}/next`
126
+
127
+ | 参数名 | 类型 | 必填 | 说明 |
128
+ |-----------|--------|----|----------------------------------------|
129
+ | id | string | 是 | 任务ID |
130
+ | signature | string | 否 | HMAC-SHA256签名(当任务设置了context.secret时必填) |
131
+ | result | string | 是 | JSON格式的结果 |
132
+
133
+ #### 记录任务日志
134
+
135
+ **POST** `{prefix}/log`
136
+
137
+ | 参数名 | 类型 | 必填 | 说明 |
138
+ |-----------|--------|----|----------------------------------------|
139
+ | id | string | 是 | 任务ID(body) |
140
+ | taskId | string | 是 | 任务ID(query) |
141
+ | data | object | 否 | 日志数据 |
142
+ | message | string | 否 | 日志消息 |
143
+ | signature | string | 否 | HMAC-SHA256签名(当任务设置了context.secret时必填) |
144
+
145
+ #### 任务回调
146
+
147
+ **POST** `{prefix}/callback`
148
+
149
+ | 参数名 | 类型 | 必填 | 说明 |
150
+ |-----------|--------|----|----------------------------------------|
151
+ | id | string | 是 | 任务ID(body) |
152
+ | taskId | string | 是 | 任务ID(query) |
153
+ | code | number | 是 | 状态码,0为成功 |
154
+ | data | object | 否 | 回调数据 |
155
+ | message | string | 否 | 回调消息 |
156
+ | signature | string | 否 | HMAC-SHA256签名(当任务设置了context.secret时必填) |
157
+
158
+ ### 服务层 API
159
+
160
+ #### create - 创建任务
161
+
162
+ | 参数名 | 类型 | 必填 | 说明 |
163
+ |------------|--------|----|---------------------|
164
+ | userId | string | 否 | 用户ID |
165
+ | input | object | 否 | 输入数据 |
166
+ | type | string | 是 | 任务类型 |
167
+ | targetId | string | 是 | 目标对象ID |
168
+ | targetType | string | 是 | 目标对象类型 |
169
+ | runnerType | string | 否 | 执行者类型:manual/system |
170
+ | delay | number | 否 | 延迟执行秒数 |
171
+ | scriptName | string | 否 | 脚本名称 |
172
+ | options | object | 否 | 扩展选项 |
173
+
174
+ **返回值**:Task 实例
175
+
176
+ #### detail - 获取任务详情
177
+
178
+ | 参数名 | 类型 | 必填 | 说明 |
179
+ |-----|--------|----|------|
180
+ | id | string | 是 | 任务ID |
181
+
182
+ **返回值**:Task 实例
183
+
184
+ #### list - 获取任务列表
185
+
186
+ 同 HTTP API list 接口参数
187
+
188
+ #### complete - 完成任务
189
+
190
+ | 参数名 | 类型 | 必填 | 说明 |
191
+ |--------|--------|----|-------------------|
192
+ | id | string | 是 | 任务ID |
193
+ | userId | string | 否 | 完成人ID |
194
+ | output | object | 否 | 输出数据 |
195
+ | status | string | 是 | 状态:success/failed |
196
+ | error | string | 否 | 错误信息 |
197
+
198
+ #### cancel - 取消任务
199
+
200
+ | 参数名 | 类型 | 必填 | 说明 |
201
+ |------------|--------|----|--------------|
202
+ | id | string | 否 | 任务ID |
203
+ | targetId | string | 否 | 目标对象ID(批量取消) |
204
+ | targetType | string | 否 | 目标对象类型(批量取消) |
205
+ | type | string | 否 | 任务类型(批量取消) |
206
+
207
+ #### retry - 重试任务
208
+
209
+ | 参数名 | 类型 | 必填 | 说明 |
210
+ |---------|----------|----|--------|
211
+ | id | string | 否 | 单个任务ID |
212
+ | taskIds | string[] | 否 | 任务ID数组 |
213
+
214
+ #### log - 记录日志
215
+
216
+ | 参数名 | 类型 | 必填 | 说明 |
217
+ |-----------|--------|----|----------------------------------------|
218
+ | id | string | 是 | 任务ID |
219
+ | taskId | string | 是 | 任务ID(id的别名) |
220
+ | data | object | 否 | 日志数据 |
221
+ | message | string | 否 | 日志消息 |
222
+ | signature | string | 否 | HMAC-SHA256签名(当任务设置了context.secret时必填) |
223
+
224
+ #### callback - 任务回调
225
+
226
+ | 参数名 | 类型 | 必填 | 说明 |
227
+ |-----------|--------|----|----------------------------------------|
228
+ | id | string | 是 | 任务ID |
229
+ | code | number | 是 | 状态码,0为成功 |
230
+ | data | object | 否 | 回调数据 |
231
+ | message | string | 否 | 回调消息 |
232
+ | signature | string | 否 | HMAC-SHA256签名(当任务设置了context.secret时必填) |
233
+
234
+ #### runner - 执行系统任务
235
+
236
+ 无参数,由 cron 定时调用
237
+
238
+ #### resetAll - 重置所有运行中任务
239
+
240
+ 无参数,将所有 running 状态任务重置为 pending
241
+
242
+ #### waitingComplete - 等待任务完成
243
+
244
+ | 参数名 | 类型 | 默认值 | 说明 |
245
+ |--------------|--------|------|----------|
246
+ | id | string | - | 任务ID |
247
+ | pollInterval | number | 1000 | 轮询间隔(毫秒) |
248
+ | maxPollTimes | number | 20 | 最大轮询次数 |
249
+
250
+ **返回值**:任务输出数据
251
+
252
+ ### 任务模型字段
27
253
 
254
+ | 属性名 | 类型 | 说明 |
255
+ |-------------|--------|---------------------|
256
+ | id | string | 任务ID |
257
+ | type | string | 任务类型 |
258
+ | scriptName | string | 脚本名称 |
259
+ | targetId | string | 目标对象ID |
260
+ | targetType | string | 目标对象类型 |
261
+ | runnerType | enum | 执行者类型:manual/system |
262
+ | startTime | date | 最早执行时间 |
263
+ | completedAt | date | 完成时间 |
264
+ | input | json | 输入数据 |
265
+ | output | json | 输出数据 |
266
+ | error | json | 错误信息 |
267
+ | status | enum | 任务状态 |
268
+ | context | json | 上下文信息 |
269
+ | pollResults | json | 轮询结果记录 |
270
+ | pollCount | number | 轮询次数 |
271
+ | progress | number | 进度(0-100) |
272
+ | msg | text | 消息 |
273
+ | options | json | 扩展选项 |
@@ -193,4 +193,83 @@ module.exports = fp(async (fastify, options) => {
193
193
  return await services.processNext(request.body);
194
194
  }
195
195
  );
196
+
197
+ fastify.post(
198
+ `${options.prefix}/log`,
199
+ {
200
+ schema: {
201
+ summary: '记录任务日志',
202
+ body: {
203
+ type: 'object',
204
+ properties: {
205
+ id: {
206
+ type: 'string'
207
+ },
208
+ data: {
209
+ type: 'object'
210
+ },
211
+ message: {
212
+ type: 'string'
213
+ },
214
+ signature: {
215
+ type: 'string',
216
+ description: 'HMAC-SHA256签名'
217
+ }
218
+ }
219
+ },
220
+ query: {
221
+ type: 'object',
222
+ properties: {
223
+ taskId: {
224
+ type: 'string'
225
+ }
226
+ }
227
+ }
228
+ }
229
+ },
230
+ async request => {
231
+ return await services.log(Object.assign({}, request.body, { taskId: request.query.taskId }));
232
+ }
233
+ );
234
+
235
+ fastify.post(
236
+ `${options.prefix}/callback`,
237
+ {
238
+ schema: {
239
+ summary: '任务回调',
240
+ body: {
241
+ type: 'object',
242
+ properties: {
243
+ id: {
244
+ type: 'string'
245
+ },
246
+ code: {
247
+ type: 'number'
248
+ },
249
+ data: {
250
+ type: 'object'
251
+ },
252
+ message: {
253
+ type: 'string'
254
+ },
255
+ signature: {
256
+ type: 'string',
257
+ description: 'HMAC-SHA256签名'
258
+ }
259
+ }
260
+ },
261
+ query: {
262
+ type: 'object',
263
+ properties: {
264
+ taskId: {
265
+ type: 'string'
266
+ }
267
+ }
268
+ }
269
+ }
270
+ },
271
+ async request => {
272
+ return await services.callback(Object.assign({}, request.body, { taskId: request.query.taskId }));
273
+ }
274
+ );
196
275
  });
@@ -1,21 +1,26 @@
1
1
  const fp = require('fastify-plugin');
2
2
  const path = require('node:path');
3
3
  const fs = require('fs-extra');
4
+ const crypto = require('node:crypto');
4
5
 
5
6
  module.exports = fp(async (fastify, options) => {
6
7
  const { models } = fastify[options.name];
7
8
  const { Op } = fastify.sequelize.Sequelize;
8
- const create = async ({
9
- userId,
10
- input,
11
- type,
12
- targetId,
13
- targetType,
14
- runnerType,
15
- delay,
16
- scriptName,
17
- options: currentOptions
18
- }) => {
9
+
10
+ const generateSignature = ({ secret, id, data }) => {
11
+ const dataToSign = `${id}|${typeof data === 'string' ? data : JSON.stringify(data)}`;
12
+ const hmac = crypto.createHmac('sha256', secret);
13
+ hmac.update(dataToSign);
14
+ return hmac.digest('hex');
15
+ };
16
+
17
+ const verifySignature = ({ secret, id, data, signature }) => {
18
+ if (!secret) return true;
19
+ const expectedSignature = generateSignature({ secret, id, data });
20
+ return signature === expectedSignature;
21
+ };
22
+
23
+ const create = async ({ userId, input, type, targetId, targetType, runnerType, delay, scriptName, options: currentOptions }) => {
19
24
  if (typeof options.task[type] !== 'function') {
20
25
  throw new Error('未找到合法的任务声明');
21
26
  }
@@ -33,13 +38,16 @@ module.exports = fp(async (fastify, options) => {
33
38
  };
34
39
 
35
40
  const resetAll = async () => {
36
- await models.task.update({
37
- status: 'pending'
38
- }, {
39
- where: {
40
- status: 'running'
41
+ await models.task.update(
42
+ {
43
+ status: 'pending'
44
+ },
45
+ {
46
+ where: {
47
+ status: 'running'
48
+ }
41
49
  }
42
- });
50
+ );
43
51
  };
44
52
 
45
53
  const executor = async ({ type, scriptName, ...props }) => {
@@ -48,11 +56,14 @@ module.exports = fp(async (fastify, options) => {
48
56
  throw new Error(`未匹配到任务执行器:${taskModulePath}`);
49
57
  }
50
58
  return await require(taskModulePath)(fastify, options, {
51
- ...props, updateProgress: async progress => {
52
- typeof props?.task?.update === 'function' && (await props.task.update({
53
- progress: progress
54
- }));
55
- }, polling: async (callback, currentOptions) => {
59
+ ...props,
60
+ updateProgress: async progress => {
61
+ typeof props?.task?.update === 'function' &&
62
+ (await props.task.update({
63
+ progress: progress
64
+ }));
65
+ },
66
+ polling: async (callback, currentOptions) => {
56
67
  let pollCount = 0;
57
68
  const maxPollTimes = currentOptions?.maxPollTimes || options.maxPollTimes;
58
69
  const pollInterval = currentOptions?.pollInterval || options.pollInterval;
@@ -70,10 +81,16 @@ module.exports = fp(async (fastify, options) => {
70
81
 
71
82
  if (typeof props?.task?.update === 'function') {
72
83
  await props.task.reload();
73
- await props.task.update(Object.assign({}, {
74
- pollCount: props.task.pollCount + 1,
75
- pollResults: [...props.task.pollResults, Object.assign({}, pollingResult, { time: new Date() })]
76
- }, Number.isInteger(progress) ? { progress } : {}));
84
+ await props.task.update(
85
+ Object.assign(
86
+ {},
87
+ {
88
+ pollCount: props.task.pollCount + 1,
89
+ pollResults: [...props.task.pollResults, Object.assign({}, pollingResult, { time: new Date() })]
90
+ },
91
+ Number.isInteger(progress) ? { progress } : {}
92
+ )
93
+ );
77
94
  }
78
95
 
79
96
  if (result === 'failed') {
@@ -95,7 +112,8 @@ module.exports = fp(async (fastify, options) => {
95
112
  // 开始第一次轮询
96
113
  setTimeout(executePolling, pollInterval);
97
114
  });
98
- }, next: async context => {
115
+ },
116
+ next: async context => {
99
117
  if (typeof props?.task?.update === 'function') {
100
118
  await props.task.update({ context, status: 'waiting' });
101
119
  }
@@ -109,25 +127,24 @@ module.exports = fp(async (fastify, options) => {
109
127
  if (task.status !== 'waiting') {
110
128
  throw new Error('当前任务状态不允许执行Next操作');
111
129
  }
112
- if (task.context?.secret) {
113
- const dataToSign = `${id}|${resultStr}`;
114
- // 使用 HMAC-SHA256 生成签名
115
- const hmac = crypto.createHmac('sha256', task.context.secret);
116
- hmac.update(dataToSign);
117
- if (signature !== hmac.digest('hex')) {
118
- throw new Error('签名验证失败');
119
- }
130
+ if (task.context?.secret && !verifySignature({ secret: task.context.secret, id, data: resultStr, signature })) {
131
+ throw new Error('签名验证失败');
120
132
  }
121
133
  const result = JSON.parse(resultStr);
122
134
  if (result.code !== 0) {
123
135
  await task.update({
124
- status: 'failed', error: result, completedAt: new Date()
136
+ status: 'failed',
137
+ error: result,
138
+ completedAt: new Date()
125
139
  });
126
140
  return;
127
141
  }
128
142
  await options.task[task.type]({ task, result: result.data, context: task.context });
129
143
  await task.update({
130
- status: 'success', output: result, progress: 100, completedAt: new Date()
144
+ status: 'success',
145
+ output: result,
146
+ progress: 100,
147
+ completedAt: new Date()
131
148
  });
132
149
  };
133
150
 
@@ -143,7 +160,7 @@ module.exports = fp(async (fastify, options) => {
143
160
  completedAt: null,
144
161
  context: {}
145
162
  });
146
- const addLog = (props) => {
163
+ const addLog = props => {
147
164
  return log(Object.assign({}, props, { taskId: task.id }));
148
165
  };
149
166
  executor({ type: task.type, scriptName: task.scriptName, task, log: addLog })
@@ -153,17 +170,24 @@ module.exports = fp(async (fastify, options) => {
153
170
  }
154
171
  await options.task[task.type]({ task, result, context: task.context });
155
172
  await task.update({
156
- status: 'success', output: result, progress: 100, completedAt: new Date()
173
+ status: 'success',
174
+ output: result,
175
+ progress: 100,
176
+ completedAt: new Date()
157
177
  });
158
178
  })
159
179
  .catch(e => {
160
180
  return task.update({
161
- status: 'failed', error: (e.stack || '').replaceAll(process.cwd(), '/server'), completedAt: new Date()
181
+ status: 'failed',
182
+ error: (e.stack || '').replaceAll(process.cwd(), '/server'),
183
+ completedAt: new Date()
162
184
  });
163
185
  });
164
186
  } catch (e) {
165
187
  await task.update({
166
- status: 'failed', error: (e.stack || '').replaceAll(process.cwd(), '/server'), completedAt: new Date()
188
+ status: 'failed',
189
+ error: (e.stack || '').replaceAll(process.cwd(), '/server'),
190
+ completedAt: new Date()
167
191
  });
168
192
  }
169
193
  };
@@ -171,7 +195,8 @@ module.exports = fp(async (fastify, options) => {
171
195
  const runner = async () => {
172
196
  const runningTaskCount = await models.task.count({
173
197
  where: {
174
- runnerType: 'system', status: 'running'
198
+ runnerType: 'system',
199
+ status: 'running'
175
200
  }
176
201
  });
177
202
  const limit = options.limit - runningTaskCount;
@@ -181,15 +206,19 @@ module.exports = fp(async (fastify, options) => {
181
206
  }
182
207
  const pendingTasks = await models.task.findAll({
183
208
  where: {
184
- runnerType: 'system', status: 'pending', startTime: {
209
+ runnerType: 'system',
210
+ status: 'pending',
211
+ startTime: {
185
212
  [Op.lte]: new Date()
186
213
  }
187
- }, limit: options.limit
214
+ },
215
+ limit: options.limit
188
216
  });
189
217
 
190
218
  const count = await models.task.count({
191
219
  where: {
192
- runnerType: 'system', status: 'pending'
220
+ runnerType: 'system',
221
+ status: 'pending'
193
222
  }
194
223
  });
195
224
 
@@ -209,15 +238,21 @@ module.exports = fp(async (fastify, options) => {
209
238
 
210
239
  const cancel = async ({ id, targetId, targetType, type }) => {
211
240
  if (targetId && targetType && type) {
212
- return await models.task.update({
213
- status: 'canceled'
214
- }, {
215
- where: {
216
- targetId, targetType, type, status: {
217
- [Op.in]: ['pending', 'running']
241
+ return await models.task.update(
242
+ {
243
+ status: 'canceled'
244
+ },
245
+ {
246
+ where: {
247
+ targetId,
248
+ targetType,
249
+ type,
250
+ status: {
251
+ [Op.in]: ['pending', 'running']
252
+ }
218
253
  }
219
254
  }
220
- });
255
+ );
221
256
  }
222
257
  if (id) {
223
258
  const task = await detail({ id });
@@ -239,18 +274,31 @@ module.exports = fp(async (fastify, options) => {
239
274
  try {
240
275
  await options.task[task.type]({ task, result: output });
241
276
  await task.update({
242
- status: 'success', output, progress: 100, completedAt: new Date(), completedUserId: userId
277
+ status: 'success',
278
+ output,
279
+ progress: 100,
280
+ completedAt: new Date(),
281
+ completedUserId: userId
243
282
  });
244
283
  } catch (e) {
245
- await task.update(Object.assign({}, props, {
246
- status: 'failed', output, error: (e.stack || '').replaceAll(process.cwd(), '/server'), completedAt: new Date()
247
- }));
284
+ await task.update(
285
+ Object.assign({}, props, {
286
+ status: 'failed',
287
+ output,
288
+ error: (e.stack || '').replaceAll(process.cwd(), '/server'),
289
+ completedAt: new Date()
290
+ })
291
+ );
248
292
  throw e;
249
293
  }
250
294
  } else {
251
- await task.update(Object.assign({}, props, {
252
- status: 'failed', completedAt: new Date(), completedUserId: userId
253
- }));
295
+ await task.update(
296
+ Object.assign({}, props, {
297
+ status: 'failed',
298
+ completedAt: new Date(),
299
+ completedUserId: userId
300
+ })
301
+ );
254
302
  }
255
303
  };
256
304
 
@@ -363,7 +411,8 @@ module.exports = fp(async (fastify, options) => {
363
411
  });
364
412
 
365
413
  return {
366
- pageData: rows, totalCount: count
414
+ pageData: rows,
415
+ totalCount: count
367
416
  };
368
417
  };
369
418
 
@@ -373,7 +422,9 @@ module.exports = fp(async (fastify, options) => {
373
422
  throw new Error('只有失败或取消的任务允许重试');
374
423
  }
375
424
  await task.update({
376
- status: 'pending', completedAt: null, completedUserId: null
425
+ status: 'pending',
426
+ completedAt: null,
427
+ completedUserId: null
377
428
  });
378
429
  };
379
430
 
@@ -388,27 +439,69 @@ module.exports = fp(async (fastify, options) => {
388
439
  }
389
440
  };
390
441
 
391
- const log = async ({ taskId, data, message = '' }) => {
392
- const task = await detail({ id: taskId });
442
+ const log = async ({ id, taskId, data, message = '', signature }) => {
443
+ const targetId = id || taskId;
444
+ const task = await detail({ id: targetId });
393
445
  if (!task) {
394
446
  throw new Error('任务不存在');
395
447
  }
396
448
 
449
+ if (task.context?.secret && !verifySignature({ secret: task.context.secret, id: targetId, data: { data, message }, signature })) {
450
+ throw new Error('签名验证失败');
451
+ }
452
+
397
453
  const currentOptions = task.options || {};
398
- const logs = currentOptions.logs || [];
399
- logs.push({
400
- data, message, time: new Date()
454
+ const logs = (currentOptions.logs || []).slice(0);
455
+ logs.splice(0, 0, {
456
+ data,
457
+ message,
458
+ time: new Date()
401
459
  });
402
-
460
+ if (logs.length > 100) {
461
+ logs.splice(0, logs.length - 100);
462
+ }
403
463
  await task.update({
404
464
  options: {
405
- ...currentOptions, logs
465
+ ...currentOptions,
466
+ logs
406
467
  }
407
468
  });
408
469
 
409
470
  return task;
410
471
  };
411
472
 
473
+ const callback = async ({ id, code, data, message, signature }) => {
474
+ const task = await detail({ id });
475
+ if (!task) {
476
+ throw new Error('任务不存在');
477
+ }
478
+
479
+ if (task.context?.secret && !verifySignature({ secret: task.context.secret, id, data: { code, data, message }, signature })) {
480
+ throw new Error('签名验证失败');
481
+ }
482
+
483
+ const result = { code, data, message };
484
+ const logData = JSON.stringify(result);
485
+ const logMessage = '回调结果';
486
+ const logSignature = task.context?.secret ? generateSignature({ secret: task.context.secret, id, data: { data: logData, message: logMessage } }) : undefined;
487
+ await log({ taskId: id, message: logMessage, data: logData, signature: logSignature });
488
+ const input = Object.assign(
489
+ {},
490
+ { id },
491
+ code === 0
492
+ ? {
493
+ status: 'success',
494
+ output: data
495
+ }
496
+ : {
497
+ status: 'failed',
498
+ output: data,
499
+ error: message
500
+ }
501
+ );
502
+ await complete(input);
503
+ };
504
+
412
505
  Object.assign(fastify[options.name].services, {
413
506
  create,
414
507
  detail,
@@ -419,9 +512,10 @@ module.exports = fp(async (fastify, options) => {
419
512
  resetAll,
420
513
  retry,
421
514
  log,
515
+ callback,
422
516
  executor,
423
517
  processNext,
424
518
  processSystemTask,
425
519
  waitingComplete
426
520
  });
427
- });
521
+ });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kne/fastify-task",
3
- "version": "1.0.9",
4
- "description": "用于编排任务,追踪任务执行状态,结果",
3
+ "version": "1.1.1",
4
+ "description": "一个 Fastify 插件,用于任务编排、追踪任务执行状态和结果。支持系统自动执行和手动执行两种模式,提供完整的任务生命周期管理",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
7
  "init": "husky",
@@ -10,7 +10,9 @@
10
10
  "build:md": "npx @kne/md-doc",
11
11
  "start:md": "npx @kne/md-doc --watch",
12
12
  "prettier": "prettier --config .prettierrc --write '{libs/**/*,index}.{js,jsx,ts,tsx,json,css,scss}'",
13
- "lint-staged": "npx lint-staged"
13
+ "lint-staged": "npx lint-staged",
14
+ "test": "mocha test/**/*.test.js",
15
+ "test:coverage": "nyc --reporter=html --reporter=text mocha test/**/*.test.js"
14
16
  },
15
17
  "lint-staged": {
16
18
  "{libs/**/*,index}.{js,jsx,ts,tsx,json,css,scss}": [
@@ -40,12 +42,17 @@
40
42
  },
41
43
  "devDependencies": {
42
44
  "@fastify/env": "^5.0.2",
45
+ "@kne/fastify-namespace": "*",
43
46
  "@kne/fastify-sequelize": "^3.0.6",
47
+ "chai": "^6.2.2",
44
48
  "fastify": "^5.4.0",
45
49
  "fastify-plugin": "^5.0.1",
46
50
  "husky": "^9.0.11",
51
+ "mocha": "^11.7.5",
47
52
  "nodemon": "^3.1.3",
53
+ "nyc": "^17.1.0",
48
54
  "prettier": "^3.2.5",
55
+ "sinon": "^21.0.0",
49
56
  "sqlite3": "^5.1.7"
50
57
  },
51
58
  "dependencies": {