@kne/fastify-task 2.0.5 → 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 +9 -4
- 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`,
|
|
@@ -175,9 +174,15 @@ module.exports = fp(
|
|
|
175
174
|
async function (request, reply) {
|
|
176
175
|
const intervalSeconds = request.query.interval ?? 5;
|
|
177
176
|
const { timezone, type, runnerType } = request.query;
|
|
177
|
+
const startTime = Date.now();
|
|
178
|
+
const maxDurationMs = 30 * 60 * 1000;
|
|
178
179
|
|
|
179
180
|
async function* eventStream() {
|
|
180
181
|
while (reply.sse.isConnected) {
|
|
182
|
+
if (Date.now() - startTime >= maxDurationMs) {
|
|
183
|
+
yield { event: 'timeout', data: JSON.stringify({ message: '连接已超过30分钟,自动断开' }) };
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
181
186
|
try {
|
|
182
187
|
yield { data: JSON.stringify(await services.statistics.getRealtime({ timezone, type, runnerType })) };
|
|
183
188
|
} catch (err) {
|
|
@@ -190,4 +195,4 @@ module.exports = fp(
|
|
|
190
195
|
await reply.sse.send(eventStream());
|
|
191
196
|
}
|
|
192
197
|
);
|
|
193
|
-
|
|
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,
|