@kne/fastify-task 0.1.5 → 0.1.6

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.
@@ -48,53 +48,61 @@ module.exports = fp(async (fastify, options) => {
48
48
  status: 'running',
49
49
  progress: 0
50
50
  });
51
- require(taskModulePath)(fastify, options, {
52
- task,
53
- progress: async progress => {
54
- await task.update({
55
- progress
56
- });
57
- },
58
- polling: async (callback, currentOptions) => {
59
- let pollCount = 0;
60
- const maxPollTimes = currentOptions?.maxPollTimes || options.maxPollTimes;
61
- const pollInterval = currentOptions?.pollInterval || options.pollInterval;
62
- return await new Promise((resolve, reject) => {
63
- const timer = setInterval(async () => {
64
- pollCount++;
65
- if (pollCount > maxPollTimes) {
66
- clearInterval(timer);
67
- reject(`轮询超时(${maxPollTimes}次),任务未完成`);
68
- }
69
- const { result, data, message } = Object.assign({}, await callback());
70
- if (result === 'failed') {
71
- clearInterval(timer);
72
- reject(`任务处理失败:${message}`);
73
- }
74
- if (result === 'success') {
75
- clearInterval(timer);
76
- resolve(data);
77
- }
78
- }, pollInterval);
79
- });
80
- }
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
- completedAt: new Date()
89
- });
51
+ 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
+ }
90
82
  })
91
- .catch(error => {
92
- return task.update({
93
- status: 'failed',
94
- error: error.toString(),
95
- completedAt: new Date()
83
+ .then(async result => {
84
+ await options.task[task.type]({ task, result });
85
+ await task.update({
86
+ status: 'success',
87
+ output: result,
88
+ progress: 100,
89
+ completedAt: new Date()
90
+ });
91
+ })
92
+ .catch(error => {
93
+ return task.update({
94
+ status: 'failed',
95
+ error: error.toString(),
96
+ completedAt: new Date()
97
+ });
96
98
  });
99
+ } catch (error) {
100
+ await task.update({
101
+ status: 'failed',
102
+ error: error.toString(),
103
+ completedAt: new Date()
97
104
  });
105
+ }
98
106
  };
99
107
 
100
108
  const runner = async () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-task",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "description": "用于编排任务,追踪任务执行状态,结果",
5
5
  "main": "index.js",
6
6
  "scripts": {