@kne/fastify-task 0.1.0-alpha.1 → 0.1.0

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,62 +1,69 @@
1
1
  const fp = require('fastify-plugin');
2
2
  const path = require('node:path');
3
3
 
4
- module.exports = fp(async (fastify, options) => {
5
- options = Object.assign(
6
- {},
7
- {
8
- dbTableNamePrefix: 't_',
9
- prefix: '/api/task',
10
- name: 'task',
11
- limit: 10,
12
- dir: path.resolve(process.cwd(), 'libs', 'tasks'),
13
- syncCron: '*/10 * * * *',
14
- scriptName: 'index',
15
- maxPollTimes: 100,
16
- pollInterval: 5000,
17
- getUserModel: () => {
18
- return fastify.account.user;
4
+ module.exports = fp(
5
+ async (fastify, options) => {
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
+ cronTime: '*/10 * * * *',
15
+ scriptName: 'index',
16
+ maxPollTimes: 100,
17
+ pollInterval: 5000,
18
+ getUserModel: () => {
19
+ return fastify.account.models.user;
20
+ },
21
+ getAuthenticate: () => {
22
+ return [fastify.account.authenticate.user, fastify.account.authenticate.admin];
23
+ },
24
+ task: {}
19
25
  },
20
- getAuthenticate: () => {
21
- return [fastify.account.authenticate.user, fastify.account.authenticate.admin];
22
- },
23
- task: {}
24
- },
25
- options
26
- );
26
+ options
27
+ );
27
28
 
28
- fastify.register(require('@kne/fastify-namespace'), {
29
- options,
30
- name: options.name,
31
- modules: [
32
- ['controllers', path.resolve(__dirname, './libs/controllers')],
33
- [
34
- 'models',
35
- await fastify.sequelize.addModels(path.resolve(__dirname, './libs/models'), {
36
- prefix: options.dbTableNamePrefix,
37
- getUserModel: options.getUserModel,
38
- modelPrefix: options.name
39
- })
40
- ],
41
- ['services', path.resolve(__dirname, './libs/services')]
42
- ]
43
- });
29
+ fastify.register(require('@kne/fastify-namespace'), {
30
+ options,
31
+ name: options.name,
32
+ modules: [
33
+ ['controllers', path.resolve(__dirname, './libs/controllers')],
34
+ [
35
+ 'models',
36
+ await fastify.sequelize.addModels(path.resolve(__dirname, './libs/models'), {
37
+ prefix: options.dbTableNamePrefix,
38
+ getUserModel: options.getUserModel,
39
+ modelPrefix: options.name
40
+ })
41
+ ],
42
+ ['services', path.resolve(__dirname, './libs/services')]
43
+ ]
44
+ });
44
45
 
45
- fastify.register(
46
- require('fastify-plugin')(async fastify => {
47
- options.syncCron &&
48
- fastify.register(require('fastify-cron'), {
49
- jobs: [
50
- {
51
- cronTime: options.syncCron,
52
- onTick: async () => {
53
- const { runner } = fastify[options.name].services;
54
- await runner();
55
- },
56
- start: true
57
- }
58
- ]
59
- });
60
- })
61
- );
62
- });
46
+ fastify.register(
47
+ require('fastify-plugin')(async fastify => {
48
+ if (options.cronTime) {
49
+ fastify.cron.createJob({
50
+ cronTime: options.cronTime,
51
+ onTick: async () => {
52
+ const { runner } = fastify[options.name].services;
53
+ await runner();
54
+ },
55
+ start: true
56
+ });
57
+ //启动时,将running状态的任务设置为pending
58
+ fastify.addHook('onReady', async () => {
59
+ await fastify[options.name].services.resetAll();
60
+ });
61
+ }
62
+ })
63
+ );
64
+ },
65
+ {
66
+ name: 'fastify-task',
67
+ dependencies: ['fastify-cron']
68
+ }
69
+ );
@@ -1,11 +1,11 @@
1
1
  const fp = require('fastify-plugin');
2
2
  const path = require('node:path');
3
- const fs = require('node:fs/promises');
3
+ const fs = require('fs-extra');
4
4
 
5
5
  module.exports = fp(async (fastify, options) => {
6
6
  const { models } = fastify[options.name];
7
7
  const { Op } = fastify.sequelize.Sequelize;
8
- const create = async ({ userId, input, type, targetId, targetType, runnerType, delay, scriptName, options }) => {
8
+ const create = async ({ userId, input, type, targetId, targetType, runnerType, delay, scriptName, options: currentOptions }) => {
9
9
  if (typeof options.task[type] !== 'function') {
10
10
  throw new Error('未找到合法的任务声明');
11
11
  }
@@ -18,17 +18,31 @@ module.exports = fp(async (fastify, options) => {
18
18
  runnerType,
19
19
  startTime: delay > 0 ? new Date(Date.now() + 1000 * delay) : new Date(),
20
20
  scriptName,
21
- options
21
+ options: currentOptions
22
22
  });
23
23
  };
24
24
 
25
+ const resetAll = async () => {
26
+ await models.task.update(
27
+ {
28
+ status: 'pending'
29
+ },
30
+ {
31
+ where: {
32
+ status: 'running'
33
+ }
34
+ }
35
+ );
36
+ };
37
+
25
38
  const processSystemTask = async task => {
26
- const taskModulePath = path.resolve(options.dir, task.scriptName || options.scriptName);
39
+ const taskModulePath = path.resolve(options.dir, task.type, `${task.scriptName || options.scriptName}.js`);
27
40
  if (!(await fs.exists(taskModulePath))) {
28
41
  await task.update({
29
42
  status: 'failed',
30
- error: '未匹配到任务执行器'
43
+ error: `未匹配到任务执行器:${taskModulePath}`
31
44
  });
45
+ return;
32
46
  }
33
47
  await task.update({
34
48
  status: 'running',
@@ -41,45 +55,46 @@ module.exports = fp(async (fastify, options) => {
41
55
  progress
42
56
  });
43
57
  },
44
- complete: async result => {
45
- await options.task[task.type]({ task, result });
46
- await task.update({
47
- status: 'success',
48
- output: result,
49
- progress: 100,
50
- completeAt: new Date()
51
- });
52
- },
53
58
  polling: async (callback, currentOptions) => {
54
59
  let pollCount = 0;
55
- const maxPollTimes = currentOptions.maxPollTimes || options.maxPollTimes;
56
- const pollInterval = currentOptions.pollInterval || options.pollInterval;
57
- await new Promise((resolve, reject) => {
60
+ const maxPollTimes = currentOptions?.maxPollTimes || options.maxPollTimes;
61
+ const pollInterval = currentOptions?.pollInterval || options.pollInterval;
62
+ return await new Promise((resolve, reject) => {
58
63
  const timer = setInterval(async () => {
59
64
  pollCount++;
60
65
  if (pollCount > maxPollTimes) {
61
66
  clearInterval(timer);
62
67
  reject(`轮询超时(${maxPollTimes}次),任务未完成`);
63
68
  }
64
- const { result, message } = await callback();
69
+ const { result, data, message } = Object.assign({}, await callback());
65
70
  if (result === 'failed') {
66
71
  clearInterval(timer);
67
72
  reject(`任务处理失败:${message}`);
68
73
  }
69
74
  if (result === 'success') {
70
75
  clearInterval(timer);
71
- resolve();
76
+ resolve(data);
72
77
  }
73
78
  }, pollInterval);
74
79
  });
75
80
  }
76
- }).catch(error => {
77
- return task.update({
78
- status: 'failed',
79
- error: error.toString(),
80
- completeAt: new Date()
81
+ })
82
+ .then(async result => {
83
+ await options.task[task.type]({ task, result });
84
+ await task.update({
85
+ status: 'success',
86
+ output: result,
87
+ progress: 100,
88
+ completeAt: new Date()
89
+ });
90
+ })
91
+ .catch(error => {
92
+ return task.update({
93
+ status: 'failed',
94
+ error: error.toString(),
95
+ completeAt: new Date()
96
+ });
81
97
  });
82
- });
83
98
  };
84
99
 
85
100
  const runner = async () => {
@@ -160,6 +175,7 @@ module.exports = fp(async (fastify, options) => {
160
175
  list,
161
176
  complete,
162
177
  cancel,
163
- runner
178
+ runner,
179
+ resetAll
164
180
  });
165
181
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-task",
3
- "version": "0.1.0-alpha.1",
3
+ "version": "0.1.0",
4
4
  "description": "用于编排任务,追踪任务执行状态,结果",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -47,6 +47,7 @@
47
47
  "sqlite3": "^5.1.7"
48
48
  },
49
49
  "dependencies": {
50
- "fastify-cron": "^1.4.0"
50
+ "fastify-cron": "^1.4.0",
51
+ "fs-extra": "^11.3.0"
51
52
  }
52
53
  }