@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 CHANGED
@@ -28,7 +28,9 @@ module.exports = fp(
28
28
  options
29
29
  );
30
30
 
31
- fastify.register(require('@fastify/sse'));
31
+ if (!fastify.sse) {
32
+ fastify.register(require('@fastify/sse'));
33
+ }
32
34
 
33
35
  fastify.register(require('@kne/fastify-namespace'), {
34
36
  options,
@@ -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
- async (fastify, options) => {
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
- reply.sse.keepAlive();
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
+ });
@@ -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
- throw new Error(`未匹配到任务执行器:${taskModulePath}`);
59
+ console.warn(`未匹配到任务执行器:${taskModulePath}`);
61
60
  }
62
61
  return await require(taskModulePath)(fastify, options, {
63
62
  ...props,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-task",
3
- "version": "2.0.4",
3
+ "version": "2.0.7",
4
4
  "description": "一个 Fastify 插件,用于任务编排、追踪任务执行状态和结果。支持系统自动执行和手动执行两种模式,提供完整的任务生命周期管理",
5
5
  "main": "index.js",
6
6
  "scripts": {