@kne/fastify-task 1.1.0 → 1.1.2
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 +113 -24
- package/libs/controllers/main.js +232 -136
- package/libs/services/main.js +171 -72
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -41,6 +41,55 @@ npm i --save @kne/fastify-task
|
|
|
41
41
|
| failed | 执行失败 |
|
|
42
42
|
| canceled | 已取消 |
|
|
43
43
|
|
|
44
|
+
### 签名验证
|
|
45
|
+
|
|
46
|
+
当任务的 `context.secret` 设置了密钥时,外部调用需要提供 HMAC-SHA256 签名进行验证。
|
|
47
|
+
|
|
48
|
+
#### 签名生成方法
|
|
49
|
+
|
|
50
|
+
```javascript
|
|
51
|
+
const crypto = require('node:crypto');
|
|
52
|
+
|
|
53
|
+
function generateSignature({ secret, id, data }) {
|
|
54
|
+
// data 可以是字符串或对象
|
|
55
|
+
const dataStr = typeof data === 'string' ? data : JSON.stringify(data);
|
|
56
|
+
const dataToSign = `${id}|${dataStr}`;
|
|
57
|
+
const hmac = crypto.createHmac('sha256', secret);
|
|
58
|
+
hmac.update(dataToSign);
|
|
59
|
+
return hmac.digest('hex');
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
#### 各接口签名数据格式
|
|
64
|
+
|
|
65
|
+
| 接口 | data 格式 |
|
|
66
|
+
|----------------------|----------------------------------|
|
|
67
|
+
| processNext | `result` 字符串(JSON格式结果) |
|
|
68
|
+
| logWithSignature | `{ data, message }` 对象 |
|
|
69
|
+
| callbackWithSignature| `{ code, data, message }` 对象 |
|
|
70
|
+
|
|
71
|
+
#### 示例
|
|
72
|
+
|
|
73
|
+
```javascript
|
|
74
|
+
// processNext 签名
|
|
75
|
+
const result = JSON.stringify({ code: 0, data: { output: 'done' } });
|
|
76
|
+
const signature = generateSignature({ secret: 'your-secret', id: 'task-1', data: result });
|
|
77
|
+
|
|
78
|
+
// logWithSignature 签名
|
|
79
|
+
const signature = generateSignature({
|
|
80
|
+
secret: 'your-secret',
|
|
81
|
+
id: 'task-1',
|
|
82
|
+
data: { data: { key: 'value' }, message: '日志消息' }
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
// callbackWithSignature 签名
|
|
86
|
+
const signature = generateSignature({
|
|
87
|
+
secret: 'your-secret',
|
|
88
|
+
id: 'task-1',
|
|
89
|
+
data: { code: 0, data: { result: 'done' }, message: '成功' }
|
|
90
|
+
});
|
|
91
|
+
```
|
|
92
|
+
|
|
44
93
|
### 示例
|
|
45
94
|
|
|
46
95
|
### API
|
|
@@ -124,34 +173,36 @@ npm i --save @kne/fastify-task
|
|
|
124
173
|
|
|
125
174
|
**POST** `{prefix}/next`
|
|
126
175
|
|
|
127
|
-
| 参数名 | 类型 | 必填 | 说明
|
|
128
|
-
|
|
129
|
-
| id | string | 是 | 任务ID
|
|
130
|
-
| signature | string | 否 | HMAC-SHA256
|
|
131
|
-
| result | string | 是 | JSON格式的结果
|
|
176
|
+
| 参数名 | 类型 | 必填 | 说明 |
|
|
177
|
+
|-----------|--------|----|----------------------------------------|
|
|
178
|
+
| id | string | 是 | 任务ID |
|
|
179
|
+
| signature | string | 否 | HMAC-SHA256签名(当任务设置了context.secret时必填) |
|
|
180
|
+
| result | string | 是 | JSON格式的结果 |
|
|
132
181
|
|
|
133
182
|
#### 记录任务日志
|
|
134
183
|
|
|
135
184
|
**POST** `{prefix}/log`
|
|
136
185
|
|
|
137
|
-
| 参数名
|
|
138
|
-
|
|
139
|
-
| id
|
|
140
|
-
| taskId
|
|
141
|
-
| data
|
|
142
|
-
| message
|
|
186
|
+
| 参数名 | 类型 | 必填 | 说明 |
|
|
187
|
+
|-----------|--------|----|----------------------------------------|
|
|
188
|
+
| id | string | 是 | 任务ID(body) |
|
|
189
|
+
| taskId | string | 是 | 任务ID(query) |
|
|
190
|
+
| data | object | 否 | 日志数据 |
|
|
191
|
+
| message | string | 否 | 日志消息 |
|
|
192
|
+
| signature | string | 否 | HMAC-SHA256签名(当任务设置了context.secret时必填) |
|
|
143
193
|
|
|
144
194
|
#### 任务回调
|
|
145
195
|
|
|
146
196
|
**POST** `{prefix}/callback`
|
|
147
197
|
|
|
148
|
-
| 参数名
|
|
149
|
-
|
|
150
|
-
| id
|
|
151
|
-
| taskId
|
|
152
|
-
| code
|
|
153
|
-
| data
|
|
154
|
-
| message
|
|
198
|
+
| 参数名 | 类型 | 必填 | 说明 |
|
|
199
|
+
|-----------|--------|----|----------------------------------------|
|
|
200
|
+
| id | string | 是 | 任务ID(body) |
|
|
201
|
+
| taskId | string | 是 | 任务ID(query) |
|
|
202
|
+
| code | number | 是 | 状态码,0为成功 |
|
|
203
|
+
| data | object | 否 | 回调数据 |
|
|
204
|
+
| message | string | 否 | 回调消息 |
|
|
205
|
+
| signature | string | 否 | HMAC-SHA256签名(当任务设置了context.secret时必填) |
|
|
155
206
|
|
|
156
207
|
### 服务层 API
|
|
157
208
|
|
|
@@ -209,13 +260,51 @@ npm i --save @kne/fastify-task
|
|
|
209
260
|
| id | string | 否 | 单个任务ID |
|
|
210
261
|
| taskIds | string[] | 否 | 任务ID数组 |
|
|
211
262
|
|
|
212
|
-
#### log -
|
|
263
|
+
#### log - 记录日志(内部调用)
|
|
264
|
+
|
|
265
|
+
> 内部调用方法,不需要签名验证
|
|
266
|
+
|
|
267
|
+
| 参数名 | 类型 | 必填 | 说明 |
|
|
268
|
+
|-----------|--------|----|-----------------|
|
|
269
|
+
| id | string | 是 | 任务ID |
|
|
270
|
+
| taskId | string | 是 | 任务ID(id的别名) |
|
|
271
|
+
| data | object | 否 | 日志数据 |
|
|
272
|
+
| message | string | 否 | 日志消息 |
|
|
273
|
+
|
|
274
|
+
#### logWithSignature - 记录日志(外部调用)
|
|
275
|
+
|
|
276
|
+
> 外部调用方法,需要签名验证(HTTP API使用)
|
|
277
|
+
|
|
278
|
+
| 参数名 | 类型 | 必填 | 说明 |
|
|
279
|
+
|-----------|--------|----|----------------------------------------|
|
|
280
|
+
| id | string | 是 | 任务ID |
|
|
281
|
+
| taskId | string | 是 | 任务ID(id的别名) |
|
|
282
|
+
| data | object | 否 | 日志数据 |
|
|
283
|
+
| message | string | 否 | 日志消息 |
|
|
284
|
+
| signature | string | 否 | HMAC-SHA256签名(当任务设置了context.secret时必填) |
|
|
285
|
+
|
|
286
|
+
#### callback - 任务回调(内部调用)
|
|
287
|
+
|
|
288
|
+
> 内部调用方法,不需要签名验证
|
|
289
|
+
|
|
290
|
+
| 参数名 | 类型 | 必填 | 说明 |
|
|
291
|
+
|-----------|--------|----|-----------|
|
|
292
|
+
| id | string | 是 | 任务ID |
|
|
293
|
+
| code | number | 是 | 状态码,0为成功 |
|
|
294
|
+
| data | object | 否 | 回调数据 |
|
|
295
|
+
| message | string | 否 | 回调消息 |
|
|
296
|
+
|
|
297
|
+
#### callbackWithSignature - 任务回调(外部调用)
|
|
298
|
+
|
|
299
|
+
> 外部调用方法,需要签名验证(HTTP API使用)
|
|
213
300
|
|
|
214
|
-
| 参数名
|
|
215
|
-
|
|
216
|
-
|
|
|
217
|
-
|
|
|
218
|
-
|
|
|
301
|
+
| 参数名 | 类型 | 必填 | 说明 |
|
|
302
|
+
|-----------|--------|----|----------------------------------------|
|
|
303
|
+
| id | string | 是 | 任务ID |
|
|
304
|
+
| code | number | 是 | 状态码,0为成功 |
|
|
305
|
+
| data | object | 否 | 回调数据 |
|
|
306
|
+
| message | string | 否 | 回调消息 |
|
|
307
|
+
| signature | string | 否 | HMAC-SHA256签名(当任务设置了context.secret时必填) |
|
|
219
308
|
|
|
220
309
|
#### runner - 执行系统任务
|
|
221
310
|
|
package/libs/controllers/main.js
CHANGED
|
@@ -2,178 +2,274 @@ const fp = require('fastify-plugin');
|
|
|
2
2
|
|
|
3
3
|
module.exports = fp(async (fastify, options) => {
|
|
4
4
|
const { services } = fastify[options.name];
|
|
5
|
-
fastify.get(
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
},
|
|
5
|
+
fastify.get(
|
|
6
|
+
`${options.prefix}/list`,
|
|
7
|
+
{
|
|
8
|
+
onRequest: options.getAuthenticate('read'),
|
|
9
|
+
schema: {
|
|
10
|
+
summary: '获取任务列表',
|
|
11
|
+
query: {
|
|
12
|
+
type: 'object',
|
|
13
|
+
properties: {
|
|
14
|
+
perPage: {
|
|
15
|
+
type: 'number',
|
|
16
|
+
default: 20
|
|
17
|
+
},
|
|
18
|
+
currentPage: {
|
|
19
|
+
type: 'number',
|
|
20
|
+
default: 1
|
|
21
|
+
},
|
|
22
|
+
filter: {
|
|
23
|
+
type: 'object',
|
|
24
|
+
properties: {
|
|
25
|
+
id: {
|
|
26
|
+
type: 'string'
|
|
27
|
+
},
|
|
28
|
+
targetId: {
|
|
29
|
+
type: 'string'
|
|
30
|
+
},
|
|
31
|
+
targetName: {
|
|
32
|
+
type: 'string'
|
|
33
|
+
},
|
|
34
|
+
type: {
|
|
35
|
+
type: 'string'
|
|
36
|
+
},
|
|
37
|
+
status: {
|
|
38
|
+
type: 'string'
|
|
39
|
+
},
|
|
40
|
+
runnerType: {
|
|
41
|
+
type: 'string'
|
|
42
|
+
},
|
|
43
|
+
createdAt: {
|
|
44
|
+
type: 'object',
|
|
45
|
+
properties: {
|
|
46
|
+
startTime: {
|
|
47
|
+
type: 'string'
|
|
48
|
+
},
|
|
49
|
+
endTime: {
|
|
50
|
+
type: 'string'
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
description: 'createdAt区间查找'
|
|
54
|
+
},
|
|
55
|
+
completedAt: {
|
|
56
|
+
type: 'object',
|
|
57
|
+
properties: {
|
|
58
|
+
startTime: {
|
|
59
|
+
type: 'string'
|
|
60
|
+
},
|
|
61
|
+
endTime: {
|
|
62
|
+
type: 'string'
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
description: 'completedAt区间查找'
|
|
66
|
+
}
|
|
43
67
|
}
|
|
68
|
+
},
|
|
69
|
+
sort: {
|
|
70
|
+
type: 'object',
|
|
71
|
+
description: '按completedAt、updatedAt字段排序,ASC为升序,DESC为降序'
|
|
44
72
|
}
|
|
45
|
-
}, sort: {
|
|
46
|
-
type: 'object', description: '按completedAt、updatedAt字段排序,ASC为升序,DESC为降序'
|
|
47
73
|
}
|
|
48
74
|
}
|
|
49
75
|
}
|
|
76
|
+
},
|
|
77
|
+
async request => {
|
|
78
|
+
return services.list(request.query);
|
|
50
79
|
}
|
|
51
|
-
|
|
52
|
-
return services.list(request.query);
|
|
53
|
-
});
|
|
80
|
+
);
|
|
54
81
|
|
|
55
|
-
fastify.post(
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
82
|
+
fastify.post(
|
|
83
|
+
`${options.prefix}/complete`,
|
|
84
|
+
{
|
|
85
|
+
onRequest: options.getAuthenticate('write'),
|
|
86
|
+
schema: {
|
|
87
|
+
summary: '手动完成任务',
|
|
88
|
+
body: {
|
|
89
|
+
type: 'object',
|
|
90
|
+
properties: {
|
|
91
|
+
id: {
|
|
92
|
+
type: 'string'
|
|
93
|
+
},
|
|
94
|
+
status: {
|
|
95
|
+
type: 'string',
|
|
96
|
+
enum: ['success', 'failed']
|
|
97
|
+
},
|
|
98
|
+
error: {
|
|
99
|
+
type: 'string'
|
|
100
|
+
},
|
|
101
|
+
msg: {
|
|
102
|
+
type: 'string'
|
|
103
|
+
},
|
|
104
|
+
output: {
|
|
105
|
+
type: 'object'
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
required: ['id', 'status']
|
|
109
|
+
}
|
|
71
110
|
}
|
|
111
|
+
},
|
|
112
|
+
async request => {
|
|
113
|
+
await services.complete(Object.assign({}, request.body, { userId: request.userInfo?.id }));
|
|
114
|
+
return {};
|
|
72
115
|
}
|
|
73
|
-
|
|
74
|
-
await services.complete(Object.assign({}, request.body, { userId: request.userInfo?.id }));
|
|
75
|
-
return {};
|
|
76
|
-
});
|
|
116
|
+
);
|
|
77
117
|
|
|
78
|
-
fastify.post(
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
118
|
+
fastify.post(
|
|
119
|
+
`${options.prefix}/cancel`,
|
|
120
|
+
{
|
|
121
|
+
onRequest: options.getAuthenticate('write'),
|
|
122
|
+
schema: {
|
|
123
|
+
summary: '取消任务',
|
|
124
|
+
body: {
|
|
125
|
+
type: 'object',
|
|
126
|
+
properties: {
|
|
127
|
+
id: {
|
|
128
|
+
type: 'string'
|
|
129
|
+
}
|
|
84
130
|
}
|
|
85
131
|
}
|
|
86
132
|
}
|
|
133
|
+
},
|
|
134
|
+
async request => {
|
|
135
|
+
await services.cancel(request.body);
|
|
136
|
+
return {};
|
|
87
137
|
}
|
|
88
|
-
|
|
89
|
-
await services.cancel(request.body);
|
|
90
|
-
return {};
|
|
91
|
-
});
|
|
138
|
+
);
|
|
92
139
|
|
|
93
|
-
fastify.post(
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
140
|
+
fastify.post(
|
|
141
|
+
`${options.prefix}/retry`,
|
|
142
|
+
{
|
|
143
|
+
onRequest: options.getAuthenticate('write'),
|
|
144
|
+
schema: {
|
|
145
|
+
summary: '重试任务',
|
|
146
|
+
body: {
|
|
147
|
+
type: 'object',
|
|
148
|
+
properties: {
|
|
149
|
+
id: {
|
|
101
150
|
type: 'string'
|
|
151
|
+
},
|
|
152
|
+
taskIds: {
|
|
153
|
+
type: 'array',
|
|
154
|
+
items: {
|
|
155
|
+
type: 'string'
|
|
156
|
+
}
|
|
102
157
|
}
|
|
103
158
|
}
|
|
104
159
|
}
|
|
105
160
|
}
|
|
161
|
+
},
|
|
162
|
+
async request => {
|
|
163
|
+
await services.retry(request.body);
|
|
164
|
+
return {};
|
|
106
165
|
}
|
|
107
|
-
|
|
108
|
-
await services.retry(request.body);
|
|
109
|
-
return {};
|
|
110
|
-
});
|
|
166
|
+
);
|
|
111
167
|
|
|
112
|
-
fastify.post(
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
168
|
+
fastify.post(
|
|
169
|
+
`${options.prefix}/next`,
|
|
170
|
+
{
|
|
171
|
+
schema: {
|
|
172
|
+
summary: '处理任务next',
|
|
173
|
+
body: {
|
|
174
|
+
type: 'object',
|
|
175
|
+
properties: {
|
|
176
|
+
id: {
|
|
177
|
+
type: 'string',
|
|
178
|
+
description: '任务ID'
|
|
179
|
+
},
|
|
180
|
+
signature: {
|
|
181
|
+
type: 'string',
|
|
182
|
+
description: '签名'
|
|
183
|
+
},
|
|
184
|
+
result: {
|
|
185
|
+
type: 'string',
|
|
186
|
+
description: '结果'
|
|
187
|
+
}
|
|
122
188
|
}
|
|
123
189
|
}
|
|
124
190
|
}
|
|
191
|
+
},
|
|
192
|
+
async request => {
|
|
193
|
+
return await services.processNext(request.body);
|
|
125
194
|
}
|
|
126
|
-
|
|
127
|
-
return await services.processNext(request.body);
|
|
128
|
-
});
|
|
195
|
+
);
|
|
129
196
|
|
|
130
|
-
fastify.post(
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
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
|
+
}
|
|
140
218
|
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
219
|
+
},
|
|
220
|
+
query: {
|
|
221
|
+
type: 'object',
|
|
222
|
+
properties: {
|
|
223
|
+
taskId: {
|
|
224
|
+
type: 'string'
|
|
225
|
+
}
|
|
146
226
|
}
|
|
147
227
|
}
|
|
148
228
|
}
|
|
229
|
+
},
|
|
230
|
+
async request => {
|
|
231
|
+
return await services.logWithSignature(Object.assign({}, request.body, { taskId: request.query.taskId }));
|
|
149
232
|
}
|
|
150
|
-
|
|
151
|
-
return await services.log(Object.assign({}, request.body, { taskId: request.query.taskId }));
|
|
152
|
-
});
|
|
233
|
+
);
|
|
153
234
|
|
|
154
|
-
fastify.post(
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
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
|
+
}
|
|
166
259
|
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
260
|
+
},
|
|
261
|
+
query: {
|
|
262
|
+
type: 'object',
|
|
263
|
+
properties: {
|
|
264
|
+
taskId: {
|
|
265
|
+
type: 'string'
|
|
266
|
+
}
|
|
172
267
|
}
|
|
173
268
|
}
|
|
174
269
|
}
|
|
270
|
+
},
|
|
271
|
+
async request => {
|
|
272
|
+
return await services.callbackWithSignature(Object.assign({}, request.body, { taskId: request.query.taskId }));
|
|
175
273
|
}
|
|
176
|
-
|
|
177
|
-
return await services.callback(Object.assign({}, request.body, { taskId: request.query.taskId }));
|
|
178
|
-
});
|
|
274
|
+
);
|
|
179
275
|
});
|
package/libs/services/main.js
CHANGED
|
@@ -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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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,
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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(
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
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
|
-
},
|
|
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
|
-
|
|
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',
|
|
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',
|
|
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 =
|
|
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',
|
|
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',
|
|
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',
|
|
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',
|
|
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',
|
|
209
|
+
runnerType: 'system',
|
|
210
|
+
status: 'pending',
|
|
211
|
+
startTime: {
|
|
185
212
|
[Op.lte]: new Date()
|
|
186
213
|
}
|
|
187
|
-
},
|
|
214
|
+
},
|
|
215
|
+
limit: options.limit
|
|
188
216
|
});
|
|
189
217
|
|
|
190
218
|
const count = await models.task.count({
|
|
191
219
|
where: {
|
|
192
|
-
runnerType: 'system',
|
|
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
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
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',
|
|
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(
|
|
246
|
-
|
|
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(
|
|
252
|
-
|
|
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,
|
|
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',
|
|
425
|
+
status: 'pending',
|
|
426
|
+
completedAt: null,
|
|
427
|
+
completedUserId: null
|
|
377
428
|
});
|
|
378
429
|
};
|
|
379
430
|
|
|
@@ -388,8 +439,9 @@ module.exports = fp(async (fastify, options) => {
|
|
|
388
439
|
}
|
|
389
440
|
};
|
|
390
441
|
|
|
391
|
-
const log = async ({ taskId, data, message = '' }) => {
|
|
392
|
-
const
|
|
442
|
+
const log = async ({ id, taskId, data, message = '' }) => {
|
|
443
|
+
const targetId = id || taskId;
|
|
444
|
+
const task = await detail({ id: targetId });
|
|
393
445
|
if (!task) {
|
|
394
446
|
throw new Error('任务不存在');
|
|
395
447
|
}
|
|
@@ -397,30 +449,75 @@ module.exports = fp(async (fastify, options) => {
|
|
|
397
449
|
const currentOptions = task.options || {};
|
|
398
450
|
const logs = (currentOptions.logs || []).slice(0);
|
|
399
451
|
logs.splice(0, 0, {
|
|
400
|
-
data,
|
|
452
|
+
data,
|
|
453
|
+
message,
|
|
454
|
+
time: new Date()
|
|
401
455
|
});
|
|
402
456
|
if (logs.length > 100) {
|
|
403
457
|
logs.splice(0, logs.length - 100);
|
|
404
458
|
}
|
|
405
459
|
await task.update({
|
|
406
460
|
options: {
|
|
407
|
-
...currentOptions,
|
|
461
|
+
...currentOptions,
|
|
462
|
+
logs
|
|
408
463
|
}
|
|
409
464
|
});
|
|
410
465
|
|
|
411
466
|
return task;
|
|
412
467
|
};
|
|
413
468
|
|
|
414
|
-
const
|
|
469
|
+
const logWithSignature = async ({ id, taskId, data, message = '', signature }) => {
|
|
470
|
+
const targetId = id || taskId;
|
|
471
|
+
const task = await detail({ id: targetId });
|
|
472
|
+
if (!task) {
|
|
473
|
+
throw new Error('任务不存在');
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
if (task.context?.secret && !verifySignature({ secret: task.context.secret, id: targetId, data: { data, message }, signature })) {
|
|
477
|
+
throw new Error('签名验证失败');
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
return log({ id: targetId, data, message });
|
|
481
|
+
};
|
|
482
|
+
|
|
483
|
+
const callback = async ({ id, code, data, message }) => {
|
|
484
|
+
const task = await detail({ id });
|
|
485
|
+
if (!task) {
|
|
486
|
+
throw new Error('任务不存在');
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
const result = { code, data, message };
|
|
415
490
|
await log({ id, message: '回调结果', data: JSON.stringify(result) });
|
|
416
|
-
const input = Object.assign(
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
491
|
+
const input = Object.assign(
|
|
492
|
+
{},
|
|
493
|
+
{ id },
|
|
494
|
+
code === 0
|
|
495
|
+
? {
|
|
496
|
+
status: 'success',
|
|
497
|
+
output: data
|
|
498
|
+
}
|
|
499
|
+
: {
|
|
500
|
+
status: 'failed',
|
|
501
|
+
output: data,
|
|
502
|
+
error: message
|
|
503
|
+
}
|
|
504
|
+
);
|
|
421
505
|
await complete(input);
|
|
422
506
|
};
|
|
423
507
|
|
|
508
|
+
const callbackWithSignature = async ({ id, code, data, message, signature }) => {
|
|
509
|
+
const task = await detail({ id });
|
|
510
|
+
if (!task) {
|
|
511
|
+
throw new Error('任务不存在');
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
if (task.context?.secret && !verifySignature({ secret: task.context.secret, id, data: { code, data, message }, signature })) {
|
|
515
|
+
throw new Error('签名验证失败');
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
return callback({ id, code, data, message });
|
|
519
|
+
};
|
|
520
|
+
|
|
424
521
|
Object.assign(fastify[options.name].services, {
|
|
425
522
|
create,
|
|
426
523
|
detail,
|
|
@@ -431,10 +528,12 @@ module.exports = fp(async (fastify, options) => {
|
|
|
431
528
|
resetAll,
|
|
432
529
|
retry,
|
|
433
530
|
log,
|
|
531
|
+
logWithSignature,
|
|
434
532
|
callback,
|
|
533
|
+
callbackWithSignature,
|
|
435
534
|
executor,
|
|
436
535
|
processNext,
|
|
437
536
|
processSystemTask,
|
|
438
537
|
waitingComplete
|
|
439
538
|
});
|
|
440
|
-
});
|
|
539
|
+
});
|