multi-tasks 1.2.7 → 2.0.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.
Files changed (43) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +137 -97
  3. package/cmd.js +42 -0
  4. package/examples/example0/run-readme.js +88 -0
  5. package/examples/example0/run.js +52 -37
  6. package/examples/example1/RunnerConsumer.js +25 -25
  7. package/examples/example1/RunnerProducer.js +34 -34
  8. package/examples/example1/run.js +8 -8
  9. package/examples/example2/RunnerConsumer.js +39 -39
  10. package/examples/example2/RunnerProducer.js +34 -34
  11. package/examples/example2/run.js +22 -22
  12. package/examples/example3-dynamicIncreasing/RunnerConsumer.js +175 -175
  13. package/examples/example3-dynamicIncreasing/RunnerProducer.js +48 -48
  14. package/examples/example3-dynamicIncreasing/run.js +35 -35
  15. package/examples/example3-dynamicIncreasing/util.js +45 -45
  16. package/index.js +3 -36
  17. package/monitors/cli.js +28 -0
  18. package/package.json +23 -22
  19. package/test/splitter.test.js +16 -16
  20. package/todo.txt +4 -0
  21. package/utils/asyncQueue.js +32 -0
  22. package/utils/backward-v1.js +11 -0
  23. package/utils/makedir.js +55 -24
  24. package/utils/randoms.js +15 -0
  25. package/utils/test/test_asyncqueue.js +116 -0
  26. package/utils/test_makedir.js +2 -2
  27. package/workers/Multithread.js +175 -0
  28. package/workers/TaskMgr.js +186 -0
  29. package/workers/WorkerMgr.js +31 -0
  30. package/workers/asMaster.js +71 -0
  31. package/workers/asWorker.js +14 -0
  32. package/workers/helper.js +22 -0
  33. package/workers/index.js +8 -0
  34. package/workers/SuperConsumer.js +0 -106
  35. package/workers/SuperProducer.js +0 -97
  36. package/workers/master.js +0 -171
  37. package/workers/report/report_status.js +0 -52
  38. package/workers/taskSplitter.js +0 -30
  39. package/workers/test/MyConsumer.js +0 -25
  40. package/workers/test/MyProducer.js +0 -26
  41. package/workers/test/test_master.js +0 -12
  42. package/workers/test/test_producer.js +0 -4
  43. package/workers/test/test_splitter.js +0 -7
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,97 +1,137 @@
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
- //You need to provide the data of all subtasks, multi-tasks will automatically split them and execute them in a multi-threaded manner
16
- let alltasks = [];
17
- for(let i=0;i<100;i++){
18
- let task_props = {
19
- index: i,
20
- name: `task-${i}`,
21
- description: `This prop is for a subtask`
22
- };
23
- alltasks.push(task_props);
24
- }
25
-
26
- //You need to provide a process function to handle a certain sub-task and return the result data
27
- let processTask = (task)=>{
28
- console.log('this is the subtask data you created above:', task);
29
- let {index} = task;
30
-
31
- //any exceptions will be captured
32
- if(index===3) throw 'exception';
33
- if(index===4) return Promise.reject({err:'a test error'});
34
-
35
- //the returned data will be saved
36
- return Promise.resolve({
37
- message: 'this is a result from a demo, random data=' + Math.random()
38
- });
39
- }
40
-
41
- multiTasks({
42
- tasks: alltasks,
43
- processTask,
44
- multi_task_parent_folder: `C:/tmp-demo`, //a directory to store progress and results files, you can check the progress here
45
- quotaOfEachTask:1, //The number of subtasks assigned to each worker at one time, the default is 1
46
- numberOfWorkers: 6, //Assign how many workers are working in parallel, default is the number of cpu cores minus one
47
- onFinish: (report)=>{
48
- console.log(report);//the report can be viewed here
49
- }
50
- });
51
-
52
- ```
53
-
54
- Or the older way, to provide the Producer & Consumer
55
-
56
- ```javascript
57
- let master = require('multi-tasks').master;
58
- let RunnerProducer;//See examples, please provide a Producer class to specify all tasks you have, multi-tasks will automatically split them and execute them in a multi-threaded manner
59
- let RunnerConsumer;//See examples, please provide a Consumer class to process a certain sub-task and return the processed data
60
-
61
- master.start(RunnerProducer, RunnerConsumer, {
62
- multi_task_parent_folder,
63
- quotaOfEachTask,
64
- numberOfWorkers,
65
- onFinish
66
- });
67
- ```
68
-
69
- Have a try:
70
-
71
- ```shell
72
- node examples/example0/run
73
- node examples/example1/run
74
- ```
75
-
76
- Changelog:
77
-
78
- - 1.2.7 Small updates
79
- - 1.2.6 Support onFinish event
80
- - 1.2.5 Rename numberOfWorks to numberOfWorkers, the old one are still supported ;-)
81
- - 1.2.4 Fix: opt.numberOfWorkers not work
82
- - 1.2.3 Update README
83
- - 1.2.2 Update README and examples
84
- - 1.2.1 Handle exceptions and errors in subtasks
85
- - 1.2.0 Simplified usage by providing the function way and support return Promise
86
- - 1.1.4 Remove make-dir
87
- - 1.1.3 Simplified usage, see example0
88
- - 1.1.2
89
- - 1.1.1 Rename files, updated changelog
90
- - 1.1.0 Simplified the usage of a customized Consumer, see example0
91
- - 1.0.8 Fix examples
92
- - 1.0.7 Remove moment
93
- - 1.0.6 Performance optimization
94
-
95
- Github:
96
-
97
- [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
+ //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({
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
+ onFinish: (report)=>{
101
+ console.log('finish callback', report);
102
+ }
103
+ });
104
+
105
+ ```
106
+
107
+ Have a try:
108
+
109
+ ```shell
110
+ node examples/example0/run
111
+ node examples/example1/run
112
+ ```
113
+
114
+ Changelog:
115
+
116
+ - 2.0.0 Rewritten with a new architecture to support dynamic tasks.
117
+ - 1.2.8 Fix: create task folder failed on MacOS
118
+ - 1.2.7 Small updates
119
+ - 1.2.6 Support onFinish event
120
+ - 1.2.5 Rename numberOfWorks to numberOfWorkers, the old one are still supported ;-)
121
+ - 1.2.4 Fix: opt.numberOfWorkers not work
122
+ - 1.2.3 Update README
123
+ - 1.2.2 Update README and examples
124
+ - 1.2.1 Handle exceptions and errors in subtasks
125
+ - 1.2.0 Simplified usage by providing the function way and support return Promise
126
+ - 1.1.4 Remove make-dir
127
+ - 1.1.3 Simplified usage, see example0
128
+ - 1.1.2
129
+ - 1.1.1 Rename files, updated changelog
130
+ - 1.1.0 Simplified the usage of a customized Consumer, see example0
131
+ - 1.0.8 Fix examples
132
+ - 1.0.7 Remove moment
133
+ - 1.0.6 Performance optimization
134
+
135
+ Github:
136
+
137
+ [https://github.com/zhanglei923/multi-tasks](https://github.com/zhanglei923/multi-tasks)
package/cmd.js ADDED
@@ -0,0 +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,以便稍后清除它
42
+ let intervalId = setInterval(updateAndDisplayValues, 1000);
@@ -0,0 +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({
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
+ });
@@ -1,38 +1,53 @@
1
- let multiTasks = require('../../index').multiTasks;
2
-
3
- //You need to provide the data of all subtasks, multi-tasks will automatically split them and execute them in a multi-threaded manner
4
- let alltasks = [];
5
- for(let i=0;i<100;i++){
6
- let task_props = {
7
- index: i,
8
- name: `task-${i}`,
9
- description: `This prop is for a subtask`
10
- };
11
- alltasks.push(task_props);
12
- }
13
-
14
- //You need to provide a process function to handle a certain sub-task and return the result data
15
- let processTask = (task)=>{
16
- console.log('this is the subtask data you created above:', task);
17
- let {index} = task;
18
-
19
- //any exceptions will be captured
20
- if(index===3) throw 'exception';
21
- if(index===4) return Promise.reject({err:'a test error'});
22
-
23
- //the returned data will be saved
24
- return Promise.resolve({
25
- message: 'this is a result from a demo, random data=' + Math.random()
26
- });
27
- }
28
-
29
- multiTasks({
30
- tasks: alltasks,
31
- processTask,
32
- multi_task_parent_folder: `C:/tmp-demo`, //a directory to store progress and results files, you can check the progress here
33
- quotaOfEachTask:1, //The number of subtasks assigned to each worker at one time, the default is 1
34
- numberOfWorkers: 6, //Assign how many workers are working in parallel
35
- onFinish: (report)=>{
36
- console.log(report);
37
- }
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
+ let task_props = {
7
+ index: i,
8
+ name: `task-${i}`,
9
+ description: `This prop is for a subtask`
10
+ };
11
+ alltasks.push(task_props);
12
+ }
13
+
14
+ //Step2, Provide a process function to handle a certain sub-task and return the result data
15
+ let processTask = (task, helper)=>{
16
+ //console.log('this is the subtask data you created above:', task);
17
+ let {taskCount} = task;
18
+
19
+ console.log(taskCount)
20
+
21
+
22
+ //This is the demo of exceptions/errors, they will be captured and saved in the results/errors folder
23
+ if(taskCount===3) throw 'exception';
24
+ if(taskCount===4) return Promise.reject({err:'a test error'});
25
+ if(taskCount===5) aaa = bbb;
26
+
27
+ if(taskCount===6) return 'a result which is not a promise'; //Return a non-promise result is also OK
28
+
29
+ //by default, you should return a promise, all returned data can be found in the results/succ folder
30
+ return new Promise((resolve, reject)=>{
31
+ setTimeout(()=>{
32
+ if(taskCount % 2 === 0)helper.createNewTasks({//
33
+ msg:'a new task'
34
+ });
35
+ taskCount === 2 ? reject('a rejected error') : resolve({
36
+ message: 'this is a result from a demo, random data=' + Math.random()
37
+ });
38
+ }, 10 * Math.random())
39
+ })
40
+ }
41
+
42
+ //Step3, run!
43
+ multiTasks({
44
+ tasks: alltasks,
45
+ processTask,
46
+ taskRootFolder: `../examples-tmp-data/example0`, //a directory to store progress and results files, you can check the progress here
47
+ taskId: 'my-task',
48
+ numberOfWorkers: 3, //Assign how many workers are working in parallel
49
+ //autoCloseAfterCompletion: true,
50
+ onFinish: (report)=>{
51
+ console.log('finish callback', report);
52
+ }
38
53
  });
@@ -1,26 +1,26 @@
1
- let SuperConsumer = require('../../index').SuperConsumer;
2
-
3
- class TestConsumer extends SuperConsumer {
4
-
5
- processTask(actionData){
6
- console.log(`processing data:`, actionData);
7
- //you can also return a data instead of a promise object
8
- let {index} = actionData;
9
- if(index===3) throw 'exception';
10
- if(index===4) return Promise.reject({err:'a test error'});
11
- return new Promise((resolve) => {
12
- setTimeout(() => {
13
- let result = {
14
- task:{
15
- ...actionData,
16
- },
17
- result: {
18
- message: 'this is a result from a demo, random data=' + Math.random()
19
- }
20
- };
21
- resolve(result);
22
- }, 100);
23
- });
24
- }
25
- }
1
+ let SuperConsumer = require('../../index').SuperConsumer;
2
+
3
+ class TestConsumer extends SuperConsumer {
4
+
5
+ processTask(actionData){
6
+ console.log(`processing data:`, actionData);
7
+ //you can also return a data instead of a promise object
8
+ let {index} = actionData;
9
+ if(index===3) throw 'exception';
10
+ if(index===4) return Promise.reject({err:'a test error'});
11
+ return new Promise((resolve) => {
12
+ setTimeout(() => {
13
+ let result = {
14
+ task:{
15
+ ...actionData,
16
+ },
17
+ result: {
18
+ message: 'this is a result from a demo, random data=' + Math.random()
19
+ }
20
+ };
21
+ resolve(result);
22
+ }, 100);
23
+ });
24
+ }
25
+ }
26
26
  module.exports = TestConsumer;
@@ -1,35 +1,35 @@
1
- let SuperProducer = require('../../index').SuperProducer;
2
- class TestProducer extends SuperProducer {
3
-
4
- // constructor
5
- constructor(taskname, parentfolder) {
6
- super(taskname, parentfolder)
7
-
8
- }
9
- setTaskTrunkInfo(trunkInfo){
10
- this.trunkInfo = trunkInfo;
11
- }
12
- getAllActions() {
13
- /**
14
- * Generate all your tasks in this method, return as an array;
15
- * These data will be allocated to multiple threads (workers) for running;
16
- * You can obtain each of the data and process it in the 'processTask' method of your consumer.
17
- *
18
- * **/
19
-
20
- return new Promise((resolve, reject)=>{
21
- let trunkInfo = this.trunkInfo;
22
- let alltasks = []
23
- for(let i=0;i<200;i++){
24
- let task_props = {
25
- index: i,
26
- name: `task-${i}`,
27
- description: `This prop is for a subtask`
28
- };
29
- alltasks.push(task_props);
30
- }
31
- resolve(alltasks);//return all your tasks as an array
32
- })
33
- }
34
- }
1
+ let SuperProducer = require('../../index').SuperProducer;
2
+ class TestProducer extends SuperProducer {
3
+
4
+ // constructor
5
+ constructor(taskname, parentfolder) {
6
+ super(taskname, parentfolder)
7
+
8
+ }
9
+ setTaskTrunkInfo(trunkInfo){
10
+ this.trunkInfo = trunkInfo;
11
+ }
12
+ getAllActions() {
13
+ /**
14
+ * Generate all your tasks in this method, return as an array;
15
+ * These data will be allocated to multiple threads (workers) for running;
16
+ * You can obtain each of the data and process it in the 'processTask' method of your consumer.
17
+ *
18
+ * **/
19
+
20
+ return new Promise((resolve, reject)=>{
21
+ let trunkInfo = this.trunkInfo;
22
+ let alltasks = []
23
+ for(let i=0;i<200;i++){
24
+ let task_props = {
25
+ index: i,
26
+ name: `task-${i}`,
27
+ description: `This prop is for a subtask`
28
+ };
29
+ alltasks.push(task_props);
30
+ }
31
+ resolve(alltasks);//return all your tasks as an array
32
+ })
33
+ }
34
+ }
35
35
  module.exports = TestProducer;
@@ -1,9 +1,9 @@
1
- let RunnerProducer = require('./RunnerProducer');//You need to provide a Producer to return all subtasks, multi-tasks will automatically split them and execute them in a multi-threaded manner
2
- let RunnerConsumer = require('./RunnerConsumer');//You need to provide a Consumer to process a certain sub-task and return the processed data
3
- let master = require('../../index').master;
4
-
5
- master.start(RunnerProducer, RunnerConsumer, {
6
- multi_task_parent_folder: `C:/tmp-demo`,
7
- quotaOfEachTask:2, //The number of tasks assigned to each worker
8
- numberOfWorkers: 6
1
+ let RunnerProducer = require('./RunnerProducer');//You need to provide a Producer to return all subtasks, multi-tasks will automatically split them and execute them in a multi-threaded manner
2
+ let RunnerConsumer = require('./RunnerConsumer');//You need to provide a Consumer to process a certain sub-task and return the processed data
3
+ let master = require('../../index').master;
4
+
5
+ master.start(RunnerProducer, RunnerConsumer, {
6
+ multi_task_parent_folder: `C:/tmp-demo`,
7
+ quotaOfEachTask:2, //The number of tasks assigned to each worker
8
+ numberOfWorkers: 6
9
9
  });