@kne/fastify-task 0.1.0 → 0.1.2
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 +34 -9
- 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
|
@@ -85,14 +85,14 @@ module.exports = fp(async (fastify, options) => {
|
|
|
85
85
|
status: 'success',
|
|
86
86
|
output: result,
|
|
87
87
|
progress: 100,
|
|
88
|
-
|
|
88
|
+
completedAt: new Date()
|
|
89
89
|
});
|
|
90
90
|
})
|
|
91
91
|
.catch(error => {
|
|
92
92
|
return task.update({
|
|
93
93
|
status: 'failed',
|
|
94
94
|
error: error.toString(),
|
|
95
|
-
|
|
95
|
+
completedAt: new Date()
|
|
96
96
|
});
|
|
97
97
|
});
|
|
98
98
|
};
|
|
@@ -137,14 +137,26 @@ module.exports = fp(async (fastify, options) => {
|
|
|
137
137
|
});
|
|
138
138
|
};
|
|
139
139
|
|
|
140
|
-
const complete = async ({ id, userId, ...props }) => {
|
|
140
|
+
const complete = async ({ id, userId, output, ...props }) => {
|
|
141
141
|
const task = await detail({ id });
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
142
|
+
if (props.status === 'success') {
|
|
143
|
+
await options.task[task.type]({ task, result: output });
|
|
144
|
+
await task.update({
|
|
145
|
+
status: 'success',
|
|
146
|
+
output,
|
|
147
|
+
progress: 100,
|
|
148
|
+
completedAt: new Date(),
|
|
145
149
|
completedUserId: userId
|
|
146
|
-
})
|
|
147
|
-
|
|
150
|
+
});
|
|
151
|
+
} else {
|
|
152
|
+
await task.update(
|
|
153
|
+
Object.assign({}, props, {
|
|
154
|
+
status: 'failed',
|
|
155
|
+
completedAt: new Date(),
|
|
156
|
+
completedUserId: userId
|
|
157
|
+
})
|
|
158
|
+
);
|
|
159
|
+
}
|
|
148
160
|
};
|
|
149
161
|
|
|
150
162
|
const list = async ({ filter, perPage, currentPage }) => {
|
|
@@ -169,6 +181,18 @@ module.exports = fp(async (fastify, options) => {
|
|
|
169
181
|
};
|
|
170
182
|
};
|
|
171
183
|
|
|
184
|
+
const retry = async ({ id }) => {
|
|
185
|
+
const task = await detail({ id });
|
|
186
|
+
if (task.status !== 'failed') {
|
|
187
|
+
throw new Error('只有失败的任务允许重试');
|
|
188
|
+
}
|
|
189
|
+
await task.update({
|
|
190
|
+
status: 'pending',
|
|
191
|
+
completedAt: null,
|
|
192
|
+
completedUserId: null
|
|
193
|
+
});
|
|
194
|
+
};
|
|
195
|
+
|
|
172
196
|
Object.assign(fastify[options.name].services, {
|
|
173
197
|
create,
|
|
174
198
|
detail,
|
|
@@ -176,6 +200,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
176
200
|
complete,
|
|
177
201
|
cancel,
|
|
178
202
|
runner,
|
|
179
|
-
resetAll
|
|
203
|
+
resetAll,
|
|
204
|
+
retry
|
|
180
205
|
});
|
|
181
206
|
});
|