multi-tasks 1.2.4 → 1.2.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.
- package/README.md +8 -3
- package/examples/example0/run.js +4 -1
- package/examples/example1/run.js +1 -1
- package/examples/example2/run.js +1 -1
- package/examples/example3-dynamicIncreasing/run.js +1 -1
- package/package.json +1 -1
- package/workers/master.js +10 -3
- package/workers/report/report_status.js +52 -0
package/README.md
CHANGED
|
@@ -43,7 +43,10 @@ multiTasks({
|
|
|
43
43
|
processTask,
|
|
44
44
|
multi_task_parent_folder: `C:/tmp-demo`, //a directory to store progress and results files, you can check the progress here
|
|
45
45
|
quotaOfEachTask:1, //The number of subtasks assigned to each worker at one time, the default is 1
|
|
46
|
-
|
|
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 execution report can be viewed here
|
|
49
|
+
}
|
|
47
50
|
});
|
|
48
51
|
|
|
49
52
|
```
|
|
@@ -58,7 +61,7 @@ let RunnerConsumer;//See examples, please provide a Consumer class to process a
|
|
|
58
61
|
master.start(RunnerProducer, RunnerConsumer, {
|
|
59
62
|
multi_task_parent_folder: `C:/tmp-demo`,
|
|
60
63
|
quotaOfEachTask:2, //The number of tasks assigned to each worker
|
|
61
|
-
|
|
64
|
+
numberOfWorkers: 6
|
|
62
65
|
});
|
|
63
66
|
```
|
|
64
67
|
|
|
@@ -71,7 +74,9 @@ node examples/example1/run
|
|
|
71
74
|
|
|
72
75
|
Changelog:
|
|
73
76
|
|
|
74
|
-
- 1.2.
|
|
77
|
+
- 1.2.6 Support onFinish event
|
|
78
|
+
- 1.2.5 Rename numberOfWorks to numberOfWorkers, the old one are still supported ;-)
|
|
79
|
+
- 1.2.4 Fix: opt.numberOfWorkers not work
|
|
75
80
|
- 1.2.3 Update README
|
|
76
81
|
- 1.2.2 Update README and examples
|
|
77
82
|
- 1.2.1 Handle exceptions and errors in subtasks
|
package/examples/example0/run.js
CHANGED
|
@@ -31,5 +31,8 @@ multiTasks({
|
|
|
31
31
|
processTask,
|
|
32
32
|
multi_task_parent_folder: `C:/tmp-demo`, //a directory to store progress and results files, you can check the progress here
|
|
33
33
|
quotaOfEachTask:1, //The number of subtasks assigned to each worker at one time, the default is 1
|
|
34
|
-
|
|
34
|
+
numberOfWorkers: 6, //Assign how many workers are working in parallel
|
|
35
|
+
onFinish: (report)=>{
|
|
36
|
+
console.log(report);
|
|
37
|
+
}
|
|
35
38
|
});
|
package/examples/example1/run.js
CHANGED
package/examples/example2/run.js
CHANGED
package/package.json
CHANGED
package/workers/master.js
CHANGED
|
@@ -2,6 +2,7 @@ var fs = require('fs');
|
|
|
2
2
|
var pathutil = require('path');
|
|
3
3
|
const cluster = require('cluster');
|
|
4
4
|
const numCPUs = require('os').cpus().length;
|
|
5
|
+
const report_status = require('./report/report_status');
|
|
5
6
|
|
|
6
7
|
let MSG_REQUEST_TASKID = 'request_taskid';
|
|
7
8
|
/**
|
|
@@ -41,7 +42,8 @@ let run = (currentProducer, currentConsumer, opt)=>{
|
|
|
41
42
|
if(typeof opt === 'undefined') opt = {};
|
|
42
43
|
let masterId = currentProducer.getTaskName();
|
|
43
44
|
if(typeof opt.quotaOfEachTask === 'undefined') opt.quotaOfEachTask = 1;
|
|
44
|
-
if(
|
|
45
|
+
if(opt.numberOfWorks && !opt.numberOfWorkers) opt.numberOfWorkers = opt.numberOfWorks; //Backward compatible with the wrong variable name
|
|
46
|
+
if(typeof opt.numberOfWorkers === 'undefined') opt.numberOfWorkers = defaultCPUs;
|
|
45
47
|
console.log('master1, masterId=', masterId)
|
|
46
48
|
|
|
47
49
|
|
|
@@ -50,7 +52,7 @@ let run = (currentProducer, currentConsumer, opt)=>{
|
|
|
50
52
|
console.log('start', getDateTimeTxt())
|
|
51
53
|
|
|
52
54
|
if (cluster.isMaster) {//init
|
|
53
|
-
let numWorks = opt.
|
|
55
|
+
let numWorks = opt.numberOfWorkers;
|
|
54
56
|
console.log(`numCPUs ${numCPUs}`);
|
|
55
57
|
console.log(`numWorks ${numWorks}`);
|
|
56
58
|
console.log(`Master=${process.pid}`);
|
|
@@ -95,7 +97,12 @@ let run = (currentProducer, currentConsumer, opt)=>{
|
|
|
95
97
|
};
|
|
96
98
|
let dataPath = pathutil.resolve(taskFolder, `./extractTaskData-${masterId}.json`);
|
|
97
99
|
fs.writeFileSync(dataPath, JSON.stringify(dataForNextMission))
|
|
98
|
-
console.log('dataForNextMission=', dataPath)
|
|
100
|
+
console.log('dataForNextMission=', dataPath);
|
|
101
|
+
if(opt.onFinish) {
|
|
102
|
+
opt.taskFolder = taskFolder;
|
|
103
|
+
let report = report_status.getReport(opt);
|
|
104
|
+
opt.onFinish(report);
|
|
105
|
+
}
|
|
99
106
|
};
|
|
100
107
|
}
|
|
101
108
|
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
var fs = require('fs');
|
|
2
|
+
var pathutil = require('path');
|
|
3
|
+
|
|
4
|
+
function countDirectChildFiles(directoryPath) {
|
|
5
|
+
const files = fs.readdirSync(directoryPath);
|
|
6
|
+
const directChildFiles = files.filter(file => {
|
|
7
|
+
const filePath = pathutil.join(directoryPath, file);
|
|
8
|
+
return fs.statSync(filePath).isFile();
|
|
9
|
+
});
|
|
10
|
+
return directChildFiles;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
let getReport = (taskConfig)=>{
|
|
14
|
+
let {taskFolder} = taskConfig;
|
|
15
|
+
let folder_result_errors = pathutil.resolve(taskFolder, './results/errors');
|
|
16
|
+
let folder_result_succ = pathutil.resolve(taskFolder, './results/succ');
|
|
17
|
+
|
|
18
|
+
let folder_new = pathutil.resolve(taskFolder, './progress/new');
|
|
19
|
+
let folder_running = pathutil.resolve(taskFolder, './progress/running');
|
|
20
|
+
let folder_finished = pathutil.resolve(taskFolder, './progress/finished');
|
|
21
|
+
let folder_finished_with_errors = pathutil.resolve(taskFolder, './progress/finished_with_errors');
|
|
22
|
+
|
|
23
|
+
let files_new = countDirectChildFiles(folder_new);
|
|
24
|
+
let files_running = countDirectChildFiles(folder_running);
|
|
25
|
+
let files_finished = countDirectChildFiles(folder_finished);
|
|
26
|
+
let files_finished_with_errors = countDirectChildFiles(folder_finished_with_errors);
|
|
27
|
+
|
|
28
|
+
let report = {
|
|
29
|
+
taskConfig,
|
|
30
|
+
task_folders:{
|
|
31
|
+
folder_new,
|
|
32
|
+
folder_running,
|
|
33
|
+
folder_finished,
|
|
34
|
+
folder_finished_with_errors
|
|
35
|
+
},
|
|
36
|
+
result_folders:{
|
|
37
|
+
folder_result_errors,
|
|
38
|
+
folder_result_succ
|
|
39
|
+
},
|
|
40
|
+
tasks:{
|
|
41
|
+
new: files_new.length,
|
|
42
|
+
running: files_running.length,
|
|
43
|
+
finished: files_finished.length,
|
|
44
|
+
finished_with_errors: files_finished_with_errors.length,
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
return report;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
module.exports = {
|
|
51
|
+
getReport
|
|
52
|
+
}
|