@kne/fastify-task 0.1.8 → 0.1.10

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.
@@ -35,51 +35,48 @@ module.exports = fp(async (fastify, options) => {
35
35
  );
36
36
  };
37
37
 
38
- const processSystemTask = async task => {
39
- const taskModulePath = path.resolve(options.dir, task.type, `${task.scriptName || options.scriptName}.js`);
38
+ const executor = async ({ type, scriptName, ...props }) => {
39
+ const taskModulePath = path.resolve(options.dir, type, `${scriptName || options.scriptName}.js`);
40
40
  if (!(await fs.exists(taskModulePath))) {
41
- await task.update({
42
- status: 'failed',
43
- error: `未匹配到任务执行器:${taskModulePath}`
44
- });
45
- return;
41
+ throw new Error(`未匹配到任务执行器:${taskModulePath}`);
46
42
  }
47
- await task.update({
48
- status: 'running',
49
- progress: 0
43
+ return await require(taskModulePath)(fastify, options, {
44
+ ...props,
45
+ polling: async (callback, currentOptions) => {
46
+ let pollCount = 0;
47
+ const maxPollTimes = currentOptions?.maxPollTimes || options.maxPollTimes;
48
+ const pollInterval = currentOptions?.pollInterval || options.pollInterval;
49
+ return await new Promise((resolve, reject) => {
50
+ const timer = setInterval(async () => {
51
+ pollCount++;
52
+ if (pollCount > maxPollTimes) {
53
+ clearInterval(timer);
54
+ reject(`轮询超时(${maxPollTimes}次),任务未完成`);
55
+ }
56
+ const { result, data, message } = Object.assign({}, await callback());
57
+ if (result === 'failed') {
58
+ clearInterval(timer);
59
+ reject(`任务处理失败:${message}`);
60
+ }
61
+ if (result === 'success') {
62
+ clearInterval(timer);
63
+ resolve(data);
64
+ }
65
+ }, pollInterval);
66
+ });
67
+ }
50
68
  });
69
+ };
70
+
71
+ const processSystemTask = async task => {
51
72
  try {
52
- require(taskModulePath)(fastify, options, {
53
- task,
54
- progress: async progress => {
55
- await task.update({
56
- progress
57
- });
58
- },
59
- polling: async (callback, currentOptions) => {
60
- let pollCount = 0;
61
- const maxPollTimes = currentOptions?.maxPollTimes || options.maxPollTimes;
62
- const pollInterval = currentOptions?.pollInterval || options.pollInterval;
63
- return await new Promise((resolve, reject) => {
64
- const timer = setInterval(async () => {
65
- pollCount++;
66
- if (pollCount > maxPollTimes) {
67
- clearInterval(timer);
68
- reject(`轮询超时(${maxPollTimes}次),任务未完成`);
69
- }
70
- const { result, data, message } = Object.assign({}, await callback());
71
- if (result === 'failed') {
72
- clearInterval(timer);
73
- reject(`任务处理失败:${message}`);
74
- }
75
- if (result === 'success') {
76
- clearInterval(timer);
77
- resolve(data);
78
- }
79
- }, pollInterval);
80
- });
81
- }
82
- })
73
+ await task.update({
74
+ status: 'running',
75
+ progress: 0,
76
+ error: null,
77
+ output: null
78
+ });
79
+ executor({ type: task.type, scriptName: task.scriptName, task })
83
80
  .then(async result => {
84
81
  await options.task[task.type]({ task, result });
85
82
  await task.update({
@@ -89,17 +86,17 @@ module.exports = fp(async (fastify, options) => {
89
86
  completedAt: new Date()
90
87
  });
91
88
  })
92
- .catch(error => {
89
+ .catch(e => {
93
90
  return task.update({
94
91
  status: 'failed',
95
- error: error.stack,
92
+ error: e.stack,
96
93
  completedAt: new Date()
97
94
  });
98
95
  });
99
- } catch (error) {
96
+ } catch (e) {
100
97
  await task.update({
101
98
  status: 'failed',
102
- error: error.stack,
99
+ error: e.stack,
103
100
  completedAt: new Date()
104
101
  });
105
102
  }
@@ -149,8 +146,8 @@ module.exports = fp(async (fastify, options) => {
149
146
  return task;
150
147
  };
151
148
 
152
- const cancel = async ({ id, targetId, targetType }) => {
153
- if (targetId && targetType) {
149
+ const cancel = async ({ id, targetId, targetType, type }) => {
150
+ if (targetId && targetType && type) {
154
151
  return await models.task.update(
155
152
  {
156
153
  status: 'canceled'
@@ -159,6 +156,7 @@ module.exports = fp(async (fastify, options) => {
159
156
  where: {
160
157
  targetId,
161
158
  targetType,
159
+ type,
162
160
  status: {
163
161
  [Op.in]: ['pending', 'running']
164
162
  }
@@ -241,6 +239,7 @@ module.exports = fp(async (fastify, options) => {
241
239
  cancel,
242
240
  runner,
243
241
  resetAll,
244
- retry
242
+ retry,
243
+ executor
245
244
  });
246
245
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-task",
3
- "version": "0.1.8",
3
+ "version": "0.1.10",
4
4
  "description": "用于编排任务,追踪任务执行状态,结果",
5
5
  "main": "index.js",
6
6
  "scripts": {