multi-tasks 2.1.4 → 2.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 +24 -16
- package/package.json +1 -1
- package/workers/Multithread.js +5 -1
- package/workers/index.js +24 -5
package/README.md
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
# multi-tasks
|
|
2
|
-
multi-tasks
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
Multi-tasks is used to handle long-term and large-scale parallel computing tasks. It manages progress and tasks based on the file system, which means that even if the host crashes, tasks can be resumed based on file records.
|
|
4
|
+
|
|
5
|
+
### Install:
|
|
5
6
|
|
|
6
7
|
```javascript
|
|
7
8
|
npm install multi-tasks
|
|
8
9
|
```
|
|
9
10
|
|
|
10
|
-
How to use:
|
|
11
|
+
### How to use:
|
|
11
12
|
|
|
12
13
|
```javascript
|
|
13
14
|
//see examples/example0
|
|
14
15
|
let multiTasks = require('multi-tasks').multiTasks;
|
|
15
16
|
|
|
16
|
-
|
|
17
17
|
//Step1, create your tasks that need to be executed simultaneously as an array.
|
|
18
18
|
let alltasks = [];
|
|
19
19
|
for(let i=0;i<50;i++){
|
|
@@ -51,14 +51,7 @@ multiTasks({
|
|
|
51
51
|
|
|
52
52
|
```
|
|
53
53
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
```shell
|
|
57
|
-
node examples/example0/run
|
|
58
|
-
node examples/example1/run
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
More about the task processing function
|
|
54
|
+
### More about the task processing function
|
|
62
55
|
|
|
63
56
|
```javascript
|
|
64
57
|
//Example1, return a promise for async processes,
|
|
@@ -77,7 +70,6 @@ let processTask = (task, helper)=>{
|
|
|
77
70
|
//Example2, dynamically create a new task while processing
|
|
78
71
|
let processTask = (task, helper)=>{
|
|
79
72
|
let {taskCount} = task;
|
|
80
|
-
|
|
81
73
|
|
|
82
74
|
if(taskCount % 2 === 0){
|
|
83
75
|
//create a new task if needed
|
|
@@ -109,8 +101,24 @@ let processTask = (task, helper)=>{
|
|
|
109
101
|
|
|
110
102
|
```
|
|
111
103
|
|
|
112
|
-
|
|
104
|
+
### Resuming
|
|
105
|
+
|
|
106
|
+
Sometimes the task execution is interrupted due to some reasons (such as power outage), you can resume the execution like this
|
|
107
|
+
|
|
108
|
+
```javascript
|
|
109
|
+
|
|
110
|
+
multiTasks({
|
|
111
|
+
initialTasks: `/myworks/my_scan_tasks/`, //Point 'initialTasks' to the interrupted task directory, multi-tasks will read the tasks in the 'new' folder and initialize them to 'initialTasks' and then continue execution
|
|
112
|
+
...
|
|
113
|
+
...//Other configurations remain unchanged
|
|
114
|
+
...
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### Changelog:
|
|
113
120
|
|
|
121
|
+
- 2.2.0 Support Resuming from an interrupted task
|
|
114
122
|
- 2.1.2 Update readme
|
|
115
123
|
- 2.1.1 Fix: recreate a new one when a worker collapsed unexpectedly.
|
|
116
124
|
- 2.1.0 Add a new output directory "log" where you can view the process time of each subtask
|
|
@@ -142,10 +150,10 @@ Changelog:
|
|
|
142
150
|
- 1.0.7 Remove moment
|
|
143
151
|
- 1.0.6 Performance optimization
|
|
144
152
|
|
|
145
|
-
Github:
|
|
153
|
+
### Github:
|
|
146
154
|
|
|
147
155
|
[https://github.com/zhanglei923/multi-tasks](https://github.com/zhanglei923/multi-tasks)
|
|
148
156
|
|
|
149
|
-
Support / Bug Report:
|
|
157
|
+
### Support / Bug Report:
|
|
150
158
|
|
|
151
159
|
[zhangleisupport923@163.com](mailto:zhangleisupport923@163.com)
|
package/package.json
CHANGED
package/workers/Multithread.js
CHANGED
|
@@ -161,12 +161,16 @@ function subWorker(){
|
|
|
161
161
|
//console.log('ssss', subTask)
|
|
162
162
|
if(subTask){
|
|
163
163
|
subTask.index = taskCount;//for v1 backward
|
|
164
|
-
subTask.taskCount = taskCount;
|
|
164
|
+
subTask.taskCount = taskCount;
|
|
165
|
+
subTask.workerId = worker.id;
|
|
166
|
+
subTask.workerPid = process.pid;
|
|
165
167
|
console.log(`[Task Start]: "${subTask.subid}"`, `pid=${process.pid}`, getDateTimeTxt());
|
|
166
168
|
let sendMsg = (msg)=>{
|
|
167
169
|
worker.send(msg);
|
|
168
170
|
};
|
|
169
171
|
let subTaskLog = {
|
|
172
|
+
workerId: worker.id,
|
|
173
|
+
workerPid: process.pid,
|
|
170
174
|
taskCount,
|
|
171
175
|
startTimestamp
|
|
172
176
|
};
|
package/workers/index.js
CHANGED
|
@@ -5,7 +5,7 @@ const backwardv1 = require('../utils/backward-v1');
|
|
|
5
5
|
|
|
6
6
|
const _toAbsPath = (path)=>{
|
|
7
7
|
if(!path) return path;
|
|
8
|
-
if(pathUtil.isAbsolute(path))return;
|
|
8
|
+
if(pathUtil.isAbsolute(path))return path;
|
|
9
9
|
let abspath = pathUtil.resolve(process.cwd(), path);
|
|
10
10
|
return abspath;
|
|
11
11
|
};
|
|
@@ -39,10 +39,29 @@ const restart = (config)=>{
|
|
|
39
39
|
};
|
|
40
40
|
|
|
41
41
|
const multiTasks = (config)=>{
|
|
42
|
-
let {taskRootFolder, taskId} = config;
|
|
43
|
-
let
|
|
42
|
+
let {initialTasks, taskRootFolder, taskId} = config;
|
|
43
|
+
let isResume = false;
|
|
44
44
|
|
|
45
|
-
if(
|
|
45
|
+
if(typeof initialTasks === 'string'){//might be an existing tasks folder
|
|
46
|
+
if(fs.existsSync(initialTasks)){
|
|
47
|
+
isResume = true;
|
|
48
|
+
config.taskRootFolder = pathUtil.dirname(initialTasks);
|
|
49
|
+
}else if(taskRootFolder){
|
|
50
|
+
let folder = pathUtil.resolve(taskRootFolder, initialTasks);
|
|
51
|
+
if(fs.existsSync(folder)){
|
|
52
|
+
isResume = true;
|
|
53
|
+
config.taskRootFolder = pathUtil.dirname(folder);;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if(!isResume) throw 'FATAL: initialTasks should be an array or a task root folder!';
|
|
57
|
+
}else{
|
|
58
|
+
let taskFolder = pathUtil.resolve(taskRootFolder, taskId);
|
|
59
|
+
if(fs.existsSync(taskFolder)){
|
|
60
|
+
isResume = true;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if(isResume){
|
|
46
65
|
resume(config);
|
|
47
66
|
}else{
|
|
48
67
|
start(config);
|
|
@@ -54,4 +73,4 @@ multiTasks.start = start;
|
|
|
54
73
|
multiTasks.restart = restart;
|
|
55
74
|
multiTasks.resume = resume;
|
|
56
75
|
|
|
57
|
-
module.exports = multiTasks;
|
|
76
|
+
module.exports = multiTasks;
|