multi-tasks 2.0.3 → 2.0.5

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
@@ -30,7 +30,34 @@ let processTask = (task, helper)=>{
30
30
  return 'a result which is not a promise';
31
31
  };
32
32
 
33
- //Step2.1, or return a promise
33
+ //Step3, run!
34
+ multiTasks({
35
+ initialTasks: alltasks,
36
+ processTask,
37
+ taskRootFolder: `../examples-tmp-data/example0`, //a directory to store progress and results files, you can check the progress here
38
+ taskId: 'my-task',
39
+ numberOfWorkers: 3, //Assign how many workers are working in parallel
40
+ //autoCloseAfterCompletion: true, //if you have dynamically generated new tasks, put this as false
41
+ shouldTerminate:(info)=>{
42
+ //return true if you need to terminate the whole process
43
+ },
44
+ onFinish: (report)=>{
45
+ console.log('finish callback', report);
46
+ }
47
+ });
48
+
49
+ ```
50
+
51
+ Have a try:
52
+
53
+ ```shell
54
+ node examples/example0/run
55
+ node examples/example1/run
56
+ ```
57
+
58
+ More about the task processing function
59
+ ```
60
+ //you can return a promise for async processes
34
61
  let processTask = (task, helper)=>{
35
62
  let {taskCount} = task;
36
63
 
@@ -41,7 +68,7 @@ let processTask = (task, helper)=>{
41
68
  })
42
69
  };
43
70
 
44
- //Step2.2, dynamically create a new task if find something new while processing
71
+ //dynamically create a new task while processing
45
72
  let processTask = (task, helper)=>{
46
73
  let {taskCount} = task;
47
74
 
@@ -60,7 +87,7 @@ let processTask = (task, helper)=>{
60
87
  })
61
88
  };
62
89
 
63
- //Step2.3, this example shows how to handle exceptions/errors
90
+ //this example shows how to generate/handle exceptions
64
91
  let processTask = (task, helper)=>{
65
92
  let {taskCount} = task;
66
93
 
@@ -88,34 +115,12 @@ let processTask = (task, helper)=>{
88
115
  }, 10)
89
116
  })
90
117
  }
91
-
92
- //Step3, run!
93
- multiTasks.start({
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
- shouldTerminate:(info)=>{
101
- //return true if you need to terminate the whole process
102
- },
103
- onFinish: (report)=>{
104
- console.log('finish callback', report);
105
- }
106
- });
107
-
108
- ```
109
-
110
- Have a try:
111
-
112
- ```shell
113
- node examples/example0/run
114
- node examples/example1/run
115
118
  ```
116
119
 
117
120
  Changelog:
118
121
 
122
+ - 2.0.5 Update readme examples
123
+ - 2.0.4 Support resume from a failed task
119
124
  - 2.0.3 Fix: mkdir bug on windows
120
125
  - 2.0.2 new feature: support shouldTerminate
121
126
  - 2.0.1 Avoid possible I/O conflicts.
@@ -40,8 +40,8 @@ let processTask = (task, helper)=>{
40
40
  }
41
41
 
42
42
  //Step3, run!
43
- multiTasks.start({
44
- tasks: alltasks,
43
+ multiTasks({
44
+ initialTasks: alltasks,
45
45
  processTask,
46
46
  taskRootFolder: `../examples-tmp-data/example0`, //a directory to store progress and results files, you can check the progress here
47
47
  taskId: 'my-task',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "multi-tasks",
3
- "version": "2.0.3",
3
+ "version": "2.0.5",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "directories": {
@@ -2,7 +2,7 @@ let multiTasks = require('../../index').multiTasks;
2
2
 
3
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
4
  let alltasks = [];
5
- for(let i=0;i<50;i++){
5
+ for(let i=0;i<1000;i++){
6
6
  let task_props = {
7
7
  index: i,
8
8
  name: `task-${i}`,
@@ -40,8 +40,8 @@ let processTask = (task, helper)=>{
40
40
  }
41
41
 
42
42
  //Step3, run!
43
- multiTasks.start({
44
- tasks: alltasks,
43
+ multiTasks({
44
+ initialTasks: alltasks,
45
45
  processTask,
46
46
  taskRootFolder: `./tmp`, //a directory to store progress and results files, you can check the progress here
47
47
  taskId: 'test_resume',
@@ -7,10 +7,11 @@ let processTask = (task, helper)=>{
7
7
  };
8
8
 
9
9
  //Step3, run!
10
- multiTasks.resume({
10
+ multiTasks({
11
11
  processTask,
12
- taskFolder: `./tmp/test_resume`, //a directory to store progress and results files, you can check the progress here
13
- numberOfWorkers: 3, //Assign how many workers are working in parallel
12
+ taskRootFolder: `./tmp`, //a directory to store progress and results files, you can check the progress here
13
+ taskId: 'test_resume',
14
+ numberOfWorkers: 11, //Assign how many workers are working in parallel
14
15
  //autoCloseAfterCompletion: true,
15
16
  shouldTerminate:(info)=>{
16
17
  //if(info.taskCount > 30) return true;
@@ -1,3 +1,4 @@
1
1
  #!/bin/bash
2
2
  node step1_start_and_crash
3
+ sleep 3
3
4
  node step2_resume
@@ -5,6 +5,7 @@ const numCPUs = require('os').cpus().length;
5
5
 
6
6
  let MSG_REQUEST_NEW_TASK = 'request_new_task';
7
7
  let MSG_NO_TASK_FOUND = 'no_task_found';
8
+ let MSG_SET_GLOBAL_PARAMS = 'MSG_SET_GLOBAL_PARAMS';
8
9
 
9
10
  const asMaster = require('./asMaster');
10
11
  const TaskMgr = require('./TaskMgr');
@@ -104,7 +105,10 @@ const start = (config)=>{
104
105
  }
105
106
  });
106
107
  });
107
- }
108
+ }else
109
+ if(message.MSG_SET_GLOBAL_PARAMS){
110
+ console.log('xxxxxxxxxxxxxxxxxxxxxxxxxx', message)
111
+ }
108
112
  });
109
113
  }
110
114
  });
@@ -149,7 +153,10 @@ function subWorker(){
149
153
  subTask.index = taskCount;//for v1 backward
150
154
  subTask.taskCount = taskCount;
151
155
  console.log(`[Task Start]: "${subTask.subid}"`, `pid=${process.pid}`, getDateTimeTxt());
152
- runTask(config, subTask, message).then((report)=>{
156
+ let sendMsg = (msg)=>{
157
+ worker.send(msg);
158
+ };
159
+ runTask(config, subTask, sendMsg).then((report)=>{
153
160
  let endTimestamp = new Date()*1;
154
161
  let cost = startTimestamp - endTimestamp;
155
162
 
@@ -166,12 +173,18 @@ function subWorker(){
166
173
  worker.send(MSG_REQUEST_NEW_TASK);//request new task
167
174
 
168
175
  };
169
- function runTask(config, subTask, message){
176
+ function runTask(config, subTask, sendMsg){
170
177
  return new Promise((resolve)=>{
171
178
  let resultPromise = Promise.resolve({});
172
179
  let resultData;
173
180
  let errors = [];
174
181
  helper.load(config);
182
+ helper.setGlobalParams = (params)=>{
183
+ sendMsg({
184
+ MSG_SET_GLOBAL_PARAMS,
185
+ params
186
+ });
187
+ };
175
188
  try{
176
189
  resultPromise = USER_CONFIG.processTask(subTask, helper);//rewrite this method in subclass
177
190
  }catch(e){
@@ -180,7 +180,7 @@ const TaskMgr = {
180
180
  });
181
181
  },
182
182
  mvTask: (subid, from, to, callback)=> {
183
- console.log('Move task:', subid, from, to, callback)
183
+ console.log('Move task:', subid, `${from}>${to}`);
184
184
  let {progressRootFolder} = main_config;
185
185
  let oldPath = pathutil.resolve(progressRootFolder, `./${from}/${subid}.task.json`);
186
186
  let newPath = pathutil.resolve(progressRootFolder, `./${to}/${subid}.task.json`);
@@ -23,14 +23,8 @@ const getDateTimeTxt = ()=>{
23
23
  };
24
24
 
25
25
  const resume = (config)=>{
26
- let {taskFolder} = config;
27
- if(!taskFolder){
28
- console.log('[ERROR]', 'method resume need param "taskFolder"')
29
- throw ('[ERROR]', 'method resume need param "taskFolder"');
30
- }
31
- let pathinfo = pathUtil.parse(taskFolder);
32
- let taskId = pathinfo.name;
33
- let taskRootFolder = pathinfo.dir;
26
+ let {taskRootFolder, taskId} = config;
27
+ let taskFolder = pathUtil.resolve(taskRootFolder, taskId);
34
28
 
35
29
  //load from config file
36
30
  let task_config = JSON.parse(fs.readFileSync(pathUtil.resolve(taskFolder, 'task_config.json')));
@@ -75,9 +69,8 @@ const resume = (config)=>{
75
69
 
76
70
  const init = (config)=>{
77
71
  if(typeof config === 'undefined') config = {};
78
- if(!config.tasks){ throw 'Please provide tasks data!'; };
72
+ if(!config.initialTasks){ throw 'Please provide initialTasks data!'; };
79
73
  if(!config.processTask){ throw 'Please provide processTask function!'; };
80
- if(!config.tasks){ throw 'Must assign initial tasks!'; };
81
74
 
82
75
  if(typeof config.numberOfWorkers === 'undefined') config.numberOfWorkers = numCPUs - 1;
83
76
 
@@ -100,7 +93,7 @@ const init = (config)=>{
100
93
  TaskMgr.load(config);
101
94
  config = TaskMgr.initFolders(config);
102
95
 
103
- config.tasks.forEach((task)=>{
96
+ config.initialTasks.forEach((task)=>{
104
97
  TaskMgr.createNewTask(task);
105
98
  });
106
99
 
package/workers/helper.js CHANGED
@@ -8,15 +8,18 @@ const load = (config)=>{
8
8
  myconfig = config;
9
9
  };
10
10
  const createNewTasks = (tasks)=>{
11
-
12
11
  if (!Array.isArray(tasks)) tasks = [tasks];
13
12
  TaskMgr.load(myconfig);
14
13
  tasks.forEach((task)=>{
15
- console.log('in a helper, ck', task)
14
+ console.log('Creating new task', task)
16
15
  TaskMgr.createNewTask(task);
17
16
  });
18
17
  };
18
+ const setGlobalParams = (params)=>{
19
+ //will be overridden in Multithread
20
+ };
19
21
  module.exports = {
20
22
  load,
21
- createNewTasks
23
+ createNewTasks,
24
+ setGlobalParams
22
25
  };
package/workers/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ const fs = require('fs');
1
2
  const pathUtil = require('path');
2
3
  const Multithread = require('./Multithread');
3
4
  const backwardv1 = require('../utils/backward-v1');
@@ -21,6 +22,8 @@ const resume = (config)=>{
21
22
  config = backwardv1.v1tov2(config);
22
23
  config.__resume = true;
23
24
 
25
+ config.initialTasks = [];
26
+
24
27
  config.taskRootFolder = _toAbsPath(config.taskRootFolder);
25
28
  config.taskFolder = _toAbsPath(config.taskFolder);
26
29
 
@@ -36,7 +39,15 @@ const restart = (config)=>{
36
39
  };
37
40
 
38
41
  const multiTasks = (config)=>{
39
- start(config);
42
+ let {taskRootFolder, taskId} = config;
43
+ let taskFolder = pathUtil.resolve(taskRootFolder, taskId);
44
+
45
+ if(fs.existsSync(taskFolder)){
46
+ resume(config);
47
+ }else{
48
+ start(config);
49
+ }
50
+
40
51
  };
41
52
 
42
53
  multiTasks.start = start;