@kne/fastify-task 1.0.2 → 1.0.4
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/controllers/main.js +26 -1
- package/libs/services/main.js +27 -4
- package/package.json +3 -1
package/libs/controllers/main.js
CHANGED
|
@@ -20,7 +20,26 @@ module.exports = fp(async (fastify, options) => {
|
|
|
20
20
|
default: 1
|
|
21
21
|
},
|
|
22
22
|
filter: {
|
|
23
|
-
type: 'object'
|
|
23
|
+
type: 'object',
|
|
24
|
+
properties: {
|
|
25
|
+
targetId: {
|
|
26
|
+
type: 'string'
|
|
27
|
+
},
|
|
28
|
+
type: {
|
|
29
|
+
type: 'string'
|
|
30
|
+
},
|
|
31
|
+
status: {
|
|
32
|
+
type: 'string'
|
|
33
|
+
},
|
|
34
|
+
runnerType: {
|
|
35
|
+
type: 'string'
|
|
36
|
+
},
|
|
37
|
+
completedAt: {
|
|
38
|
+
type: 'string',
|
|
39
|
+
enum: ['ASC', 'DESC'],
|
|
40
|
+
description: '按completedAt字段排序,ASC为升序,DESC为降序'
|
|
41
|
+
}
|
|
42
|
+
}
|
|
24
43
|
}
|
|
25
44
|
}
|
|
26
45
|
}
|
|
@@ -100,6 +119,12 @@ module.exports = fp(async (fastify, options) => {
|
|
|
100
119
|
properties: {
|
|
101
120
|
id: {
|
|
102
121
|
type: 'string'
|
|
122
|
+
},
|
|
123
|
+
taskIds: {
|
|
124
|
+
type: 'array',
|
|
125
|
+
items: {
|
|
126
|
+
type: 'string'
|
|
127
|
+
}
|
|
103
128
|
}
|
|
104
129
|
}
|
|
105
130
|
}
|
package/libs/services/main.js
CHANGED
|
@@ -275,11 +275,23 @@ module.exports = fp(async (fastify, options) => {
|
|
|
275
275
|
}
|
|
276
276
|
});
|
|
277
277
|
|
|
278
|
+
// 处理completedAt排序
|
|
279
|
+
let orderBy = 'createdAt';
|
|
280
|
+
let orderDirection = 'DESC';
|
|
281
|
+
|
|
282
|
+
if (filter && filter.completedAt) {
|
|
283
|
+
const completedAtValue = filter.completedAt.toUpperCase();
|
|
284
|
+
if (['ASC', 'DESC'].includes(completedAtValue)) {
|
|
285
|
+
orderBy = 'completedAt';
|
|
286
|
+
orderDirection = completedAtValue;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
278
290
|
const { rows, count } = await models.task.findAndCountAll({
|
|
279
291
|
where: Object.assign({}, whereQuery),
|
|
280
292
|
offset: perPage * (currentPage - 1),
|
|
281
293
|
limit: perPage,
|
|
282
|
-
order: [[
|
|
294
|
+
order: [[orderBy, orderDirection]]
|
|
283
295
|
});
|
|
284
296
|
|
|
285
297
|
return {
|
|
@@ -288,10 +300,10 @@ module.exports = fp(async (fastify, options) => {
|
|
|
288
300
|
};
|
|
289
301
|
};
|
|
290
302
|
|
|
291
|
-
const
|
|
303
|
+
const retryFunc = async ({ id }) => {
|
|
292
304
|
const task = await detail({ id });
|
|
293
|
-
if (task.status
|
|
294
|
-
throw new Error('
|
|
305
|
+
if (['failed', 'canceled'].indexOf(task.status) === -1) {
|
|
306
|
+
throw new Error('只有失败或取消的任务允许重试');
|
|
295
307
|
}
|
|
296
308
|
await task.update({
|
|
297
309
|
status: 'pending',
|
|
@@ -300,6 +312,17 @@ module.exports = fp(async (fastify, options) => {
|
|
|
300
312
|
});
|
|
301
313
|
};
|
|
302
314
|
|
|
315
|
+
const retry = async ({ id, taskIds }) => {
|
|
316
|
+
if (id) {
|
|
317
|
+
await retryFunc({ id });
|
|
318
|
+
}
|
|
319
|
+
if (taskIds && taskIds.length > 0) {
|
|
320
|
+
for (const taskId of taskIds) {
|
|
321
|
+
await retryFunc({ id: taskId });
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
|
|
303
326
|
Object.assign(fastify[options.name].services, {
|
|
304
327
|
create,
|
|
305
328
|
detail,
|
package/package.json
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kne/fastify-task",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "用于编排任务,追踪任务执行状态,结果",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"init": "husky",
|
|
8
|
+
"dev": "nodemon ./index.js",
|
|
9
|
+
"start": "node ./index.js",
|
|
8
10
|
"build:md": "npx @kne/md-doc",
|
|
9
11
|
"start:md": "npx @kne/md-doc --watch",
|
|
10
12
|
"prettier": "prettier --config .prettierrc --write '{libs/**/*,index}.{js,jsx,ts,tsx,json,css,scss}'",
|