multi-tasks 1.2.8 → 2.0.1
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 +80 -40
- package/cmd.js +42 -0
- package/examples/example0/run-readme.js +88 -0
- package/examples/example0/run.js +32 -17
- package/index.js +1 -34
- package/monitors/cli.js +28 -0
- package/package.json +2 -1
- package/todo.txt +4 -0
- package/utils/asyncQueue.js +32 -0
- package/utils/backward-v1.js +11 -0
- package/utils/makedir.js +23 -1
- package/utils/randoms.js +15 -0
- package/utils/test/test_asyncqueue.js +116 -0
- package/workers/Multithread.js +177 -0
- package/workers/TaskMgr.js +182 -0
- package/workers/WorkerMgr.js +31 -0
- package/workers/asMaster.js +71 -0
- package/workers/asWorker.js +14 -0
- package/workers/helper.js +22 -0
- package/workers/index.js +8 -0
- package/workers/SuperConsumer.js +0 -106
- package/workers/SuperProducer.js +0 -97
- package/workers/master.js +0 -171
- package/workers/report/report_status.js +0 -52
- package/workers/taskSplitter.js +0 -30
- package/workers/test/MyConsumer.js +0 -25
- package/workers/test/MyProducer.js +0 -26
- package/workers/test/test_master.js +0 -12
- package/workers/test/test_producer.js +0 -4
- package/workers/test/test_splitter.js +0 -7
package/README.md
CHANGED
|
@@ -12,60 +12,98 @@ How to use:
|
|
|
12
12
|
```javascript
|
|
13
13
|
//see examples/example0
|
|
14
14
|
let multiTasks = require('multi-tasks').multiTasks;
|
|
15
|
-
//You need to provide the data of all subtasks, multi-tasks will automatically split them and execute them in a multi-threaded manner
|
|
16
|
-
let alltasks = [];
|
|
17
|
-
for(let i=0;i<100;i++){
|
|
18
|
-
let task_props = {
|
|
19
|
-
index: i,
|
|
20
|
-
name: `task-${i}`,
|
|
21
|
-
description: `This prop is for a subtask`
|
|
22
|
-
};
|
|
23
|
-
alltasks.push(task_props);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
//You need to provide a process function to handle a certain sub-task and return the result data
|
|
27
|
-
let processTask = (task)=>{
|
|
28
|
-
console.log('this is the subtask data you created above:', task);
|
|
29
|
-
let {index} = task;
|
|
30
15
|
|
|
31
|
-
//any exceptions will be captured
|
|
32
|
-
if(index===3) throw 'exception';
|
|
33
|
-
if(index===4) return Promise.reject({err:'a test error'});
|
|
34
16
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
17
|
+
//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.
|
|
18
|
+
let alltasks = [];
|
|
19
|
+
for(let i=0;i<50;i++){
|
|
20
|
+
alltasks.push({
|
|
21
|
+
name: `task-${i}`,
|
|
22
|
+
data: `This prop is for a subtask`
|
|
38
23
|
});
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
//Step2, Provide a function to process a certain sub-task and return the result data
|
|
27
|
+
let processTask = (task, helper)=>{
|
|
28
|
+
let {taskCount} = task;
|
|
29
|
+
|
|
30
|
+
return 'a result which is not a promise';
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
//Step2.1, or return a promise
|
|
34
|
+
let processTask = (task, helper)=>{
|
|
35
|
+
let {taskCount} = task;
|
|
36
|
+
|
|
37
|
+
return new Promise((resolve, reject)=>{
|
|
38
|
+
resolve({
|
|
39
|
+
data:`task${taskCount} complete`
|
|
40
|
+
})
|
|
41
|
+
})
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
//Step2.2, dynamically create a new task if find something new while processing
|
|
45
|
+
let processTask = (task, helper)=>{
|
|
46
|
+
let {taskCount} = task;
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
if(taskCount % 2 === 0){
|
|
50
|
+
helper.createNewTasks({//You can dynamically create a new task if find something new while processing
|
|
51
|
+
msg:'a new task'
|
|
52
|
+
});
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return new Promise((resolve, reject)=>{
|
|
57
|
+
resolve({
|
|
58
|
+
data:`task${taskCount} complete`
|
|
59
|
+
})
|
|
60
|
+
})
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
//Step2.3, this example shows how to handle exceptions/errors
|
|
64
|
+
let processTask = (task, helper)=>{
|
|
65
|
+
let {taskCount} = task;
|
|
66
|
+
|
|
67
|
+
//This is the demo of exceptions/errors, they will be captured and saved in the results/errors folder
|
|
68
|
+
if(taskCount===3) throw 'exception';
|
|
69
|
+
if(taskCount===4) return Promise.reject({err:'a test error'});
|
|
70
|
+
if(taskCount===5) aaa = bbb;
|
|
71
|
+
|
|
72
|
+
if(taskCount===6) return 'a result which is not a promise'; //Return a non-promise result is OK
|
|
73
|
+
|
|
74
|
+
//by default, you should return a promise, all returned data can be found in the results/succ folder
|
|
75
|
+
//but to return a non-promise result is also OK, see above
|
|
76
|
+
return new Promise((resolve, reject)=>{
|
|
77
|
+
setTimeout(()=>{
|
|
78
|
+
if(taskCount % 2 === 0){
|
|
79
|
+
helper.createNewTasks({//You can dynamically create a new task if find something new while processing
|
|
80
|
+
msg:'a new task'
|
|
81
|
+
});
|
|
82
|
+
resolve()
|
|
83
|
+
}else{
|
|
84
|
+
resolve({
|
|
85
|
+
data:`task${taskCount} complete`
|
|
86
|
+
})
|
|
87
|
+
}
|
|
88
|
+
}, 10)
|
|
89
|
+
})
|
|
39
90
|
}
|
|
40
91
|
|
|
92
|
+
//Step3, run!
|
|
41
93
|
multiTasks({
|
|
42
94
|
tasks: alltasks,
|
|
43
95
|
processTask,
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
numberOfWorkers:
|
|
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
|
|
47
100
|
onFinish: (report)=>{
|
|
48
|
-
console.log(report)
|
|
101
|
+
console.log('finish callback', report);
|
|
49
102
|
}
|
|
50
103
|
});
|
|
51
104
|
|
|
52
105
|
```
|
|
53
106
|
|
|
54
|
-
Or the older way, to provide the Producer & Consumer
|
|
55
|
-
|
|
56
|
-
```javascript
|
|
57
|
-
let master = require('multi-tasks').master;
|
|
58
|
-
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
|
|
59
|
-
let RunnerConsumer;//See examples, please provide a Consumer class to process a certain sub-task and return the processed data
|
|
60
|
-
|
|
61
|
-
master.start(RunnerProducer, RunnerConsumer, {
|
|
62
|
-
multi_task_parent_folder,
|
|
63
|
-
quotaOfEachTask,
|
|
64
|
-
numberOfWorkers,
|
|
65
|
-
onFinish
|
|
66
|
-
});
|
|
67
|
-
```
|
|
68
|
-
|
|
69
107
|
Have a try:
|
|
70
108
|
|
|
71
109
|
```shell
|
|
@@ -75,6 +113,8 @@ node examples/example1/run
|
|
|
75
113
|
|
|
76
114
|
Changelog:
|
|
77
115
|
|
|
116
|
+
- 2.0.1 Avoid possible I/O conflicts.
|
|
117
|
+
- 2.0.0 Rewritten with a new architecture to support dynamic tasks.
|
|
78
118
|
- 1.2.8 Fix: create task folder failed on MacOS
|
|
79
119
|
- 1.2.7 Small updates
|
|
80
120
|
- 1.2.6 Support onFinish event
|
package/cmd.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
import readline from 'readline';
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
// 初始化数值
|
|
6
|
+
let values = [0, 0, 0];
|
|
7
|
+
|
|
8
|
+
// 更新并显示数值的函数
|
|
9
|
+
function updateAndDisplayValues() {
|
|
10
|
+
// 增加一些随机值
|
|
11
|
+
values = values.map(value => value + Math.floor(Math.random() * 10));
|
|
12
|
+
|
|
13
|
+
// 清除当前行并将光标移回行首
|
|
14
|
+
process.stdout.clearLine();
|
|
15
|
+
process.stdout.cursorTo(0);
|
|
16
|
+
|
|
17
|
+
// 显示更新后的数值,不换行
|
|
18
|
+
process.stdout.write(chalk.green(`Value 1: ${values[0]}\t`));
|
|
19
|
+
process.stdout.write(chalk.yellow(`Value 2: ${values[1]}\t`));
|
|
20
|
+
process.stdout.write(chalk.blue(`Value 3: ${values[2]}\r`)); // \r 将光标移回行首,准备下一次输出
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// 创建readline接口实例以监听键盘输入
|
|
24
|
+
const rl = readline.createInterface({
|
|
25
|
+
input: process.stdin,
|
|
26
|
+
output: process.stdout
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// 每秒更新和显示数值
|
|
30
|
+
setInterval(updateAndDisplayValues, 1000);
|
|
31
|
+
|
|
32
|
+
// 监听'q'键退出
|
|
33
|
+
rl.on('line', (input) => {
|
|
34
|
+
if (input.trim().toLowerCase() === 'q') {
|
|
35
|
+
clearInterval(intervalId); // 清除定时器
|
|
36
|
+
rl.close(); // 关闭readline接口
|
|
37
|
+
process.exit(0); // 退出程序
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// 存储定时器的ID,以便稍后清除它
|
|
42
|
+
let intervalId = setInterval(updateAndDisplayValues, 1000);
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
let multiTasks = require('../../index').multiTasks;
|
|
2
|
+
|
|
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
|
+
let alltasks = [];
|
|
5
|
+
for(let i=0;i<50;i++){
|
|
6
|
+
alltasks.push({
|
|
7
|
+
name: `task-${i}`,
|
|
8
|
+
data: `This prop is for a subtask`
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
//Step2, Provide a function to process a certain sub-task and return the result data
|
|
13
|
+
let processTask = (task, helper)=>{
|
|
14
|
+
let {taskCount} = task;
|
|
15
|
+
|
|
16
|
+
return 'a result which is not a promise';
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
//Step2.1, or return a promise
|
|
20
|
+
let processTask = (task, helper)=>{
|
|
21
|
+
let {taskCount} = task;
|
|
22
|
+
|
|
23
|
+
return new Promise((resolve, reject)=>{
|
|
24
|
+
resolve({
|
|
25
|
+
data:`task${taskCount} complete`
|
|
26
|
+
})
|
|
27
|
+
})
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
//Step2.2, dynamically create a new task if find something new while processing
|
|
31
|
+
let processTask = (task, helper)=>{
|
|
32
|
+
let {taskCount} = task;
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
if(taskCount % 2 === 0){
|
|
36
|
+
helper.createNewTasks({//You can dynamically create a new task if find something new while processing
|
|
37
|
+
msg:'a new task'
|
|
38
|
+
});
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return new Promise((resolve, reject)=>{
|
|
43
|
+
resolve({
|
|
44
|
+
data:`task${taskCount} complete`
|
|
45
|
+
})
|
|
46
|
+
})
|
|
47
|
+
};
|
|
48
|
+
//Step2.3, this example shows how to handle exceptions/errors
|
|
49
|
+
let processTask = (task, helper)=>{
|
|
50
|
+
let {taskCount} = task;
|
|
51
|
+
|
|
52
|
+
//This is the demo of exceptions/errors, they will be captured and saved in the results/errors folder
|
|
53
|
+
if(taskCount===3) throw 'exception';
|
|
54
|
+
if(taskCount===4) return Promise.reject({err:'a test error'});
|
|
55
|
+
if(taskCount===5) aaa = bbb;
|
|
56
|
+
|
|
57
|
+
if(taskCount===6) return 'a result which is not a promise'; //Return a non-promise result is OK
|
|
58
|
+
|
|
59
|
+
//by default, you should return a promise, all returned data can be found in the results/succ folder
|
|
60
|
+
//but to return a non-promise result is also OK, see above
|
|
61
|
+
return new Promise((resolve, reject)=>{
|
|
62
|
+
setTimeout(()=>{
|
|
63
|
+
if(taskCount % 2 === 0){
|
|
64
|
+
helper.createNewTasks({//You can dynamically create a new task if find something new while processing
|
|
65
|
+
msg:'a new task'
|
|
66
|
+
});
|
|
67
|
+
resolve()
|
|
68
|
+
}else{
|
|
69
|
+
resolve({
|
|
70
|
+
data:`task${taskCount} complete`
|
|
71
|
+
})
|
|
72
|
+
}
|
|
73
|
+
}, 10)
|
|
74
|
+
})
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
//Step3, run!
|
|
78
|
+
multiTasks({
|
|
79
|
+
tasks: alltasks,
|
|
80
|
+
processTask,
|
|
81
|
+
taskRootFolder: `../examples-tmp-data/example0`, //a directory to store progress and results files, you can check the progress here
|
|
82
|
+
taskId: 'my-task',
|
|
83
|
+
numberOfWorkers: 3, //Assign how many workers are working in parallel
|
|
84
|
+
//autoCloseAfterCompletion: true, //if you have dynamically generated new tasks, put this as false
|
|
85
|
+
onFinish: (report)=>{
|
|
86
|
+
console.log('finish callback', report);
|
|
87
|
+
}
|
|
88
|
+
});
|
package/examples/example0/run.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
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<
|
|
5
|
+
for(let i=0;i<50;i++){
|
|
6
6
|
let task_props = {
|
|
7
7
|
index: i,
|
|
8
8
|
name: `task-${i}`,
|
|
@@ -11,28 +11,43 @@ for(let i=0;i<100;i++){
|
|
|
11
11
|
alltasks.push(task_props);
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
//
|
|
15
|
-
let processTask = (task)=>{
|
|
16
|
-
console.log('this is the subtask data you created above:', task);
|
|
17
|
-
let {
|
|
14
|
+
//Step2, Provide a process function to handle a certain sub-task and return the result data
|
|
15
|
+
let processTask = (task, helper)=>{
|
|
16
|
+
//console.log('this is the subtask data you created above:', task);
|
|
17
|
+
let {taskCount} = task;
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
if(index===3) throw 'exception';
|
|
21
|
-
if(index===4) return Promise.reject({err:'a test error'});
|
|
19
|
+
console.log(taskCount)
|
|
22
20
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
});
|
|
21
|
+
|
|
22
|
+
//This is the demo of exceptions/errors, they will be captured and saved in the results/errors folder
|
|
23
|
+
if(taskCount===3) throw 'exception';
|
|
24
|
+
if(taskCount===4) return Promise.reject({err:'a test error'});
|
|
25
|
+
if(taskCount===5) aaa = bbb;
|
|
26
|
+
|
|
27
|
+
if(taskCount===6) return 'a result which is not a promise'; //Return a non-promise result is also OK
|
|
28
|
+
|
|
29
|
+
//by default, you should return a promise, all returned data can be found in the results/succ folder
|
|
30
|
+
return new Promise((resolve, reject)=>{
|
|
31
|
+
setTimeout(()=>{
|
|
32
|
+
if(taskCount % 2 === 0)helper.createNewTasks({//
|
|
33
|
+
msg:'a new task'
|
|
34
|
+
});
|
|
35
|
+
taskCount === 2 ? reject('a rejected error') : resolve({
|
|
36
|
+
message: 'this is a result from a demo, random data=' + Math.random()
|
|
37
|
+
});
|
|
38
|
+
}, 10 * Math.random())
|
|
39
|
+
})
|
|
27
40
|
}
|
|
28
41
|
|
|
42
|
+
//Step3, run!
|
|
29
43
|
multiTasks({
|
|
30
44
|
tasks: alltasks,
|
|
31
45
|
processTask,
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
numberOfWorkers:
|
|
46
|
+
taskRootFolder: `../examples-tmp-data/example0`, //a directory to store progress and results files, you can check the progress here
|
|
47
|
+
taskId: 'my-task',
|
|
48
|
+
numberOfWorkers: 3, //Assign how many workers are working in parallel
|
|
49
|
+
autoCloseAfterCompletion: true,
|
|
35
50
|
onFinish: (report)=>{
|
|
36
|
-
console.log(report);
|
|
51
|
+
console.log('finish callback', report);
|
|
37
52
|
}
|
|
38
53
|
});
|
package/index.js
CHANGED
|
@@ -1,37 +1,4 @@
|
|
|
1
|
-
const
|
|
2
|
-
const SuperConsumer = require('./workers/SuperConsumer');
|
|
3
|
-
const master = require('./workers/master');
|
|
4
|
-
const multiTasks = (config)=>{
|
|
5
|
-
if(!config.tasks){
|
|
6
|
-
throw 'Please provide tasks data';
|
|
7
|
-
};
|
|
8
|
-
if(!config.processTask){
|
|
9
|
-
throw 'Please provide processTask function';
|
|
10
|
-
};
|
|
11
|
-
class TestProducer extends SuperProducer {
|
|
12
|
-
// constructor
|
|
13
|
-
setTaskTrunkInfo(trunkInfo){
|
|
14
|
-
this.trunkInfo = trunkInfo;
|
|
15
|
-
}
|
|
16
|
-
getAllActions() {
|
|
17
|
-
return Promise.resolve(config.tasks);//return all your tasks as an array
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
class TestConsumer extends SuperConsumer {
|
|
21
|
-
processTask(taskProp){
|
|
22
|
-
console.log(`processing data:`, taskProp);
|
|
23
|
-
let result = config.processTask(taskProp);
|
|
24
|
-
return Promise.resolve(result);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
master.start(TestProducer, TestConsumer, {
|
|
29
|
-
...config
|
|
30
|
-
});
|
|
31
|
-
};
|
|
1
|
+
const multiTasks = require('./workers');
|
|
32
2
|
module.exports = {
|
|
33
|
-
SuperProducer,
|
|
34
|
-
SuperConsumer,
|
|
35
|
-
master,
|
|
36
3
|
multiTasks
|
|
37
4
|
}
|
package/monitors/cli.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const cliProgress = require('cli-progress');
|
|
2
|
+
|
|
3
|
+
// create new container
|
|
4
|
+
const multibar = new cliProgress.MultiBar({
|
|
5
|
+
clearOnComplete: false,
|
|
6
|
+
hideCursor: true,
|
|
7
|
+
format: ' {bar} | {filename} | {value}/{total}',
|
|
8
|
+
}, cliProgress.Presets.shades_grey);
|
|
9
|
+
|
|
10
|
+
// add bars
|
|
11
|
+
const b1 = multibar.create(200, 0);
|
|
12
|
+
const b2 = multibar.create(1000, 0);
|
|
13
|
+
|
|
14
|
+
// control bars
|
|
15
|
+
b1.increment();
|
|
16
|
+
b2.update(20, {filename: "test1.txt"});
|
|
17
|
+
b1.update(20, {filename: "helloworld.txt"});
|
|
18
|
+
|
|
19
|
+
b2.setTotal(2000);
|
|
20
|
+
let v = 200;
|
|
21
|
+
setInterval(()=>{
|
|
22
|
+
v=v+20
|
|
23
|
+
b2.update(v, {filename: "2222.txt"});
|
|
24
|
+
|
|
25
|
+
// stop all bars
|
|
26
|
+
|
|
27
|
+
//multibar.stop();
|
|
28
|
+
}, 300)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "multi-tasks",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"directories": {
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
"author": "zhanglei923@gmail.com",
|
|
18
18
|
"license": "MIT",
|
|
19
19
|
"devDependencies": {
|
|
20
|
+
"cli-progress": "^3.12.0",
|
|
20
21
|
"jest": "^29.7.0"
|
|
21
22
|
}
|
|
22
23
|
}
|
package/todo.txt
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
class AsyncQueue {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.queue = [];
|
|
4
|
+
this.isProcessing = false;
|
|
5
|
+
}
|
|
6
|
+
enqueue(task) {
|
|
7
|
+
this.queue.push(task);
|
|
8
|
+
|
|
9
|
+
if (!this.isProcessing) {
|
|
10
|
+
this.processNext();
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
async processNext() {
|
|
14
|
+
this.isProcessing = true;
|
|
15
|
+
if (this.queue.length === 0) {
|
|
16
|
+
this.isProcessing = false;
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const task = this.queue.shift();
|
|
20
|
+
let result = task();
|
|
21
|
+
if(!result instanceof Promise) result = Promise.resolve(result);
|
|
22
|
+
|
|
23
|
+
try {
|
|
24
|
+
await result;
|
|
25
|
+
} catch (error) {
|
|
26
|
+
console.error('Error processing task:', error);
|
|
27
|
+
}
|
|
28
|
+
this.processNext(); // 处理完一个任务后,继续处理下一个任务
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
module.exports = AsyncQueue;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
function v1tov2(config) {
|
|
2
|
+
if(config.multi_task_parent_folder) config.taskRootFolder = config.multi_task_parent_folder;//backward compatible
|
|
3
|
+
delete config.multi_task_parent_folder;
|
|
4
|
+
|
|
5
|
+
if(config.numberOfWorks && !config.numberOfWorkers) config.numberOfWorkers = config.numberOfWorks; //Backward compatible with the wrong variable name "numberOfWorks"
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
return config;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
module.exports = {v1tov2};
|
package/utils/makedir.js
CHANGED
|
@@ -25,5 +25,27 @@ function sync(directoryPath) {
|
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
|
+
function getDirectFiles(dir, callback) {
|
|
29
|
+
let childrenfiles = [];
|
|
30
|
+
if(!fs.existsSync(dir)){
|
|
31
|
+
console.log('FATAL: directory is missing:', dir);
|
|
32
|
+
return process.exit(1);
|
|
33
|
+
}
|
|
34
|
+
fs.readdir(dir, (err, files) => {
|
|
35
|
+
if (err) {
|
|
36
|
+
console.error("Could not list the directory.", err);
|
|
37
|
+
return callback([]);
|
|
38
|
+
}
|
|
39
|
+
files.forEach(file => {
|
|
40
|
+
const filePath = path.join(dir, file);
|
|
41
|
+
//console.log(filePath)
|
|
42
|
+
let stats = fs.statSync(filePath);
|
|
43
|
+
if (stats.isFile()) {
|
|
44
|
+
childrenfiles.push(file);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
callback(childrenfiles);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
28
50
|
|
|
29
|
-
module.exports = {sync};
|
|
51
|
+
module.exports = {sync, getDirectFiles};
|
package/utils/randoms.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
|
|
2
|
+
const fmtDigit = (n)=>{
|
|
3
|
+
return n > 9 ? "" + n: "0" + n;
|
|
4
|
+
}
|
|
5
|
+
const getDateTimeTxt = ()=>{
|
|
6
|
+
let d=new Date();
|
|
7
|
+
let YYYY=d.getFullYear()
|
|
8
|
+
let MM=fmtDigit(d.getMonth()+1);
|
|
9
|
+
let DD=fmtDigit(d.getDate());
|
|
10
|
+
let HH=fmtDigit(d.getHours());
|
|
11
|
+
let mm=fmtDigit(d.getMinutes());
|
|
12
|
+
let ss=fmtDigit(d.getSeconds());
|
|
13
|
+
return `${YYYY}-${MM}-${DD}_${HH}-${mm}-${ss}`;
|
|
14
|
+
};
|
|
15
|
+
module.exports = {getDateTimeTxt};
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
const AsyncQueue = require(`../asyncQueue`);
|
|
2
|
+
const queue = new AsyncQueue();
|
|
3
|
+
|
|
4
|
+
let count=0;
|
|
5
|
+
|
|
6
|
+
queue.enqueue(async () => {
|
|
7
|
+
count++;
|
|
8
|
+
console.log(`Task ${count} start`);
|
|
9
|
+
await new Promise(resolve => setTimeout(resolve, 100));
|
|
10
|
+
console.log(`Task ${count} end`);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
queue.enqueue(async () => {
|
|
15
|
+
count++;
|
|
16
|
+
console.log(`Task ${count} start`);
|
|
17
|
+
await new Promise(resolve => setTimeout(resolve, 20));
|
|
18
|
+
console.log(`Task ${count} end`);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
queue.enqueue(() => {
|
|
23
|
+
count++;
|
|
24
|
+
console.log(`Task ${count} start`);
|
|
25
|
+
console.log(`Task ${count} end`);
|
|
26
|
+
return 11;
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
queue.enqueue(() => {
|
|
31
|
+
count++;
|
|
32
|
+
console.log(`Task ${count} start`);
|
|
33
|
+
console.log(`Task ${count} end`);
|
|
34
|
+
return 11;
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
queue.enqueue(() => {
|
|
38
|
+
count++;
|
|
39
|
+
console.log(`Task ${count} start`);
|
|
40
|
+
console.log(`Task ${count} end`);
|
|
41
|
+
return 11;
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
queue.enqueue(() => {
|
|
45
|
+
count++;
|
|
46
|
+
console.log(`Task ${count} start`);
|
|
47
|
+
console.log(`Task ${count} end`);
|
|
48
|
+
return 11;
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
queue.enqueue(() => {
|
|
52
|
+
count++;
|
|
53
|
+
console.log(`Task ${count} start`);
|
|
54
|
+
console.log(`Task ${count} end`);
|
|
55
|
+
return 11;
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
queue.enqueue(() => {
|
|
59
|
+
count++;
|
|
60
|
+
console.log(`Task ${count} start`);
|
|
61
|
+
console.log(`Task ${count} end`);
|
|
62
|
+
return 11;
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
queue.enqueue(async () => {
|
|
66
|
+
count++;
|
|
67
|
+
console.log(`Task ${count} start`);
|
|
68
|
+
await new Promise(resolve => setTimeout(resolve, 20));
|
|
69
|
+
console.log(`Task ${count} end`);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
queue.enqueue(async () => {
|
|
75
|
+
count++;
|
|
76
|
+
console.log(`Task ${count} start`);
|
|
77
|
+
await new Promise(resolve => setTimeout(resolve, 20));
|
|
78
|
+
console.log(`Task ${count} end`);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
queue.enqueue(async () => {
|
|
84
|
+
count++;
|
|
85
|
+
console.log(`Task ${count} start`);
|
|
86
|
+
await new Promise(resolve => setTimeout(resolve, 20));
|
|
87
|
+
console.log(`Task ${count} end`);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
queue.enqueue(async () => {
|
|
93
|
+
count++;
|
|
94
|
+
console.log(`Task ${count} start`);
|
|
95
|
+
await new Promise(resolve => setTimeout(resolve, 20));
|
|
96
|
+
console.log(`Task ${count} end`);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
queue.enqueue(() => {
|
|
101
|
+
count++;
|
|
102
|
+
console.log(`Task ${count} start`);
|
|
103
|
+
console.log(`Task ${count} end`);
|
|
104
|
+
return 11;
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
queue.enqueue(async () => {
|
|
109
|
+
count++;
|
|
110
|
+
console.log(`Task ${count} start`);
|
|
111
|
+
await new Promise(resolve => setTimeout(resolve, 20));
|
|
112
|
+
console.log(`Task ${count} end`);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
|