@kne/fastify-task 2.0.4 → 2.0.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/index.js +3 -1
- package/libs/controllers/statistics.js +10 -6
- package/libs/services/main.js +2 -3
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
const fp = require('fastify-plugin');
|
|
2
2
|
|
|
3
3
|
/** 看板统计:概览 GET /statistics 与 SSE /statistics/sse(独立 controller 插件,见业务插件开发指南「路由标准化」) */
|
|
4
|
-
module.exports = fp(
|
|
5
|
-
|
|
6
|
-
const { services } = fastify[options.name];
|
|
4
|
+
module.exports = fp(async (fastify, options) => {
|
|
5
|
+
const { services } = fastify[options.name];
|
|
7
6
|
|
|
8
7
|
fastify.get(
|
|
9
8
|
`${options.prefix}/statistics`,
|
|
@@ -173,12 +172,17 @@ module.exports = fp(
|
|
|
173
172
|
}
|
|
174
173
|
},
|
|
175
174
|
async function (request, reply) {
|
|
176
|
-
const intervalSeconds = request.query.interval;
|
|
175
|
+
const intervalSeconds = request.query.interval ?? 5;
|
|
177
176
|
const { timezone, type, runnerType } = request.query;
|
|
178
|
-
|
|
177
|
+
const startTime = Date.now();
|
|
178
|
+
const maxDurationMs = 30 * 60 * 1000;
|
|
179
179
|
|
|
180
180
|
async function* eventStream() {
|
|
181
181
|
while (reply.sse.isConnected) {
|
|
182
|
+
if (Date.now() - startTime >= maxDurationMs) {
|
|
183
|
+
yield { event: 'timeout', data: JSON.stringify({ message: '连接已超过30分钟,自动断开' }) };
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
182
186
|
try {
|
|
183
187
|
yield { data: JSON.stringify(await services.statistics.getRealtime({ timezone, type, runnerType })) };
|
|
184
188
|
} catch (err) {
|
|
@@ -191,4 +195,4 @@ module.exports = fp(
|
|
|
191
195
|
await reply.sse.send(eventStream());
|
|
192
196
|
}
|
|
193
197
|
);
|
|
194
|
-
|
|
198
|
+
});
|
package/libs/services/main.js
CHANGED
|
@@ -8,8 +8,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
8
8
|
const { Op } = fastify.sequelize.Sequelize;
|
|
9
9
|
|
|
10
10
|
/** statistics 插件在 main 之后注册,不能在初始化时解构,否则恒为 undefined */
|
|
11
|
-
const callFlushCompletionStatistics = task =>
|
|
12
|
-
fastify[options.name].services.flushCompletionStatistics?.(task);
|
|
11
|
+
const callFlushCompletionStatistics = task => fastify[options.name].services.flushCompletionStatistics?.(task);
|
|
13
12
|
|
|
14
13
|
const generateSignature = ({ secret, id, data }) => {
|
|
15
14
|
const dataToSign = `${id}|${typeof data === 'string' ? data : JSON.stringify(data)}`;
|
|
@@ -57,7 +56,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
57
56
|
const executor = async ({ type, scriptName, ...props }) => {
|
|
58
57
|
const taskModulePath = path.resolve(options.dir, type, `${scriptName || options.scriptName}.js`);
|
|
59
58
|
if (!(await fs.exists(taskModulePath))) {
|
|
60
|
-
|
|
59
|
+
console.warn(`未匹配到任务执行器:${taskModulePath}`);
|
|
61
60
|
}
|
|
62
61
|
return await require(taskModulePath)(fastify, options, {
|
|
63
62
|
...props,
|