@kne/fastify-task 0.1.5 → 0.1.7
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/services/main.js +63 -44
- package/package.json +1 -1
package/libs/services/main.js
CHANGED
|
@@ -48,56 +48,75 @@ module.exports = fp(async (fastify, options) => {
|
|
|
48
48
|
status: 'running',
|
|
49
49
|
progress: 0
|
|
50
50
|
});
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
.then(async result => {
|
|
83
|
-
await options.task[task.type]({ task, result });
|
|
84
|
-
await task.update({
|
|
85
|
-
status: 'success',
|
|
86
|
-
output: result,
|
|
87
|
-
progress: 100,
|
|
88
|
-
completedAt: new Date()
|
|
89
|
-
});
|
|
51
|
+
try {
|
|
52
|
+
require(taskModulePath)(fastify, options, {
|
|
53
|
+
task,
|
|
54
|
+
progress: async progress => {
|
|
55
|
+
await task.update({
|
|
56
|
+
progress
|
|
57
|
+
});
|
|
58
|
+
},
|
|
59
|
+
polling: async (callback, currentOptions) => {
|
|
60
|
+
let pollCount = 0;
|
|
61
|
+
const maxPollTimes = currentOptions?.maxPollTimes || options.maxPollTimes;
|
|
62
|
+
const pollInterval = currentOptions?.pollInterval || options.pollInterval;
|
|
63
|
+
return await new Promise((resolve, reject) => {
|
|
64
|
+
const timer = setInterval(async () => {
|
|
65
|
+
pollCount++;
|
|
66
|
+
if (pollCount > maxPollTimes) {
|
|
67
|
+
clearInterval(timer);
|
|
68
|
+
reject(`轮询超时(${maxPollTimes}次),任务未完成`);
|
|
69
|
+
}
|
|
70
|
+
const { result, data, message } = Object.assign({}, await callback());
|
|
71
|
+
if (result === 'failed') {
|
|
72
|
+
clearInterval(timer);
|
|
73
|
+
reject(`任务处理失败:${message}`);
|
|
74
|
+
}
|
|
75
|
+
if (result === 'success') {
|
|
76
|
+
clearInterval(timer);
|
|
77
|
+
resolve(data);
|
|
78
|
+
}
|
|
79
|
+
}, pollInterval);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
90
82
|
})
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
83
|
+
.then(async result => {
|
|
84
|
+
await options.task[task.type]({ task, result });
|
|
85
|
+
await task.update({
|
|
86
|
+
status: 'success',
|
|
87
|
+
output: result,
|
|
88
|
+
progress: 100,
|
|
89
|
+
completedAt: new Date()
|
|
90
|
+
});
|
|
91
|
+
})
|
|
92
|
+
.catch(error => {
|
|
93
|
+
return task.update({
|
|
94
|
+
status: 'failed',
|
|
95
|
+
error: error.toString(),
|
|
96
|
+
completedAt: new Date()
|
|
97
|
+
});
|
|
96
98
|
});
|
|
99
|
+
} catch (error) {
|
|
100
|
+
await task.update({
|
|
101
|
+
status: 'failed',
|
|
102
|
+
error: error.toString(),
|
|
103
|
+
completedAt: new Date()
|
|
97
104
|
});
|
|
105
|
+
}
|
|
98
106
|
};
|
|
99
107
|
|
|
100
108
|
const runner = async () => {
|
|
109
|
+
const runningTaskCount = await models.task.count({
|
|
110
|
+
where: {
|
|
111
|
+
runnerType: 'system',
|
|
112
|
+
status: 'running'
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
const limit = options.limit - runningTaskCount;
|
|
116
|
+
if (limit <= 0) {
|
|
117
|
+
fastify.log.info(`当前运行中的系统任务数(${runningTaskCount})已达上限(${options.limit}),暂不执行新任务`);
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
101
120
|
const pendingTasks = await models.task.findAll({
|
|
102
121
|
where: {
|
|
103
122
|
runnerType: 'system',
|