@kne/fastify-task 1.0.5 → 1.0.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.
- package/libs/controllers/main.js +32 -3
- package/libs/services/main.js +116 -123
- package/package.json +1 -1
package/libs/controllers/main.js
CHANGED
|
@@ -22,9 +22,15 @@ module.exports = fp(async (fastify, options) => {
|
|
|
22
22
|
filter: {
|
|
23
23
|
type: 'object',
|
|
24
24
|
properties: {
|
|
25
|
+
id: {
|
|
26
|
+
type: 'string'
|
|
27
|
+
},
|
|
25
28
|
targetId: {
|
|
26
29
|
type: 'string'
|
|
27
30
|
},
|
|
31
|
+
targetName: {
|
|
32
|
+
type: 'string'
|
|
33
|
+
},
|
|
28
34
|
type: {
|
|
29
35
|
type: 'string'
|
|
30
36
|
},
|
|
@@ -34,12 +40,35 @@ module.exports = fp(async (fastify, options) => {
|
|
|
34
40
|
runnerType: {
|
|
35
41
|
type: 'string'
|
|
36
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
|
+
},
|
|
37
55
|
completedAt: {
|
|
38
|
-
type: '
|
|
39
|
-
|
|
40
|
-
|
|
56
|
+
type: 'object',
|
|
57
|
+
properties: {
|
|
58
|
+
startTime: {
|
|
59
|
+
type: 'string'
|
|
60
|
+
},
|
|
61
|
+
endTime: {
|
|
62
|
+
type: 'string'
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
description: 'completedAt区间查找'
|
|
41
66
|
}
|
|
42
67
|
}
|
|
68
|
+
},
|
|
69
|
+
sort: {
|
|
70
|
+
type: 'object',
|
|
71
|
+
description: '按completedAt、updatedAt字段排序,ASC为升序,DESC为降序'
|
|
43
72
|
}
|
|
44
73
|
}
|
|
45
74
|
}
|
package/libs/services/main.js
CHANGED
|
@@ -5,7 +5,17 @@ const fs = require('fs-extra');
|
|
|
5
5
|
module.exports = fp(async (fastify, options) => {
|
|
6
6
|
const { models } = fastify[options.name];
|
|
7
7
|
const { Op } = fastify.sequelize.Sequelize;
|
|
8
|
-
const create = async ({
|
|
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
19
|
if (typeof options.task[type] !== 'function') {
|
|
10
20
|
throw new Error('未找到合法的任务声明');
|
|
11
21
|
}
|
|
@@ -23,16 +33,13 @@ module.exports = fp(async (fastify, options) => {
|
|
|
23
33
|
};
|
|
24
34
|
|
|
25
35
|
const resetAll = async () => {
|
|
26
|
-
await models.task.update(
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
where: {
|
|
32
|
-
status: 'running'
|
|
33
|
-
}
|
|
36
|
+
await models.task.update({
|
|
37
|
+
status: 'pending'
|
|
38
|
+
}, {
|
|
39
|
+
where: {
|
|
40
|
+
status: 'running'
|
|
34
41
|
}
|
|
35
|
-
);
|
|
42
|
+
});
|
|
36
43
|
};
|
|
37
44
|
|
|
38
45
|
const executor = async ({ type, scriptName, ...props }) => {
|
|
@@ -41,55 +48,54 @@ module.exports = fp(async (fastify, options) => {
|
|
|
41
48
|
throw new Error(`未匹配到任务执行器:${taskModulePath}`);
|
|
42
49
|
}
|
|
43
50
|
return await require(taskModulePath)(fastify, options, {
|
|
44
|
-
...props,
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}));
|
|
50
|
-
},
|
|
51
|
-
polling: async (callback, currentOptions) => {
|
|
51
|
+
...props, updateProgress: async progress => {
|
|
52
|
+
typeof props?.task?.update === 'function' && (await props.task.update({
|
|
53
|
+
progress: progress
|
|
54
|
+
}));
|
|
55
|
+
}, polling: async (callback, currentOptions) => {
|
|
52
56
|
let pollCount = 0;
|
|
53
57
|
const maxPollTimes = currentOptions?.maxPollTimes || options.maxPollTimes;
|
|
54
58
|
const pollInterval = currentOptions?.pollInterval || options.pollInterval;
|
|
55
59
|
return await new Promise((resolve, reject) => {
|
|
56
|
-
const
|
|
57
|
-
pollCount++;
|
|
58
|
-
if (pollCount > maxPollTimes) {
|
|
59
|
-
clearInterval(timer);
|
|
60
|
-
reject(new Error(`轮询超时(${maxPollTimes}次),任务未完成`));
|
|
61
|
-
}
|
|
60
|
+
const executePolling = async () => {
|
|
62
61
|
try {
|
|
62
|
+
pollCount++;
|
|
63
|
+
if (pollCount > maxPollTimes) {
|
|
64
|
+
reject(new Error(`轮询超时(${maxPollTimes}次),任务未完成`));
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
63
68
|
const pollingResult = Object.assign({}, await callback());
|
|
64
69
|
const { result, data, message, progress } = pollingResult;
|
|
70
|
+
|
|
65
71
|
if (typeof props?.task?.update === 'function') {
|
|
66
72
|
await props.task.reload();
|
|
67
|
-
await props.task.update(
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
pollCount: props.task.pollCount + 1,
|
|
72
|
-
pollResults: [...props.task.pollResults, Object.assign({}, pollingResult, { time: new Date() })]
|
|
73
|
-
},
|
|
74
|
-
Number.isInteger(progress) ? { progress } : {}
|
|
75
|
-
)
|
|
76
|
-
);
|
|
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 } : {}));
|
|
77
77
|
}
|
|
78
|
+
|
|
78
79
|
if (result === 'failed') {
|
|
79
|
-
clearInterval(timer);
|
|
80
80
|
reject(new Error(`任务处理失败:${message}`));
|
|
81
|
+
return;
|
|
81
82
|
}
|
|
83
|
+
|
|
82
84
|
if (result === 'success') {
|
|
83
|
-
clearInterval(timer);
|
|
84
85
|
resolve(data);
|
|
86
|
+
return;
|
|
85
87
|
}
|
|
88
|
+
|
|
89
|
+
// 任务未完成,等待pollInterval后继续执行
|
|
90
|
+
setTimeout(executePolling, pollInterval);
|
|
86
91
|
} catch (e) {
|
|
87
92
|
reject(e);
|
|
88
93
|
}
|
|
89
|
-
}
|
|
94
|
+
};
|
|
95
|
+
// 开始第一次轮询
|
|
96
|
+
setTimeout(executePolling, pollInterval);
|
|
90
97
|
});
|
|
91
|
-
},
|
|
92
|
-
next: async context => {
|
|
98
|
+
}, next: async context => {
|
|
93
99
|
if (typeof props?.task?.update === 'function') {
|
|
94
100
|
await props.task.update({ context, status: 'waiting' });
|
|
95
101
|
}
|
|
@@ -115,18 +121,13 @@ module.exports = fp(async (fastify, options) => {
|
|
|
115
121
|
const result = JSON.parse(resultStr);
|
|
116
122
|
if (result.code !== 0) {
|
|
117
123
|
await task.update({
|
|
118
|
-
status: 'failed',
|
|
119
|
-
error: result,
|
|
120
|
-
completedAt: new Date()
|
|
124
|
+
status: 'failed', error: result, completedAt: new Date()
|
|
121
125
|
});
|
|
122
126
|
return;
|
|
123
127
|
}
|
|
124
128
|
await options.task[task.type]({ task, result: result.data, context: task.context });
|
|
125
129
|
await task.update({
|
|
126
|
-
status: 'success',
|
|
127
|
-
output: result,
|
|
128
|
-
progress: 100,
|
|
129
|
-
completedAt: new Date()
|
|
130
|
+
status: 'success', output: result, progress: 100, completedAt: new Date()
|
|
130
131
|
});
|
|
131
132
|
};
|
|
132
133
|
|
|
@@ -149,24 +150,17 @@ module.exports = fp(async (fastify, options) => {
|
|
|
149
150
|
}
|
|
150
151
|
await options.task[task.type]({ task, result, context: task.context });
|
|
151
152
|
await task.update({
|
|
152
|
-
status: 'success',
|
|
153
|
-
output: result,
|
|
154
|
-
progress: 100,
|
|
155
|
-
completedAt: new Date()
|
|
153
|
+
status: 'success', output: result, progress: 100, completedAt: new Date()
|
|
156
154
|
});
|
|
157
155
|
})
|
|
158
156
|
.catch(e => {
|
|
159
157
|
return task.update({
|
|
160
|
-
status: 'failed',
|
|
161
|
-
error: (e.stack || '').replaceAll(process.cwd(), '/server'),
|
|
162
|
-
completedAt: new Date()
|
|
158
|
+
status: 'failed', error: (e.stack || '').replaceAll(process.cwd(), '/server'), completedAt: new Date()
|
|
163
159
|
});
|
|
164
160
|
});
|
|
165
161
|
} catch (e) {
|
|
166
162
|
await task.update({
|
|
167
|
-
status: 'failed',
|
|
168
|
-
error: (e.stack || '').replaceAll(process.cwd(), '/server'),
|
|
169
|
-
completedAt: new Date()
|
|
163
|
+
status: 'failed', error: (e.stack || '').replaceAll(process.cwd(), '/server'), completedAt: new Date()
|
|
170
164
|
});
|
|
171
165
|
}
|
|
172
166
|
};
|
|
@@ -174,8 +168,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
174
168
|
const runner = async () => {
|
|
175
169
|
const runningTaskCount = await models.task.count({
|
|
176
170
|
where: {
|
|
177
|
-
runnerType: 'system',
|
|
178
|
-
status: 'running'
|
|
171
|
+
runnerType: 'system', status: 'running'
|
|
179
172
|
}
|
|
180
173
|
});
|
|
181
174
|
const limit = options.limit - runningTaskCount;
|
|
@@ -185,19 +178,15 @@ module.exports = fp(async (fastify, options) => {
|
|
|
185
178
|
}
|
|
186
179
|
const pendingTasks = await models.task.findAll({
|
|
187
180
|
where: {
|
|
188
|
-
runnerType: 'system',
|
|
189
|
-
status: 'pending',
|
|
190
|
-
startTime: {
|
|
181
|
+
runnerType: 'system', status: 'pending', startTime: {
|
|
191
182
|
[Op.lte]: new Date()
|
|
192
183
|
}
|
|
193
|
-
},
|
|
194
|
-
limit: options.limit
|
|
184
|
+
}, limit: options.limit
|
|
195
185
|
});
|
|
196
186
|
|
|
197
187
|
const count = await models.task.count({
|
|
198
188
|
where: {
|
|
199
|
-
runnerType: 'system',
|
|
200
|
-
status: 'pending'
|
|
189
|
+
runnerType: 'system', status: 'pending'
|
|
201
190
|
}
|
|
202
191
|
});
|
|
203
192
|
|
|
@@ -217,21 +206,15 @@ module.exports = fp(async (fastify, options) => {
|
|
|
217
206
|
|
|
218
207
|
const cancel = async ({ id, targetId, targetType, type }) => {
|
|
219
208
|
if (targetId && targetType && type) {
|
|
220
|
-
return await models.task.update(
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
targetId,
|
|
227
|
-
targetType,
|
|
228
|
-
type,
|
|
229
|
-
status: {
|
|
230
|
-
[Op.in]: ['pending', 'running']
|
|
231
|
-
}
|
|
209
|
+
return await models.task.update({
|
|
210
|
+
status: 'canceled'
|
|
211
|
+
}, {
|
|
212
|
+
where: {
|
|
213
|
+
targetId, targetType, type, status: {
|
|
214
|
+
[Op.in]: ['pending', 'running']
|
|
232
215
|
}
|
|
233
216
|
}
|
|
234
|
-
);
|
|
217
|
+
});
|
|
235
218
|
}
|
|
236
219
|
if (id) {
|
|
237
220
|
const task = await detail({ id });
|
|
@@ -253,53 +236,76 @@ module.exports = fp(async (fastify, options) => {
|
|
|
253
236
|
try {
|
|
254
237
|
await options.task[task.type]({ task, result: output });
|
|
255
238
|
await task.update({
|
|
256
|
-
status: 'success',
|
|
257
|
-
output,
|
|
258
|
-
progress: 100,
|
|
259
|
-
completedAt: new Date(),
|
|
260
|
-
completedUserId: userId
|
|
239
|
+
status: 'success', output, progress: 100, completedAt: new Date(), completedUserId: userId
|
|
261
240
|
});
|
|
262
241
|
} catch (e) {
|
|
263
|
-
await task.update(
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
output,
|
|
267
|
-
error: (e.stack || '').replaceAll(process.cwd(), '/server'),
|
|
268
|
-
completedAt: new Date()
|
|
269
|
-
})
|
|
270
|
-
);
|
|
242
|
+
await task.update(Object.assign({}, props, {
|
|
243
|
+
status: 'failed', output, error: (e.stack || '').replaceAll(process.cwd(), '/server'), completedAt: new Date()
|
|
244
|
+
}));
|
|
271
245
|
throw e;
|
|
272
246
|
}
|
|
273
247
|
} else {
|
|
274
|
-
await task.update(
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
248
|
+
await task.update(Object.assign({}, props, {
|
|
249
|
+
status: 'failed', completedAt: new Date(), completedUserId: userId
|
|
250
|
+
}));
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
const getTimeQuery = fieldValue => {
|
|
255
|
+
const { startTime, endTime } = fieldValue;
|
|
256
|
+
if (!!startTime && !!endTime) {
|
|
257
|
+
// 两个日期都有值,使用between
|
|
258
|
+
return {
|
|
259
|
+
[Op.between]: [new Date(startTime), new Date(endTime)]
|
|
260
|
+
};
|
|
261
|
+
} else if (!!startTime) {
|
|
262
|
+
// 只有开始日期,大于等于
|
|
263
|
+
return {
|
|
264
|
+
[Op.gte]: new Date(startTime)
|
|
265
|
+
};
|
|
266
|
+
} else if (!!endTime) {
|
|
267
|
+
// 只有结束日期,小于等于
|
|
268
|
+
return {
|
|
269
|
+
[Op.lte]: new Date(endTime)
|
|
270
|
+
};
|
|
281
271
|
}
|
|
282
272
|
};
|
|
283
273
|
|
|
284
|
-
const list = async ({ filter, perPage, currentPage }) => {
|
|
274
|
+
const list = async ({ filter, perPage, currentPage, sort }) => {
|
|
285
275
|
const whereQuery = {};
|
|
286
276
|
|
|
287
|
-
['targetId', 'type', 'status', 'runnerType'].forEach(key => {
|
|
277
|
+
['id', 'targetId', 'type', 'status', 'runnerType'].forEach(key => {
|
|
288
278
|
if (filter && filter[key]) {
|
|
289
279
|
whereQuery[key] = filter[key];
|
|
290
280
|
}
|
|
291
281
|
});
|
|
292
282
|
|
|
293
|
-
// 处理
|
|
283
|
+
// 处理targetName模糊匹配,查询input.name字段
|
|
284
|
+
if (filter && filter.targetName) {
|
|
285
|
+
whereQuery['input.name'] = {
|
|
286
|
+
[Op.like]: `%${filter.targetName}%`
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// 处理createdAt日期范围查询
|
|
291
|
+
if (filter && filter.createdAt) {
|
|
292
|
+
whereQuery.createdAt = getTimeQuery(filter.createdAt);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// 处理completedAt日期范围查询
|
|
296
|
+
if (filter && filter.completedAt) {
|
|
297
|
+
whereQuery.completedAt = getTimeQuery(filter.completedAt);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// 处理sort排序
|
|
294
301
|
let orderBy = 'createdAt';
|
|
295
302
|
let orderDirection = 'DESC';
|
|
296
303
|
|
|
297
|
-
if (
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
}
|
|
304
|
+
if (sort && Object.keys(sort).length > 0) {
|
|
305
|
+
Object.keys(sort).forEach(key => {
|
|
306
|
+
orderBy = key;
|
|
307
|
+
orderDirection = sort[key];
|
|
308
|
+
});
|
|
303
309
|
}
|
|
304
310
|
|
|
305
311
|
const { rows, count } = await models.task.findAndCountAll({
|
|
@@ -310,8 +316,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
310
316
|
});
|
|
311
317
|
|
|
312
318
|
return {
|
|
313
|
-
pageData: rows,
|
|
314
|
-
totalCount: count
|
|
319
|
+
pageData: rows, totalCount: count
|
|
315
320
|
};
|
|
316
321
|
};
|
|
317
322
|
|
|
@@ -321,9 +326,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
321
326
|
throw new Error('只有失败或取消的任务允许重试');
|
|
322
327
|
}
|
|
323
328
|
await task.update({
|
|
324
|
-
status: 'pending',
|
|
325
|
-
completedAt: null,
|
|
326
|
-
completedUserId: null
|
|
329
|
+
status: 'pending', completedAt: null, completedUserId: null
|
|
327
330
|
});
|
|
328
331
|
};
|
|
329
332
|
|
|
@@ -339,16 +342,6 @@ module.exports = fp(async (fastify, options) => {
|
|
|
339
342
|
};
|
|
340
343
|
|
|
341
344
|
Object.assign(fastify[options.name].services, {
|
|
342
|
-
create,
|
|
343
|
-
detail,
|
|
344
|
-
list,
|
|
345
|
-
complete,
|
|
346
|
-
cancel,
|
|
347
|
-
runner,
|
|
348
|
-
resetAll,
|
|
349
|
-
retry,
|
|
350
|
-
executor,
|
|
351
|
-
processNext,
|
|
352
|
-
processSystemTask
|
|
345
|
+
create, detail, list, complete, cancel, runner, resetAll, retry, executor, processNext, processSystemTask
|
|
353
346
|
});
|
|
354
347
|
});
|