@kne/fastify-task 3.0.0-alpha.6 → 3.0.0-alpha.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
@@ -1,12 +1,51 @@
1
1
  const fp = require('fastify-plugin');
2
2
  const path = require('node:path');
3
- const { getConfigManager } = require('./libs/utils/config');
4
3
 
5
4
  module.exports = fp(
6
5
  async (fastify, options) => {
7
- // 初始化配置管理器,合并默认配置和用户配置
8
- const configManager = getConfigManager(options);
9
- options = configManager.getAll();
6
+ options = Object.assign(
7
+ {},
8
+ {
9
+ dbTableNamePrefix: 't_',
10
+ prefix: '/api/task',
11
+ name: 'task',
12
+ limit: 10,
13
+ dir: path.resolve(process.cwd(), 'libs', 'tasks'),
14
+ /** 任务目录列表,运行时可通过 addDir 动态添加 */
15
+ dirs: null,
16
+ cronTime: '*/10 * * * *',
17
+ scriptName: 'index',
18
+ maxPollTimes: 20,
19
+ pollInterval: 10000,
20
+ /** 任务执行超时时间(毫秒),0 表示不超时,默认 1800000ms */
21
+ taskTimeout: 30 * 60 * 1000,
22
+ /** 启动时仅恢复超出该时间窗口的 running 任务,避免多实例重启误重置正在执行的任务 */
23
+ recoverRunningTaskAfter: null,
24
+ /** 是否在启动时恢复陈旧 running 任务 */
25
+ recoverRunningTasksOnStart: true,
26
+ /** 重试基础延迟(毫秒),实际延迟 = retryBaseDelay * 2^(retryCount-1) */
27
+ retryBaseDelay: 5000,
28
+ getUserModel: () => {
29
+ return fastify.account.models.user;
30
+ },
31
+ getAuthenticate: () => {
32
+ return [fastify.account.authenticate.user, fastify.account.authenticate.admin];
33
+ },
34
+ task: {}
35
+ },
36
+ options
37
+ );
38
+
39
+ if (options.recoverRunningTaskAfter == null) {
40
+ options.recoverRunningTaskAfter = options.taskTimeout > 0 ? options.taskTimeout : 30 * 60 * 1000;
41
+ }
42
+
43
+ // 初始化 dirs:优先使用用户传入的 dirs,否则以 dir 为默认值,保证向后兼容
44
+ if (!options.dirs) {
45
+ options.dirs = [options.dir];
46
+ } else if (!options.dirs.includes(options.dir)) {
47
+ options.dirs = [options.dir, ...options.dirs];
48
+ }
10
49
 
11
50
  fastify.register(require('@kne/fastify-statistics'), {
12
51
  dbTableNamePrefix: options.dbTableNamePrefix,
@@ -41,9 +80,11 @@ module.exports = fp(
41
80
  },
42
81
  start: true
43
82
  });
44
- //启动时,将running状态的任务设置为pending
83
+ // 启动时只恢复陈旧 running 任务,避免多实例部署时重置其他实例正在执行的任务
45
84
  fastify.addHook('onReady', async () => {
46
- await fastify[options.name].services.resetAll();
85
+ if (options.recoverRunningTasksOnStart) {
86
+ await fastify[options.name].services.resetAll({ staleOnly: true });
87
+ }
47
88
  });
48
89
  }
49
90
  })
@@ -2,7 +2,6 @@ const fp = require('fastify-plugin');
2
2
 
3
3
  module.exports = fp(async (fastify, options) => {
4
4
  const { services } = fastify[options.name];
5
-
6
5
  // #14: 新增创建任务的 REST 接口
7
6
  fastify.post(
8
7
  `${options.prefix}/create`,
@@ -56,7 +55,7 @@ module.exports = fp(async (fastify, options) => {
56
55
  },
57
56
  timeout: {
58
57
  type: 'number',
59
- description: '任务超时时间(分钟),0表示不超时,默认60分钟'
58
+ description: '任务超时时间(毫秒),0表示不超时,默认3600000ms'
60
59
  }
61
60
  },
62
61
  required: ['type', 'targetId', 'targetType']
@@ -45,8 +45,8 @@ module.exports = ({ DataTypes, definePrimaryType, options }) => {
45
45
  },
46
46
  timeout: {
47
47
  type: DataTypes.INTEGER,
48
- comment: '任务超时时间(分钟),0表示不超时,默认60分钟',
49
- defaultValue: 60
48
+ comment: '任务超时时间(毫秒),0表示不超时,默认3600000ms',
49
+ defaultValue: 60 * 60 * 1000
50
50
  },
51
51
  startTime: {
52
52
  type: DataTypes.DATE,