@kne/fastify-task 0.1.13 → 1.0.0-alpha.1
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/index.js +2 -2
- package/libs/controllers/main.js +29 -0
- package/libs/models/task.js +16 -1
- package/libs/services/main.js +64 -4
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -13,8 +13,8 @@ module.exports = fp(
|
|
|
13
13
|
dir: path.resolve(process.cwd(), 'libs', 'tasks'),
|
|
14
14
|
cronTime: '*/10 * * * *',
|
|
15
15
|
scriptName: 'index',
|
|
16
|
-
maxPollTimes:
|
|
17
|
-
pollInterval:
|
|
16
|
+
maxPollTimes: 20,
|
|
17
|
+
pollInterval: 10000,
|
|
18
18
|
getUserModel: () => {
|
|
19
19
|
return fastify.account.models.user;
|
|
20
20
|
},
|
package/libs/controllers/main.js
CHANGED
|
@@ -110,4 +110,33 @@ module.exports = fp(async (fastify, options) => {
|
|
|
110
110
|
return {};
|
|
111
111
|
}
|
|
112
112
|
);
|
|
113
|
+
|
|
114
|
+
fastify.post(
|
|
115
|
+
`${options.prefix}/next`,
|
|
116
|
+
{
|
|
117
|
+
schema: {
|
|
118
|
+
summary: '处理任务next',
|
|
119
|
+
body: {
|
|
120
|
+
type: 'object',
|
|
121
|
+
properties: {
|
|
122
|
+
id: {
|
|
123
|
+
type: 'string',
|
|
124
|
+
description: '任务ID'
|
|
125
|
+
},
|
|
126
|
+
signature: {
|
|
127
|
+
type: 'string',
|
|
128
|
+
description: '签名'
|
|
129
|
+
},
|
|
130
|
+
result: {
|
|
131
|
+
type: 'string',
|
|
132
|
+
description: '结果'
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
async request => {
|
|
139
|
+
return await services.processNext(request.body);
|
|
140
|
+
}
|
|
141
|
+
);
|
|
113
142
|
});
|
package/libs/models/task.js
CHANGED
|
@@ -46,10 +46,25 @@ module.exports = ({ DataTypes, definePrimaryType, options }) => {
|
|
|
46
46
|
comment: '错误信息'
|
|
47
47
|
},
|
|
48
48
|
status: {
|
|
49
|
-
type: DataTypes.ENUM('pending', 'running', 'success', 'failed', 'canceled'),
|
|
49
|
+
type: DataTypes.ENUM('pending', 'running', 'waiting', 'success', 'failed', 'canceled'),
|
|
50
50
|
comment: '任务状态',
|
|
51
51
|
defaultValue: 'pending'
|
|
52
52
|
},
|
|
53
|
+
context: {
|
|
54
|
+
type: DataTypes.JSON,
|
|
55
|
+
comment: '上下文信息',
|
|
56
|
+
defaultValue: {}
|
|
57
|
+
},
|
|
58
|
+
pollResults: {
|
|
59
|
+
type: DataTypes.JSON,
|
|
60
|
+
comment: '轮询执行结果',
|
|
61
|
+
defaultValue: []
|
|
62
|
+
},
|
|
63
|
+
pollCount: {
|
|
64
|
+
type: DataTypes.INTEGER,
|
|
65
|
+
comment: '轮询次数',
|
|
66
|
+
defaultValue: 0
|
|
67
|
+
},
|
|
53
68
|
progress: {
|
|
54
69
|
type: DataTypes.INTEGER,
|
|
55
70
|
comment: '任务进度(0-100)',
|
package/libs/services/main.js
CHANGED
|
@@ -59,7 +59,21 @@ module.exports = fp(async (fastify, options) => {
|
|
|
59
59
|
clearInterval(timer);
|
|
60
60
|
reject(new Error(`轮询超时(${maxPollTimes}次),任务未完成`));
|
|
61
61
|
}
|
|
62
|
-
const
|
|
62
|
+
const pollingResult = Object.assign({}, await callback());
|
|
63
|
+
const { result, data, message, progress } = pollingResult;
|
|
64
|
+
if (typeof props?.task?.update === 'function') {
|
|
65
|
+
await props.task.reload();
|
|
66
|
+
await props.task.update(
|
|
67
|
+
Object.assign(
|
|
68
|
+
{},
|
|
69
|
+
{
|
|
70
|
+
pollCount: props.task.pollCount + 1,
|
|
71
|
+
pollResults: [...props.task.pollResults, Object.assign({}, pollingResult, { time: new Date() })]
|
|
72
|
+
},
|
|
73
|
+
Number.isInteger(progress) ? { progress } : {}
|
|
74
|
+
)
|
|
75
|
+
);
|
|
76
|
+
}
|
|
63
77
|
if (result === 'failed') {
|
|
64
78
|
clearInterval(timer);
|
|
65
79
|
reject(new Error(`任务处理失败:${message}`));
|
|
@@ -70,7 +84,45 @@ module.exports = fp(async (fastify, options) => {
|
|
|
70
84
|
}
|
|
71
85
|
}, pollInterval);
|
|
72
86
|
});
|
|
87
|
+
},
|
|
88
|
+
next: async context => {
|
|
89
|
+
if (typeof props?.task?.update === 'function') {
|
|
90
|
+
await props.task.update({ context, status: 'waiting' });
|
|
91
|
+
}
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const processNext = async ({ id, signature, result: resultStr }) => {
|
|
98
|
+
const task = await detail({ id });
|
|
99
|
+
if (task.status !== 'waiting') {
|
|
100
|
+
throw new Error('当前任务状态不允许执行Next操作');
|
|
101
|
+
}
|
|
102
|
+
if (task.context?.secret) {
|
|
103
|
+
const dataToSign = `${id}|${resultStr}`;
|
|
104
|
+
// 使用 HMAC-SHA256 生成签名
|
|
105
|
+
const hmac = crypto.createHmac('sha256', task.context.secret);
|
|
106
|
+
hmac.update(dataToSign);
|
|
107
|
+
if (signature !== hmac.digest('hex')) {
|
|
108
|
+
throw new Error('签名验证失败');
|
|
73
109
|
}
|
|
110
|
+
}
|
|
111
|
+
const result = JSON.parse(resultStr);
|
|
112
|
+
if (result.code !== 0) {
|
|
113
|
+
await task.update({
|
|
114
|
+
status: 'failed',
|
|
115
|
+
error: result,
|
|
116
|
+
completedAt: new Date()
|
|
117
|
+
});
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
await options.task[task.type]({ task, result: result.data, context: task.context });
|
|
121
|
+
await task.update({
|
|
122
|
+
status: 'success',
|
|
123
|
+
output: result,
|
|
124
|
+
progress: 100,
|
|
125
|
+
completedAt: new Date()
|
|
74
126
|
});
|
|
75
127
|
};
|
|
76
128
|
|
|
@@ -80,11 +132,18 @@ module.exports = fp(async (fastify, options) => {
|
|
|
80
132
|
status: 'running',
|
|
81
133
|
progress: 0,
|
|
82
134
|
error: null,
|
|
83
|
-
output: null
|
|
135
|
+
output: null,
|
|
136
|
+
pollCount: 0,
|
|
137
|
+
pollResults: [],
|
|
138
|
+
completedAt: null,
|
|
139
|
+
context: {}
|
|
84
140
|
});
|
|
85
141
|
executor({ type: task.type, scriptName: task.scriptName, task })
|
|
86
142
|
.then(async result => {
|
|
87
|
-
|
|
143
|
+
if (result === false) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
await options.task[task.type]({ task, result, context: task.context });
|
|
88
147
|
await task.update({
|
|
89
148
|
status: 'success',
|
|
90
149
|
output: result,
|
|
@@ -246,6 +305,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
246
305
|
runner,
|
|
247
306
|
resetAll,
|
|
248
307
|
retry,
|
|
249
|
-
executor
|
|
308
|
+
executor,
|
|
309
|
+
processNext
|
|
250
310
|
});
|
|
251
311
|
});
|