multi-tasks 1.1.4 → 1.2.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/README.md +21 -3
- package/examples/example0/run.js +26 -0
- package/examples/example1/RunnerConsumer.js +23 -0
- package/{example1 → examples/example1}/RunnerProducer.js +1 -1
- package/{example0 → examples/example1}/run.js +1 -1
- package/{example1 → examples/example2}/RunnerConsumer.js +1 -1
- package/{example0 → examples/example2}/RunnerProducer.js +1 -1
- package/{example1 → examples/example2}/run.js +1 -1
- package/index.js +31 -2
- package/package.json +1 -1
- package/workers/SuperConsumer.js +12 -6
- package/example0/RunnerConsumer.js +0 -18
- /package/{example2-dynamicIncreasing → examples/example3-dynamicIncreasing}/RunnerConsumer.js +0 -0
- /package/{example2-dynamicIncreasing → examples/example3-dynamicIncreasing}/RunnerProducer.js +0 -0
- /package/{example2-dynamicIncreasing → examples/example3-dynamicIncreasing}/run.js +0 -0
- /package/{example2-dynamicIncreasing → examples/example3-dynamicIncreasing}/util.js +0 -0
package/README.md
CHANGED
|
@@ -9,6 +9,23 @@ npm install multi-tasks
|
|
|
9
9
|
|
|
10
10
|
How to use:
|
|
11
11
|
|
|
12
|
+
```javascript
|
|
13
|
+
//see examples/example0
|
|
14
|
+
let multiTasks = require('multi-tasks').multiTasks;
|
|
15
|
+
multiTasks({
|
|
16
|
+
tasks: [...], //You need to provide an array containing all the subtasks you want to execute, multi-tasks will automatically split them and execute them in a multi-threaded manner
|
|
17
|
+
taskResolver:(task)=>{//You need to provide a taskResolver function to process a certain sub-task and return the result data
|
|
18
|
+
return Promise.resolve(data);
|
|
19
|
+
},
|
|
20
|
+
multi_task_parent_folder: `C:/tmp-demo`, //a directory to store progress and results files
|
|
21
|
+
quotaOfEachTask:2, //The number of tasks assigned to each worker
|
|
22
|
+
numberOfWorks: 6
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Or, to provide the Producer & Consumer
|
|
28
|
+
|
|
12
29
|
```javascript
|
|
13
30
|
let master = require('multi-tasks').master;
|
|
14
31
|
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
|
|
@@ -24,12 +41,13 @@ master.start(RunnerProducer, RunnerConsumer, {
|
|
|
24
41
|
Have a try:
|
|
25
42
|
|
|
26
43
|
```shell
|
|
27
|
-
node example0/run
|
|
28
|
-
node example1/run
|
|
44
|
+
node examples/example0/run
|
|
45
|
+
node examples/example1/run
|
|
29
46
|
```
|
|
30
47
|
|
|
31
48
|
Changelog:
|
|
32
49
|
|
|
50
|
+
- 1.2.0 Simplified usage by providing the function way and support return Promise
|
|
33
51
|
- 1.1.4 Remove make-dir
|
|
34
52
|
- 1.1.3 Simplified usage, see example0
|
|
35
53
|
- 1.1.2
|
|
@@ -37,4 +55,4 @@ Changelog:
|
|
|
37
55
|
- 1.1.0 Simplified the usage of a customized Consumer, see example0
|
|
38
56
|
- 1.0.8 Fix examples
|
|
39
57
|
- 1.0.7 Remove moment
|
|
40
|
-
- 1.0.6 Performance optimization
|
|
58
|
+
- 1.0.6 Performance optimization
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
let multiTasks = require('../../index').multiTasks;
|
|
2
|
+
|
|
3
|
+
let alltasks = []
|
|
4
|
+
for(let i=0;i<200;i++){
|
|
5
|
+
let task_props = {
|
|
6
|
+
index: i,
|
|
7
|
+
name: `task-${i}`,
|
|
8
|
+
description: `This prop is for a subtask`
|
|
9
|
+
};
|
|
10
|
+
alltasks.push(task_props);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
let taskResolver = (actionData)=>{
|
|
14
|
+
console.log('this is the actionData of one task', actionData);
|
|
15
|
+
return Promise.resolve({
|
|
16
|
+
message: 'this is a result from a demo, random data=' + Math.random()
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
multiTasks({
|
|
21
|
+
tasks: alltasks, //You need to provide the data of all subtasks, multi-tasks will automatically split them and execute them in a multi-threaded manner
|
|
22
|
+
taskResolver, //You need to provide a taskResolver function to process a certain sub-task and return the result data
|
|
23
|
+
multi_task_parent_folder: `C:/tmp-demo`, //a directory to store progress and results files
|
|
24
|
+
quotaOfEachTask:2, //The number of tasks assigned to each worker
|
|
25
|
+
numberOfWorks: 6
|
|
26
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
let SuperConsumer = require('../../index').SuperConsumer;
|
|
2
|
+
|
|
3
|
+
class TestConsumer extends SuperConsumer {
|
|
4
|
+
|
|
5
|
+
process(actionData){
|
|
6
|
+
console.log(`processing data:`, actionData);
|
|
7
|
+
//you can also return a data instead of a promise object
|
|
8
|
+
return new Promise((resolve) => {
|
|
9
|
+
setTimeout(() => {
|
|
10
|
+
let result = {
|
|
11
|
+
task:{
|
|
12
|
+
...actionData,
|
|
13
|
+
},
|
|
14
|
+
result: {
|
|
15
|
+
message: 'this is a result from a demo, random data=' + Math.random()
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
resolve(result);
|
|
19
|
+
}, 100);
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
module.exports = TestConsumer;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
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
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('
|
|
3
|
+
let master = require('../../index').master;
|
|
4
4
|
|
|
5
5
|
master.start(RunnerProducer, RunnerConsumer, {
|
|
6
6
|
multi_task_parent_folder: `C:/tmp-demo`,
|
|
@@ -6,7 +6,7 @@ let multi_task_parent_folder = `C:/tmp-demo`;
|
|
|
6
6
|
|
|
7
7
|
let producer = new RunnerProducer(`workers_of_${taskId}`, multi_task_parent_folder)
|
|
8
8
|
let consumer = new RunnerConsumer();
|
|
9
|
-
let master = require('
|
|
9
|
+
let master = require('../../index').master;
|
|
10
10
|
|
|
11
11
|
console.log(producer.getTaskName())
|
|
12
12
|
//return;
|
package/index.js
CHANGED
|
@@ -1,8 +1,37 @@
|
|
|
1
1
|
const SuperProducer = require('./workers/SuperProducer');
|
|
2
2
|
const SuperConsumer = require('./workers/SuperConsumer');
|
|
3
|
-
const master = require('./workers/master')
|
|
3
|
+
const master = require('./workers/master');
|
|
4
|
+
const multiTasks = (config)=>{
|
|
5
|
+
if(!config.tasks){
|
|
6
|
+
throw 'Please provide tasks data';
|
|
7
|
+
};
|
|
8
|
+
if(!config.taskResolver){
|
|
9
|
+
throw 'Please provide taskResolver function';
|
|
10
|
+
};
|
|
11
|
+
class TestProducer extends SuperProducer {
|
|
12
|
+
// constructor
|
|
13
|
+
setTaskTrunkInfo(trunkInfo){
|
|
14
|
+
this.trunkInfo = trunkInfo;
|
|
15
|
+
}
|
|
16
|
+
getAllActions() {
|
|
17
|
+
return Promise.resolve(config.tasks);//return all your tasks as an array
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
class TestConsumer extends SuperConsumer {
|
|
21
|
+
process(taskInfo, actionData){
|
|
22
|
+
console.log(`processing data:`, taskInfo, actionData);
|
|
23
|
+
let result = config.taskResolver(actionData);
|
|
24
|
+
return Promise.resolve(result);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
master.start(TestProducer, TestConsumer, {
|
|
29
|
+
...config
|
|
30
|
+
});
|
|
31
|
+
};
|
|
4
32
|
module.exports = {
|
|
5
33
|
SuperProducer,
|
|
6
34
|
SuperConsumer,
|
|
7
|
-
master
|
|
35
|
+
master,
|
|
36
|
+
multiTasks
|
|
8
37
|
}
|
package/package.json
CHANGED
package/workers/SuperConsumer.js
CHANGED
|
@@ -25,13 +25,19 @@ class SuperConsumer {
|
|
|
25
25
|
let actionData = actions.shift();
|
|
26
26
|
|
|
27
27
|
if(this.process){
|
|
28
|
-
let result = this.process(
|
|
29
|
-
if(result){
|
|
30
|
-
|
|
28
|
+
let result = this.process(actionData);//rewrite this method in subclass
|
|
29
|
+
if(typeof result === 'undefined') result = {};
|
|
30
|
+
let ispromise = result instanceof Promise;
|
|
31
|
+
if(! ispromise){
|
|
32
|
+
result = Promise.resolve(result);
|
|
31
33
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
result.then((data)=>{
|
|
35
|
+
results.push(data);
|
|
36
|
+
this._run(mainTaskInfo, subTaskInfo, actions, results, callback);
|
|
37
|
+
});
|
|
38
|
+
}else{
|
|
39
|
+
this._run(mainTaskInfo, subTaskInfo, actions, results, callback);
|
|
40
|
+
}
|
|
35
41
|
};
|
|
36
42
|
run (mainTaskInfo, subTaskInfo, taskInfo, callback){
|
|
37
43
|
let results = [];
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
let SuperConsumer = require('../index').SuperConsumer;
|
|
2
|
-
|
|
3
|
-
class TestConsumer extends SuperConsumer {
|
|
4
|
-
|
|
5
|
-
process(taskInfo, actionData){
|
|
6
|
-
console.log(`processing data:`, taskInfo, actionData);
|
|
7
|
-
return {
|
|
8
|
-
task:{
|
|
9
|
-
...taskInfo,
|
|
10
|
-
...actionData,
|
|
11
|
-
},
|
|
12
|
-
result: {
|
|
13
|
-
message: 'this is a result from a demo, random data=' + Math.random()
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
module.exports = TestConsumer;
|
/package/{example2-dynamicIncreasing → examples/example3-dynamicIncreasing}/RunnerConsumer.js
RENAMED
|
File without changes
|
/package/{example2-dynamicIncreasing → examples/example3-dynamicIncreasing}/RunnerProducer.js
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|