@kne/fastify-task 1.0.7 → 1.0.9
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 +83 -3
- package/package.json +1 -1
package/libs/services/main.js
CHANGED
|
@@ -143,7 +143,10 @@ module.exports = fp(async (fastify, options) => {
|
|
|
143
143
|
completedAt: null,
|
|
144
144
|
context: {}
|
|
145
145
|
});
|
|
146
|
-
|
|
146
|
+
const addLog = (props) => {
|
|
147
|
+
return log(Object.assign({}, props, { taskId: task.id }));
|
|
148
|
+
};
|
|
149
|
+
executor({ type: task.type, scriptName: task.scriptName, task, log: addLog })
|
|
147
150
|
.then(async result => {
|
|
148
151
|
if (result === false) {
|
|
149
152
|
return;
|
|
@@ -251,6 +254,50 @@ module.exports = fp(async (fastify, options) => {
|
|
|
251
254
|
}
|
|
252
255
|
};
|
|
253
256
|
|
|
257
|
+
const waitingComplete = async ({ id, pollInterval = 1000, maxPollTimes = 20 }) => {
|
|
258
|
+
const task = await detail({ id });
|
|
259
|
+
if (!task) {
|
|
260
|
+
throw new Error('任务不存在');
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if (task.status === 'pending') {
|
|
264
|
+
await processSystemTask(task);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
let pollCount = 0;
|
|
268
|
+
return await new Promise((resolve, reject) => {
|
|
269
|
+
const executePolling = async () => {
|
|
270
|
+
try {
|
|
271
|
+
pollCount++;
|
|
272
|
+
if (pollCount > maxPollTimes) {
|
|
273
|
+
reject(new Error('任务超时'));
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
await task.reload();
|
|
278
|
+
const { status, output, error } = task;
|
|
279
|
+
|
|
280
|
+
if (status === 'success') {
|
|
281
|
+
resolve(output);
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
if (status === 'failed' || status === 'canceled') {
|
|
286
|
+
reject(new Error(error || `任务${status === 'failed' ? '失败' : '取消'}`));
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// 任务未完成,等待pollInterval后继续执行
|
|
291
|
+
setTimeout(executePolling, pollInterval);
|
|
292
|
+
} catch (e) {
|
|
293
|
+
reject(e);
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
// 开始第一次轮询
|
|
297
|
+
setTimeout(executePolling, pollInterval);
|
|
298
|
+
});
|
|
299
|
+
};
|
|
300
|
+
|
|
254
301
|
const getTimeQuery = fieldValue => {
|
|
255
302
|
const { startTime, endTime } = fieldValue;
|
|
256
303
|
if (!!startTime && !!endTime) {
|
|
@@ -341,7 +388,40 @@ module.exports = fp(async (fastify, options) => {
|
|
|
341
388
|
}
|
|
342
389
|
};
|
|
343
390
|
|
|
391
|
+
const log = async ({ taskId, data, message = '' }) => {
|
|
392
|
+
const task = await detail({ id: taskId });
|
|
393
|
+
if (!task) {
|
|
394
|
+
throw new Error('任务不存在');
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
const currentOptions = task.options || {};
|
|
398
|
+
const logs = currentOptions.logs || [];
|
|
399
|
+
logs.push({
|
|
400
|
+
data, message, time: new Date()
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
await task.update({
|
|
404
|
+
options: {
|
|
405
|
+
...currentOptions, logs
|
|
406
|
+
}
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
return task;
|
|
410
|
+
};
|
|
411
|
+
|
|
344
412
|
Object.assign(fastify[options.name].services, {
|
|
345
|
-
create,
|
|
413
|
+
create,
|
|
414
|
+
detail,
|
|
415
|
+
list,
|
|
416
|
+
complete,
|
|
417
|
+
cancel,
|
|
418
|
+
runner,
|
|
419
|
+
resetAll,
|
|
420
|
+
retry,
|
|
421
|
+
log,
|
|
422
|
+
executor,
|
|
423
|
+
processNext,
|
|
424
|
+
processSystemTask,
|
|
425
|
+
waitingComplete
|
|
346
426
|
});
|
|
347
|
-
});
|
|
427
|
+
});
|