@kne/fastify-task 0.1.1 → 0.1.3
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 +23 -1
- package/libs/services/main.js +58 -11
- package/package.json +1 -1
package/libs/controllers/main.js
CHANGED
|
@@ -62,7 +62,8 @@ module.exports = fp(async (fastify, options) => {
|
|
|
62
62
|
}
|
|
63
63
|
},
|
|
64
64
|
async request => {
|
|
65
|
-
|
|
65
|
+
await services.complete(Object.assign({}, request.body, { userId: request.userInfo?.id }));
|
|
66
|
+
return {};
|
|
66
67
|
}
|
|
67
68
|
);
|
|
68
69
|
|
|
@@ -86,4 +87,25 @@ module.exports = fp(async (fastify, options) => {
|
|
|
86
87
|
return services.cancel(request.body);
|
|
87
88
|
}
|
|
88
89
|
);
|
|
90
|
+
|
|
91
|
+
fastify.post(
|
|
92
|
+
`${options.prefix}/retry`,
|
|
93
|
+
{
|
|
94
|
+
onRequest: options.getAuthenticate('write'),
|
|
95
|
+
schema: {
|
|
96
|
+
summary: '重试任务',
|
|
97
|
+
body: {
|
|
98
|
+
type: 'object',
|
|
99
|
+
properties: {
|
|
100
|
+
id: {
|
|
101
|
+
type: 'string'
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
async request => {
|
|
108
|
+
return services.retry(request.body);
|
|
109
|
+
}
|
|
110
|
+
);
|
|
89
111
|
});
|
package/libs/services/main.js
CHANGED
|
@@ -130,21 +130,55 @@ module.exports = fp(async (fastify, options) => {
|
|
|
130
130
|
return task;
|
|
131
131
|
};
|
|
132
132
|
|
|
133
|
-
const cancel = async ({ id }) => {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
133
|
+
const cancel = async ({ id, targetId, targetType }) => {
|
|
134
|
+
if (targetId && targetType) {
|
|
135
|
+
return await models.task.update(
|
|
136
|
+
{
|
|
137
|
+
status: 'canceled'
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
where: {
|
|
141
|
+
targetId,
|
|
142
|
+
targetType,
|
|
143
|
+
status: {
|
|
144
|
+
[Op.in]: ['pending', 'running']
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
if (id) {
|
|
151
|
+
const task = await detail({ id });
|
|
152
|
+
if (!['pending', 'running'].includes(task.type)) {
|
|
153
|
+
throw new Error('任务状态不允许取消');
|
|
154
|
+
}
|
|
155
|
+
return await task.update({
|
|
156
|
+
status: 'canceled'
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
throw new Error('参数错误');
|
|
138
160
|
};
|
|
139
161
|
|
|
140
|
-
const complete = async ({ id, userId, ...props }) => {
|
|
162
|
+
const complete = async ({ id, userId, output, ...props }) => {
|
|
141
163
|
const task = await detail({ id });
|
|
142
|
-
|
|
143
|
-
|
|
164
|
+
if (props.status === 'success') {
|
|
165
|
+
await options.task[task.type]({ task, result: output });
|
|
166
|
+
await task.update({
|
|
167
|
+
status: 'success',
|
|
168
|
+
output,
|
|
169
|
+
progress: 100,
|
|
144
170
|
completedAt: new Date(),
|
|
145
171
|
completedUserId: userId
|
|
146
|
-
})
|
|
147
|
-
|
|
172
|
+
});
|
|
173
|
+
} else {
|
|
174
|
+
await task.update(
|
|
175
|
+
Object.assign({}, props, {
|
|
176
|
+
status: 'failed',
|
|
177
|
+
completedAt: new Date(),
|
|
178
|
+
completedUserId: userId
|
|
179
|
+
})
|
|
180
|
+
);
|
|
181
|
+
}
|
|
148
182
|
};
|
|
149
183
|
|
|
150
184
|
const list = async ({ filter, perPage, currentPage }) => {
|
|
@@ -169,6 +203,18 @@ module.exports = fp(async (fastify, options) => {
|
|
|
169
203
|
};
|
|
170
204
|
};
|
|
171
205
|
|
|
206
|
+
const retry = async ({ id }) => {
|
|
207
|
+
const task = await detail({ id });
|
|
208
|
+
if (task.status !== 'failed') {
|
|
209
|
+
throw new Error('只有失败的任务允许重试');
|
|
210
|
+
}
|
|
211
|
+
await task.update({
|
|
212
|
+
status: 'pending',
|
|
213
|
+
completedAt: null,
|
|
214
|
+
completedUserId: null
|
|
215
|
+
});
|
|
216
|
+
};
|
|
217
|
+
|
|
172
218
|
Object.assign(fastify[options.name].services, {
|
|
173
219
|
create,
|
|
174
220
|
detail,
|
|
@@ -176,6 +222,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
176
222
|
complete,
|
|
177
223
|
cancel,
|
|
178
224
|
runner,
|
|
179
|
-
resetAll
|
|
225
|
+
resetAll,
|
|
226
|
+
retry
|
|
180
227
|
});
|
|
181
228
|
});
|