@kne/fastify-task 1.0.4 → 1.0.6

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.
@@ -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: 'string',
39
- enum: ['ASC', 'DESC'],
40
- description: '按completedAt字段排序,ASC为升序,DESC为降序'
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
  }
@@ -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, {
@@ -266,25 +281,61 @@ module.exports = fp(async (fastify, options) => {
266
281
  }
267
282
  };
268
283
 
269
- const list = async ({ filter, perPage, currentPage }) => {
284
+ const getTimeQuery = fieldValue => {
285
+ const { startTime, endTime } = fieldValue;
286
+ if (!!startTime && !!endTime) {
287
+ // 两个日期都有值,使用between
288
+ return {
289
+ [Op.between]: [new Date(startTime), new Date(endTime)]
290
+ };
291
+ } else if (!!startTime) {
292
+ // 只有开始日期,大于等于
293
+ return {
294
+ [Op.gte]: new Date(startTime)
295
+ };
296
+ } else if (!!endTime) {
297
+ // 只有结束日期,小于等于
298
+ return {
299
+ [Op.lte]: new Date(endTime)
300
+ };
301
+ }
302
+ };
303
+
304
+ const list = async ({ filter, perPage, currentPage, sort }) => {
270
305
  const whereQuery = {};
271
306
 
272
- ['targetId', 'type', 'status', 'runnerType'].forEach(key => {
307
+ ['id', 'targetId', 'type', 'status', 'runnerType'].forEach(key => {
273
308
  if (filter && filter[key]) {
274
309
  whereQuery[key] = filter[key];
275
310
  }
276
311
  });
277
312
 
278
- // 处理completedAt排序
313
+ // 处理targetName模糊匹配,查询input.name字段
314
+ if (filter && filter.targetName) {
315
+ whereQuery['input.name'] = {
316
+ [Op.like]: `%${filter.targetName}%`
317
+ };
318
+ }
319
+
320
+ // 处理createdAt日期范围查询
321
+ if (filter && filter.createdAt) {
322
+ whereQuery.createdAt = getTimeQuery(filter.createdAt);
323
+ }
324
+
325
+ // 处理completedAt日期范围查询
326
+ if (filter && filter.completedAt) {
327
+ whereQuery.completedAt = getTimeQuery(filter.completedAt);
328
+ }
329
+
330
+ // 处理sort排序
279
331
  let orderBy = 'createdAt';
280
332
  let orderDirection = 'DESC';
281
333
 
282
- if (filter && filter.completedAt) {
283
- const completedAtValue = filter.completedAt.toUpperCase();
284
- if (['ASC', 'DESC'].includes(completedAtValue)) {
285
- orderBy = 'completedAt';
286
- orderDirection = completedAtValue;
287
- }
334
+ if (sort && Object.keys(sort).length > 0) {
335
+ Object.keys(sort).forEach(key => {
336
+ orderBy = key;
337
+ orderDirection = sort[key];
338
+ });
288
339
  }
289
340
 
290
341
  const { rows, count } = await models.task.findAndCountAll({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-task",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "用于编排任务,追踪任务执行状态,结果",
5
5
  "main": "index.js",
6
6
  "scripts": {