@kne/fastify-task 1.0.7 → 1.0.8
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 +56 -1
- package/package.json +1 -1
package/libs/services/main.js
CHANGED
|
@@ -251,6 +251,50 @@ module.exports = fp(async (fastify, options) => {
|
|
|
251
251
|
}
|
|
252
252
|
};
|
|
253
253
|
|
|
254
|
+
const waitingComplete = async ({ id, pollInterval = 1000, maxPollTimes = 20 }) => {
|
|
255
|
+
const task = await detail({ id });
|
|
256
|
+
if (!task) {
|
|
257
|
+
throw new Error('任务不存在');
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
if (task.status === 'pending') {
|
|
261
|
+
await processSystemTask(task);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
let pollCount = 0;
|
|
265
|
+
return await new Promise((resolve, reject) => {
|
|
266
|
+
const executePolling = async () => {
|
|
267
|
+
try {
|
|
268
|
+
pollCount++;
|
|
269
|
+
if (pollCount > maxPollTimes) {
|
|
270
|
+
reject(new Error('任务超时'));
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
await task.reload();
|
|
275
|
+
const { status, output, error } = task;
|
|
276
|
+
|
|
277
|
+
if (status === 'success') {
|
|
278
|
+
resolve(output);
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
if (status === 'failed' || status === 'canceled') {
|
|
283
|
+
reject(new Error(error || `任务${status === 'failed' ? '失败' : '取消'}`));
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// 任务未完成,等待pollInterval后继续执行
|
|
288
|
+
setTimeout(executePolling, pollInterval);
|
|
289
|
+
} catch (e) {
|
|
290
|
+
reject(e);
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
// 开始第一次轮询
|
|
294
|
+
setTimeout(executePolling, pollInterval);
|
|
295
|
+
});
|
|
296
|
+
};
|
|
297
|
+
|
|
254
298
|
const getTimeQuery = fieldValue => {
|
|
255
299
|
const { startTime, endTime } = fieldValue;
|
|
256
300
|
if (!!startTime && !!endTime) {
|
|
@@ -342,6 +386,17 @@ module.exports = fp(async (fastify, options) => {
|
|
|
342
386
|
};
|
|
343
387
|
|
|
344
388
|
Object.assign(fastify[options.name].services, {
|
|
345
|
-
create,
|
|
389
|
+
create,
|
|
390
|
+
detail,
|
|
391
|
+
list,
|
|
392
|
+
complete,
|
|
393
|
+
cancel,
|
|
394
|
+
runner,
|
|
395
|
+
resetAll,
|
|
396
|
+
retry,
|
|
397
|
+
executor,
|
|
398
|
+
processNext,
|
|
399
|
+
processSystemTask,
|
|
400
|
+
waitingComplete
|
|
346
401
|
});
|
|
347
402
|
});
|