@kne/fastify-task 1.0.3 → 1.0.5

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.
@@ -246,15 +246,30 @@ module.exports = fp(async (fastify, options) => {
246
246
 
247
247
  const complete = async ({ id, userId, output, ...props }) => {
248
248
  const task = await detail({ id });
249
+ if (!task) {
250
+ throw new Error('任务不存在');
251
+ }
249
252
  if (props.status === 'success') {
250
- await options.task[task.type]({ task, result: output });
251
- await task.update({
252
- status: 'success',
253
- output,
254
- progress: 100,
255
- completedAt: new Date(),
256
- completedUserId: userId
257
- });
253
+ try {
254
+ await options.task[task.type]({ task, result: output });
255
+ await task.update({
256
+ status: 'success',
257
+ output,
258
+ progress: 100,
259
+ completedAt: new Date(),
260
+ completedUserId: userId
261
+ });
262
+ } catch (e) {
263
+ await task.update(
264
+ Object.assign({}, props, {
265
+ status: 'failed',
266
+ output,
267
+ error: (e.stack || '').replaceAll(process.cwd(), '/server'),
268
+ completedAt: new Date()
269
+ })
270
+ );
271
+ throw e;
272
+ }
258
273
  } else {
259
274
  await task.update(
260
275
  Object.assign({}, props, {
@@ -302,8 +317,8 @@ module.exports = fp(async (fastify, options) => {
302
317
 
303
318
  const retryFunc = async ({ id }) => {
304
319
  const task = await detail({ id });
305
- if (task.status !== 'failed') {
306
- throw new Error('只有失败的任务允许重试');
320
+ if (['failed', 'canceled'].indexOf(task.status) === -1) {
321
+ throw new Error('只有失败或取消的任务允许重试');
307
322
  }
308
323
  await task.update({
309
324
  status: 'pending',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-task",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "用于编排任务,追踪任务执行状态,结果",
5
5
  "main": "index.js",
6
6
  "scripts": {