@kne/fastify-task 1.0.6 → 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/services/main.js +71 -114
- package/package.json +1 -1
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,31 +236,18 @@ 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
|
-
completedAt: new Date(),
|
|
278
|
-
completedUserId: userId
|
|
279
|
-
})
|
|
280
|
-
);
|
|
248
|
+
await task.update(Object.assign({}, props, {
|
|
249
|
+
status: 'failed', completedAt: new Date(), completedUserId: userId
|
|
250
|
+
}));
|
|
281
251
|
}
|
|
282
252
|
};
|
|
283
253
|
|
|
@@ -346,8 +316,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
346
316
|
});
|
|
347
317
|
|
|
348
318
|
return {
|
|
349
|
-
pageData: rows,
|
|
350
|
-
totalCount: count
|
|
319
|
+
pageData: rows, totalCount: count
|
|
351
320
|
};
|
|
352
321
|
};
|
|
353
322
|
|
|
@@ -357,9 +326,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
357
326
|
throw new Error('只有失败或取消的任务允许重试');
|
|
358
327
|
}
|
|
359
328
|
await task.update({
|
|
360
|
-
status: 'pending',
|
|
361
|
-
completedAt: null,
|
|
362
|
-
completedUserId: null
|
|
329
|
+
status: 'pending', completedAt: null, completedUserId: null
|
|
363
330
|
});
|
|
364
331
|
};
|
|
365
332
|
|
|
@@ -375,16 +342,6 @@ module.exports = fp(async (fastify, options) => {
|
|
|
375
342
|
};
|
|
376
343
|
|
|
377
344
|
Object.assign(fastify[options.name].services, {
|
|
378
|
-
create,
|
|
379
|
-
detail,
|
|
380
|
-
list,
|
|
381
|
-
complete,
|
|
382
|
-
cancel,
|
|
383
|
-
runner,
|
|
384
|
-
resetAll,
|
|
385
|
-
retry,
|
|
386
|
-
executor,
|
|
387
|
-
processNext,
|
|
388
|
-
processSystemTask
|
|
345
|
+
create, detail, list, complete, cancel, runner, resetAll, retry, executor, processNext, processSystemTask
|
|
389
346
|
});
|
|
390
347
|
});
|