multi-tasks 1.1.2 → 1.1.3
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 +18 -1
- package/example0/RunnerConsumer.js +6 -4
- package/example0/RunnerProducer.js +1 -1
- package/example0/run.js +5 -19
- package/example1/RunnerConsumer.js +6 -3
- package/example1/RunnerProducer.js +1 -1
- package/package.json +1 -1
- package/workers/SuperConsumer.js +7 -5
- package/workers/master.js +20 -2
package/README.md
CHANGED
|
@@ -7,14 +7,31 @@ Install:
|
|
|
7
7
|
npm install multi-tasks
|
|
8
8
|
```
|
|
9
9
|
|
|
10
|
+
How to use:
|
|
11
|
+
|
|
12
|
+
```javascript
|
|
13
|
+
let master = require('multi-tasks').master;
|
|
14
|
+
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
|
|
15
|
+
let RunnerConsumer;//See examples, please provide a Consumer class to process a certain sub-task and return the processed data
|
|
16
|
+
|
|
17
|
+
master.start(RunnerProducer, RunnerConsumer, {
|
|
18
|
+
multi_task_parent_folder: `C:/tmp-demo`,
|
|
19
|
+
quotaOfEachTask:2, //The number of tasks assigned to each worker
|
|
20
|
+
numberOfWorks: 6
|
|
21
|
+
});
|
|
22
|
+
```
|
|
23
|
+
|
|
10
24
|
Have a try:
|
|
11
25
|
|
|
12
26
|
```shell
|
|
13
|
-
node
|
|
27
|
+
node example0/run
|
|
28
|
+
node example1/run
|
|
14
29
|
```
|
|
15
30
|
|
|
16
31
|
Changelog:
|
|
17
32
|
|
|
33
|
+
- 1.1.3 Simplified usage, see example0
|
|
34
|
+
- 1.1.2
|
|
18
35
|
- 1.1.1 Rename files, updated changelog
|
|
19
36
|
- 1.1.0 Simplified the usage of a customized Consumer, see example0
|
|
20
37
|
- 1.0.8 Fix examples
|
|
@@ -3,12 +3,14 @@ let SuperConsumer = require('../index').SuperConsumer;
|
|
|
3
3
|
class TestConsumer extends SuperConsumer {
|
|
4
4
|
|
|
5
5
|
process(taskInfo, actionData){
|
|
6
|
-
console.log(`
|
|
6
|
+
console.log(`processing data:`, taskInfo, actionData);
|
|
7
7
|
return {
|
|
8
|
-
|
|
9
|
-
message: 'this is a result from a demo, random=' + Math.random(),
|
|
8
|
+
task:{
|
|
10
9
|
...taskInfo,
|
|
11
|
-
...actionData
|
|
10
|
+
...actionData,
|
|
11
|
+
},
|
|
12
|
+
result: {
|
|
13
|
+
message: 'this is a result from a demo, random data=' + Math.random()
|
|
12
14
|
}
|
|
13
15
|
}
|
|
14
16
|
}
|
package/example0/run.js
CHANGED
|
@@ -1,23 +1,9 @@
|
|
|
1
|
-
let RunnerProducer = require('./RunnerProducer');//
|
|
2
|
-
let RunnerConsumer = require('./RunnerConsumer');//
|
|
3
|
-
|
|
4
|
-
let taskId = Math.random()+'';
|
|
5
|
-
let multi_task_parent_folder = `C:/tmp-demo`;
|
|
6
|
-
|
|
7
|
-
let producer = new RunnerProducer(`workers_of_${taskId}`, multi_task_parent_folder)
|
|
8
|
-
let consumer = new RunnerConsumer();
|
|
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
|
|
9
3
|
let master = require('../index').master;
|
|
10
4
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
let taskConfig = {
|
|
15
|
-
multi_task_parent_folder
|
|
16
|
-
}
|
|
17
|
-
//console.log(resp)
|
|
18
|
-
producer.setTaskTrunkInfo(taskConfig);
|
|
19
|
-
consumer.setTaskTrunkInfo(taskConfig)
|
|
20
|
-
master.run(producer, consumer, {
|
|
21
|
-
quotaOfEachTask:2,
|
|
5
|
+
master.start(RunnerProducer, RunnerConsumer, {
|
|
6
|
+
multi_task_parent_folder: `C:/tmp-demo`,
|
|
7
|
+
quotaOfEachTask:2, //The number of tasks assigned to each worker
|
|
22
8
|
numberOfWorks: 6
|
|
23
9
|
});
|
|
@@ -2,15 +2,18 @@ let SuperConsumer = require('../index').SuperConsumer;
|
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
let _do = (mainTaskInfo, actions, callback)=>{
|
|
5
|
-
if(actions.length === 0) return callback({
|
|
6
|
-
|
|
5
|
+
if(actions.length === 0) return callback({
|
|
6
|
+
data:'subtask finished, random data='+Math.random()
|
|
7
|
+
});
|
|
8
|
+
let actionData = actions.shift();
|
|
7
9
|
|
|
8
10
|
//start to write your process code here
|
|
9
11
|
|
|
10
|
-
console.log('
|
|
12
|
+
console.log('actionData', actionData)
|
|
11
13
|
|
|
12
14
|
//end of your process code.
|
|
13
15
|
|
|
16
|
+
//trigger next task
|
|
14
17
|
_do(mainTaskInfo, actions, callback);
|
|
15
18
|
};
|
|
16
19
|
|
package/package.json
CHANGED
package/workers/SuperConsumer.js
CHANGED
|
@@ -12,10 +12,11 @@ class SuperConsumer {
|
|
|
12
12
|
setTaskTrunkInfo(trunkInfo){
|
|
13
13
|
this.trunkInfo = trunkInfo;
|
|
14
14
|
}
|
|
15
|
-
_run (mainTaskInfo, actions, callback){
|
|
15
|
+
_run (mainTaskInfo, subTaskInfo, actions, results, callback){
|
|
16
16
|
if(actions.length === 0) return callback({
|
|
17
17
|
finishedAt: new Date()*1,
|
|
18
|
-
results
|
|
18
|
+
results,
|
|
19
|
+
subTaskInfo
|
|
19
20
|
});
|
|
20
21
|
let taskInfo = {
|
|
21
22
|
mainid: this.mainid,
|
|
@@ -26,16 +27,17 @@ class SuperConsumer {
|
|
|
26
27
|
if(this.process){
|
|
27
28
|
let result = this.process(taskInfo, actionData);//rewrite this method in subclass
|
|
28
29
|
if(result){
|
|
29
|
-
|
|
30
|
+
results.push(result);
|
|
30
31
|
}
|
|
31
32
|
}
|
|
32
33
|
|
|
33
|
-
this._run(mainTaskInfo, actions, callback);
|
|
34
|
+
this._run(mainTaskInfo, subTaskInfo, actions, results, callback);
|
|
34
35
|
};
|
|
35
36
|
run (mainTaskInfo, subTaskInfo, taskInfo, callback){
|
|
37
|
+
let results = [];
|
|
36
38
|
let actions = subTaskInfo.actions;
|
|
37
39
|
mainTaskInfo.defination = this.trunkInfo.defination;
|
|
38
|
-
this._run(mainTaskInfo, actions, (result)=>{
|
|
40
|
+
this._run(mainTaskInfo, subTaskInfo, actions, results, (result)=>{
|
|
39
41
|
callback(result)
|
|
40
42
|
})
|
|
41
43
|
|
package/workers/master.js
CHANGED
|
@@ -3,7 +3,6 @@ var pathutil = require('path');
|
|
|
3
3
|
const cluster = require('cluster');
|
|
4
4
|
const numCPUs = require('os').cpus().length;
|
|
5
5
|
|
|
6
|
-
|
|
7
6
|
let MSG_REQUEST_TASKID = 'request_taskid';
|
|
8
7
|
/**
|
|
9
8
|
* masterId, the id of scanning flow
|
|
@@ -141,7 +140,26 @@ let run = (currentProducer, currentConsumer, opt)=>{
|
|
|
141
140
|
worker.send(MSG_REQUEST_TASKID);//request new task
|
|
142
141
|
|
|
143
142
|
}
|
|
143
|
+
};
|
|
144
|
+
let start = (ProducerClass, ConsumerClass, taskConfig)=>{
|
|
145
|
+
let taskId = Math.random()+'';
|
|
146
|
+
let {multi_task_parent_folder} = taskConfig;
|
|
147
|
+
|
|
148
|
+
let producer = new ProducerClass(`workers_of_${taskId}`, multi_task_parent_folder)
|
|
149
|
+
let consumer = new ConsumerClass();
|
|
150
|
+
|
|
151
|
+
console.log(producer.getTaskName())
|
|
152
|
+
//return;
|
|
153
|
+
|
|
154
|
+
//console.log(resp)
|
|
155
|
+
producer.setTaskTrunkInfo(taskConfig);
|
|
156
|
+
consumer.setTaskTrunkInfo(taskConfig);
|
|
157
|
+
run(producer, consumer, {
|
|
158
|
+
quotaOfEachTask:2,
|
|
159
|
+
numberOfWorks: 6
|
|
160
|
+
});
|
|
144
161
|
}
|
|
145
162
|
module.exports = {
|
|
146
|
-
run
|
|
163
|
+
run,
|
|
164
|
+
start
|
|
147
165
|
};
|