multi-tasks 2.0.2 → 2.0.4

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/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2024 Lei Zhang
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Lei Zhang
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,140 +1,144 @@
1
- # multi-tasks
2
- multi-tasks
3
-
4
- Install:
5
-
6
- ```javascript
7
- npm install multi-tasks
8
- ```
9
-
10
- How to use:
11
-
12
- ```javascript
13
- //see examples/example0
14
- let multiTasks = require('multi-tasks').multiTasks;
15
-
16
-
17
- //Step1, If you have tasks that need to be executed simultaneously, please provide them as an array, multi-tasks will automatically split them and execute.
18
- let alltasks = [];
19
- for(let i=0;i<50;i++){
20
- alltasks.push({
21
- name: `task-${i}`,
22
- data: `This prop is for a subtask`
23
- });
24
- };
25
-
26
- //Step2, Provide a function to process a certain sub-task and return the result data
27
- let processTask = (task, helper)=>{
28
- let {taskCount} = task;
29
-
30
- return 'a result which is not a promise';
31
- };
32
-
33
- //Step2.1, or return a promise
34
- let processTask = (task, helper)=>{
35
- let {taskCount} = task;
36
-
37
- return new Promise((resolve, reject)=>{
38
- resolve({
39
- data:`task${taskCount} complete`
40
- })
41
- })
42
- };
43
-
44
- //Step2.2, dynamically create a new task if find something new while processing
45
- let processTask = (task, helper)=>{
46
- let {taskCount} = task;
47
-
48
-
49
- if(taskCount % 2 === 0){
50
- helper.createNewTasks({//You can dynamically create a new task if find something new while processing
51
- msg:'a new task'
52
- });
53
- return;
54
- }
55
-
56
- return new Promise((resolve, reject)=>{
57
- resolve({
58
- data:`task${taskCount} complete`
59
- })
60
- })
61
- };
62
-
63
- //Step2.3, this example shows how to handle exceptions/errors
64
- let processTask = (task, helper)=>{
65
- let {taskCount} = task;
66
-
67
- //This is the demo of exceptions/errors, they will be captured and saved in the results/errors folder
68
- if(taskCount===3) throw 'exception';
69
- if(taskCount===4) return Promise.reject({err:'a test error'});
70
- if(taskCount===5) aaa = bbb;
71
-
72
- if(taskCount===6) return 'a result which is not a promise'; //Return a non-promise result is OK
73
-
74
- //by default, you should return a promise, all returned data can be found in the results/succ folder
75
- //but to return a non-promise result is also OK, see above
76
- return new Promise((resolve, reject)=>{
77
- setTimeout(()=>{
78
- if(taskCount % 2 === 0){
79
- helper.createNewTasks({//You can dynamically create a new task if find something new while processing
80
- msg:'a new task'
81
- });
82
- resolve()
83
- }else{
84
- resolve({
85
- data:`task${taskCount} complete`
86
- })
87
- }
88
- }, 10)
89
- })
90
- }
91
-
92
- //Step3, run!
93
- multiTasks.start({
94
- tasks: alltasks,
95
- processTask,
96
- taskRootFolder: `../examples-tmp-data/example0`, //a directory to store progress and results files, you can check the progress here
97
- taskId: 'my-task',
98
- numberOfWorkers: 3, //Assign how many workers are working in parallel
99
- //autoCloseAfterCompletion: true, //if you have dynamically generated new tasks, put this as false
100
- shouldTerminate,//return true if you need to terminate the whole process
101
- onFinish: (report)=>{
102
- console.log('finish callback', report);
103
- }
104
- });
105
-
106
- ```
107
-
108
- Have a try:
109
-
110
- ```shell
111
- node examples/example0/run
112
- node examples/example1/run
113
- ```
114
-
115
- Changelog:
116
-
117
- - 2.0.2 new feature: support shouldTerminate
118
- - 2.0.1 Avoid possible I/O conflicts.
119
- - 2.0.0 Rewritten with a new architecture to support dynamic tasks.
120
- - 1.2.8 Fix: create task folder failed on MacOS
121
- - 1.2.7 Small updates
122
- - 1.2.6 Support onFinish event
123
- - 1.2.5 Rename numberOfWorks to numberOfWorkers, the old one are still supported ;-)
124
- - 1.2.4 Fix: opt.numberOfWorkers not work
125
- - 1.2.3 Update README
126
- - 1.2.2 Update README and examples
127
- - 1.2.1 Handle exceptions and errors in subtasks
128
- - 1.2.0 Simplified usage by providing the function way and support return Promise
129
- - 1.1.4 Remove make-dir
130
- - 1.1.3 Simplified usage, see example0
131
- - 1.1.2
132
- - 1.1.1 Rename files, updated changelog
133
- - 1.1.0 Simplified the usage of a customized Consumer, see example0
134
- - 1.0.8 Fix examples
135
- - 1.0.7 Remove moment
136
- - 1.0.6 Performance optimization
137
-
138
- Github:
139
-
140
- [https://github.com/zhanglei923/multi-tasks](https://github.com/zhanglei923/multi-tasks)
1
+ # multi-tasks
2
+ multi-tasks
3
+
4
+ Install:
5
+
6
+ ```javascript
7
+ npm install multi-tasks
8
+ ```
9
+
10
+ How to use:
11
+
12
+ ```javascript
13
+ //see examples/example0
14
+ let multiTasks = require('multi-tasks').multiTasks;
15
+
16
+
17
+ //Step1, If you have tasks that need to be executed simultaneously, please provide them as an array, multi-tasks will automatically split them and execute.
18
+ let alltasks = [];
19
+ for(let i=0;i<50;i++){
20
+ alltasks.push({
21
+ name: `task-${i}`,
22
+ data: `This prop is for a subtask`
23
+ });
24
+ };
25
+
26
+ //Step2, Provide a function to process a certain sub-task and return the result data
27
+ let processTask = (task, helper)=>{
28
+ let {taskCount} = task;
29
+
30
+ return 'a result which is not a promise';
31
+ };
32
+
33
+ //or return a promise
34
+ let processTask = (task, helper)=>{
35
+ let {taskCount} = task;
36
+
37
+ return new Promise((resolve, reject)=>{
38
+ resolve({
39
+ data:`task${taskCount} complete`
40
+ })
41
+ })
42
+ };
43
+
44
+ //or, dynamically create a new task if find something new while processing
45
+ let processTask = (task, helper)=>{
46
+ let {taskCount} = task;
47
+
48
+
49
+ if(taskCount % 2 === 0){
50
+ helper.createNewTasks({//You can dynamically create a new task if find something new while processing
51
+ msg:'a new task'
52
+ });
53
+ return;
54
+ }
55
+
56
+ return new Promise((resolve, reject)=>{
57
+ resolve({
58
+ data:`task${taskCount} complete`
59
+ })
60
+ })
61
+ };
62
+
63
+ //or, this example shows how to generate/handle exceptions
64
+ let processTask = (task, helper)=>{
65
+ let {taskCount} = task;
66
+
67
+ //This is the demo of exceptions/errors, they will be captured and saved in the results/errors folder
68
+ if(taskCount===3) throw 'exception';
69
+ if(taskCount===4) return Promise.reject({err:'a test error'});
70
+ if(taskCount===5) aaa = bbb;
71
+
72
+ if(taskCount===6) return 'a result which is not a promise'; //Return a non-promise result is OK
73
+
74
+ //by default, you should return a promise, all returned data can be found in the results/succ folder
75
+ //but to return a non-promise result is also OK, see above
76
+ return new Promise((resolve, reject)=>{
77
+ setTimeout(()=>{
78
+ if(taskCount % 2 === 0){
79
+ helper.createNewTasks({//You can dynamically create a new task if find something new while processing
80
+ msg:'a new task'
81
+ });
82
+ resolve()
83
+ }else{
84
+ resolve({
85
+ data:`task${taskCount} complete`
86
+ })
87
+ }
88
+ }, 10)
89
+ })
90
+ }
91
+
92
+ //Step3, run!
93
+ multiTasks({
94
+ initialTasks: alltasks,
95
+ processTask,
96
+ taskRootFolder: `../examples-tmp-data/example0`, //a directory to store progress and results files, you can check the progress here
97
+ taskId: 'my-task',
98
+ numberOfWorkers: 3, //Assign how many workers are working in parallel
99
+ //autoCloseAfterCompletion: true, //if you have dynamically generated new tasks, put this as false
100
+ shouldTerminate:(info)=>{
101
+ //return true if you need to terminate the whole process
102
+ },
103
+ onFinish: (report)=>{
104
+ console.log('finish callback', report);
105
+ }
106
+ });
107
+
108
+ ```
109
+
110
+ Have a try:
111
+
112
+ ```shell
113
+ node examples/example0/run
114
+ node examples/example1/run
115
+ ```
116
+
117
+ Changelog:
118
+
119
+ - 2.0.4 Support resume from a failed task
120
+ - 2.0.3 Fix: mkdir bug on windows
121
+ - 2.0.2 new feature: support shouldTerminate
122
+ - 2.0.1 Avoid possible I/O conflicts.
123
+ - 2.0.0 Rewritten with a new architecture to support dynamic tasks.
124
+ - 1.2.8 Fix: create task folder failed on MacOS
125
+ - 1.2.7 Small updates
126
+ - 1.2.6 Support onFinish event
127
+ - 1.2.5 Rename numberOfWorks to numberOfWorkers, the old one are still supported ;-)
128
+ - 1.2.4 Fix: opt.numberOfWorkers not work
129
+ - 1.2.3 Update README
130
+ - 1.2.2 Update README and examples
131
+ - 1.2.1 Handle exceptions and errors in subtasks
132
+ - 1.2.0 Simplified usage by providing the function way and support return Promise
133
+ - 1.1.4 Remove make-dir
134
+ - 1.1.3 Simplified usage, see example0
135
+ - 1.1.2
136
+ - 1.1.1 Rename files, updated changelog
137
+ - 1.1.0 Simplified the usage of a customized Consumer, see example0
138
+ - 1.0.8 Fix examples
139
+ - 1.0.7 Remove moment
140
+ - 1.0.6 Performance optimization
141
+
142
+ Github:
143
+
144
+ [https://github.com/zhanglei923/multi-tasks](https://github.com/zhanglei923/multi-tasks)
package/cmd.js CHANGED
@@ -1,42 +1,42 @@
1
- import chalk from 'chalk';
2
- import readline from 'readline';
3
-
4
-
5
- // 初始化数值
6
- let values = [0, 0, 0];
7
-
8
- // 更新并显示数值的函数
9
- function updateAndDisplayValues() {
10
- // 增加一些随机值
11
- values = values.map(value => value + Math.floor(Math.random() * 10));
12
-
13
- // 清除当前行并将光标移回行首
14
- process.stdout.clearLine();
15
- process.stdout.cursorTo(0);
16
-
17
- // 显示更新后的数值,不换行
18
- process.stdout.write(chalk.green(`Value 1: ${values[0]}\t`));
19
- process.stdout.write(chalk.yellow(`Value 2: ${values[1]}\t`));
20
- process.stdout.write(chalk.blue(`Value 3: ${values[2]}\r`)); // \r 将光标移回行首,准备下一次输出
21
- }
22
-
23
- // 创建readline接口实例以监听键盘输入
24
- const rl = readline.createInterface({
25
- input: process.stdin,
26
- output: process.stdout
27
- });
28
-
29
- // 每秒更新和显示数值
30
- setInterval(updateAndDisplayValues, 1000);
31
-
32
- // 监听'q'键退出
33
- rl.on('line', (input) => {
34
- if (input.trim().toLowerCase() === 'q') {
35
- clearInterval(intervalId); // 清除定时器
36
- rl.close(); // 关闭readline接口
37
- process.exit(0); // 退出程序
38
- }
39
- });
40
-
41
- // 存储定时器的ID,以便稍后清除它
1
+ import chalk from 'chalk';
2
+ import readline from 'readline';
3
+
4
+
5
+ // 初始化数值
6
+ let values = [0, 0, 0];
7
+
8
+ // 更新并显示数值的函数
9
+ function updateAndDisplayValues() {
10
+ // 增加一些随机值
11
+ values = values.map(value => value + Math.floor(Math.random() * 10));
12
+
13
+ // 清除当前行并将光标移回行首
14
+ process.stdout.clearLine();
15
+ process.stdout.cursorTo(0);
16
+
17
+ // 显示更新后的数值,不换行
18
+ process.stdout.write(chalk.green(`Value 1: ${values[0]}\t`));
19
+ process.stdout.write(chalk.yellow(`Value 2: ${values[1]}\t`));
20
+ process.stdout.write(chalk.blue(`Value 3: ${values[2]}\r`)); // \r 将光标移回行首,准备下一次输出
21
+ }
22
+
23
+ // 创建readline接口实例以监听键盘输入
24
+ const rl = readline.createInterface({
25
+ input: process.stdin,
26
+ output: process.stdout
27
+ });
28
+
29
+ // 每秒更新和显示数值
30
+ setInterval(updateAndDisplayValues, 1000);
31
+
32
+ // 监听'q'键退出
33
+ rl.on('line', (input) => {
34
+ if (input.trim().toLowerCase() === 'q') {
35
+ clearInterval(intervalId); // 清除定时器
36
+ rl.close(); // 关闭readline接口
37
+ process.exit(0); // 退出程序
38
+ }
39
+ });
40
+
41
+ // 存储定时器的ID,以便稍后清除它
42
42
  let intervalId = setInterval(updateAndDisplayValues, 1000);
@@ -1,88 +1,88 @@
1
- let multiTasks = require('../../index').multiTasks;
2
-
3
- //Step1, If you have tasks that need to be executed simultaneously, please provide them as an array, multi-tasks will automatically split them and execute.
4
- let alltasks = [];
5
- for(let i=0;i<50;i++){
6
- alltasks.push({
7
- name: `task-${i}`,
8
- data: `This prop is for a subtask`
9
- });
10
- };
11
-
12
- //Step2, Provide a function to process a certain sub-task and return the result data
13
- let processTask = (task, helper)=>{
14
- let {taskCount} = task;
15
-
16
- return 'a result which is not a promise';
17
- };
18
-
19
- //Step2.1, or return a promise
20
- let processTask = (task, helper)=>{
21
- let {taskCount} = task;
22
-
23
- return new Promise((resolve, reject)=>{
24
- resolve({
25
- data:`task${taskCount} complete`
26
- })
27
- })
28
- };
29
-
30
- //Step2.2, dynamically create a new task if find something new while processing
31
- let processTask = (task, helper)=>{
32
- let {taskCount} = task;
33
-
34
-
35
- if(taskCount % 2 === 0){
36
- helper.createNewTasks({//You can dynamically create a new task if find something new while processing
37
- msg:'a new task'
38
- });
39
- return;
40
- }
41
-
42
- return new Promise((resolve, reject)=>{
43
- resolve({
44
- data:`task${taskCount} complete`
45
- })
46
- })
47
- };
48
- //Step2.3, this example shows how to handle exceptions/errors
49
- let processTask = (task, helper)=>{
50
- let {taskCount} = task;
51
-
52
- //This is the demo of exceptions/errors, they will be captured and saved in the results/errors folder
53
- if(taskCount===3) throw 'exception';
54
- if(taskCount===4) return Promise.reject({err:'a test error'});
55
- if(taskCount===5) aaa = bbb;
56
-
57
- if(taskCount===6) return 'a result which is not a promise'; //Return a non-promise result is OK
58
-
59
- //by default, you should return a promise, all returned data can be found in the results/succ folder
60
- //but to return a non-promise result is also OK, see above
61
- return new Promise((resolve, reject)=>{
62
- setTimeout(()=>{
63
- if(taskCount % 2 === 0){
64
- helper.createNewTasks({//You can dynamically create a new task if find something new while processing
65
- msg:'a new task'
66
- });
67
- resolve()
68
- }else{
69
- resolve({
70
- data:`task${taskCount} complete`
71
- })
72
- }
73
- }, 10)
74
- })
75
- }
76
-
77
- //Step3, run!
78
- multiTasks.start({
79
- tasks: alltasks,
80
- processTask,
81
- taskRootFolder: `../examples-tmp-data/example0`, //a directory to store progress and results files, you can check the progress here
82
- taskId: 'my-task',
83
- numberOfWorkers: 3, //Assign how many workers are working in parallel
84
- //autoCloseAfterCompletion: true, //if you have dynamically generated new tasks, put this as false
85
- onFinish: (report)=>{
86
- console.log('finish callback', report);
87
- }
1
+ let multiTasks = require('../../index').multiTasks;
2
+
3
+ //Step1, If you have tasks that need to be executed simultaneously, please provide them as an array, multi-tasks will automatically split them and execute.
4
+ let alltasks = [];
5
+ for(let i=0;i<50;i++){
6
+ alltasks.push({
7
+ name: `task-${i}`,
8
+ data: `This prop is for a subtask`
9
+ });
10
+ };
11
+
12
+ //Step2, Provide a function to process a certain sub-task and return the result data
13
+ let processTask = (task, helper)=>{
14
+ let {taskCount} = task;
15
+
16
+ return 'a result which is not a promise';
17
+ };
18
+
19
+ //Step2.1, or return a promise
20
+ let processTask = (task, helper)=>{
21
+ let {taskCount} = task;
22
+
23
+ return new Promise((resolve, reject)=>{
24
+ resolve({
25
+ data:`task${taskCount} complete`
26
+ })
27
+ })
28
+ };
29
+
30
+ //Step2.2, dynamically create a new task if find something new while processing
31
+ let processTask = (task, helper)=>{
32
+ let {taskCount} = task;
33
+
34
+
35
+ if(taskCount % 2 === 0){
36
+ helper.createNewTasks({//You can dynamically create a new task if find something new while processing
37
+ msg:'a new task'
38
+ });
39
+ return;
40
+ }
41
+
42
+ return new Promise((resolve, reject)=>{
43
+ resolve({
44
+ data:`task${taskCount} complete`
45
+ })
46
+ })
47
+ };
48
+ //Step2.3, this example shows how to handle exceptions/errors
49
+ let processTask = (task, helper)=>{
50
+ let {taskCount} = task;
51
+
52
+ //This is the demo of exceptions/errors, they will be captured and saved in the results/errors folder
53
+ if(taskCount===3) throw 'exception';
54
+ if(taskCount===4) return Promise.reject({err:'a test error'});
55
+ if(taskCount===5) aaa = bbb;
56
+
57
+ if(taskCount===6) return 'a result which is not a promise'; //Return a non-promise result is OK
58
+
59
+ //by default, you should return a promise, all returned data can be found in the results/succ folder
60
+ //but to return a non-promise result is also OK, see above
61
+ return new Promise((resolve, reject)=>{
62
+ setTimeout(()=>{
63
+ if(taskCount % 2 === 0){
64
+ helper.createNewTasks({//You can dynamically create a new task if find something new while processing
65
+ msg:'a new task'
66
+ });
67
+ resolve()
68
+ }else{
69
+ resolve({
70
+ data:`task${taskCount} complete`
71
+ })
72
+ }
73
+ }, 10)
74
+ })
75
+ }
76
+
77
+ //Step3, run!
78
+ multiTasks.start({
79
+ tasks: alltasks,
80
+ processTask,
81
+ taskRootFolder: `../examples-tmp-data/example0`, //a directory to store progress and results files, you can check the progress here
82
+ taskId: 'my-task',
83
+ numberOfWorkers: 3, //Assign how many workers are working in parallel
84
+ //autoCloseAfterCompletion: true, //if you have dynamically generated new tasks, put this as false
85
+ onFinish: (report)=>{
86
+ console.log('finish callback', report);
87
+ }
88
88
  });