multi-tasks 1.2.5 → 1.2.7

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 CHANGED
@@ -43,12 +43,15 @@ 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
- numberOfWorkers: 6 //Assign how many workers are working in parallel, default is the number of cpu cores minus one
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
+ }
47
50
  });
48
51
 
49
52
  ```
50
53
 
51
- Or, to provide the Producer & Consumer
54
+ Or the older way, to provide the Producer & Consumer
52
55
 
53
56
  ```javascript
54
57
  let master = require('multi-tasks').master;
@@ -56,9 +59,10 @@ let RunnerProducer;//See examples, please provide a Producer class to specify al
56
59
  let RunnerConsumer;//See examples, please provide a Consumer class to process a certain sub-task and return the processed data
57
60
 
58
61
  master.start(RunnerProducer, RunnerConsumer, {
59
- multi_task_parent_folder: `C:/tmp-demo`,
60
- quotaOfEachTask:2, //The number of tasks assigned to each worker
61
- numberOfWorkers: 6
62
+ multi_task_parent_folder,
63
+ quotaOfEachTask,
64
+ numberOfWorkers,
65
+ onFinish
62
66
  });
63
67
  ```
64
68
 
@@ -71,6 +75,8 @@ node examples/example1/run
71
75
 
72
76
  Changelog:
73
77
 
78
+ - 1.2.7 Small updates
79
+ - 1.2.6 Support onFinish event
74
80
  - 1.2.5 Rename numberOfWorks to numberOfWorkers, the old one are still supported ;-)
75
81
  - 1.2.4 Fix: opt.numberOfWorkers not work
76
82
  - 1.2.3 Update README
@@ -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
- numberOfWorkers: 6 //Assign how many workers are working in parallel
34
+ numberOfWorkers: 6, //Assign how many workers are working in parallel
35
+ onFinish: (report)=>{
36
+ console.log(report);
37
+ }
35
38
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "multi-tasks",
3
- "version": "1.2.5",
3
+ "version": "1.2.7",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "directories": {
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
  /**
@@ -96,7 +97,12 @@ let run = (currentProducer, currentConsumer, opt)=>{
96
97
  };
97
98
  let dataPath = pathutil.resolve(taskFolder, `./extractTaskData-${masterId}.json`);
98
99
  fs.writeFileSync(dataPath, JSON.stringify(dataForNextMission))
99
- 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
+ }
100
106
  };
101
107
  }
102
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
+ }