multi-tasks 2.0.1 → 2.0.3

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.
Files changed (36) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +143 -138
  3. package/cmd.js +41 -41
  4. package/examples/example0/run-readme.js +87 -87
  5. package/examples/example0/run.js +52 -52
  6. package/examples/example1/RunnerConsumer.js +25 -25
  7. package/examples/example1/RunnerProducer.js +34 -34
  8. package/examples/example1/run.js +8 -8
  9. package/examples/example2/RunnerConsumer.js +39 -39
  10. package/examples/example2/RunnerProducer.js +34 -34
  11. package/examples/example2/run.js +22 -22
  12. package/index.js +3 -3
  13. package/monitors/cli.js +28 -28
  14. package/package.json +23 -23
  15. package/test/splitter.test.js +16 -16
  16. package/test/test_resume/step1_start_and_crash.js +56 -0
  17. package/test/test_resume/step2_resume.js +21 -0
  18. package/test/test_resume/test.sh +3 -0
  19. package/todo.txt +3 -3
  20. package/utils/asyncQueue.js +35 -31
  21. package/utils/backward-v1.js +10 -10
  22. package/utils/makedir.js +40 -50
  23. package/utils/randoms.js +21 -15
  24. package/utils/test/test_asyncqueue.js +115 -115
  25. package/utils/test_makedir.js +2 -2
  26. package/workers/Multithread.js +196 -176
  27. package/workers/TaskMgr.js +212 -181
  28. package/workers/WorkerMgr.js +30 -30
  29. package/workers/asMaster.js +122 -70
  30. package/workers/asWorker.js +13 -13
  31. package/workers/helper.js +21 -21
  32. package/workers/index.js +45 -7
  33. package/examples/example3-dynamicIncreasing/RunnerConsumer.js +0 -176
  34. package/examples/example3-dynamicIncreasing/RunnerProducer.js +0 -49
  35. package/examples/example3-dynamicIncreasing/run.js +0 -36
  36. package/examples/example3-dynamicIncreasing/util.js +0 -46
@@ -0,0 +1,21 @@
1
+ let multiTasks = require('../../index').multiTasks;
2
+
3
+ let processTask = (task, helper)=>{
4
+ let {taskCount} = task;
5
+
6
+ return 'a result which is not a promise';
7
+ };
8
+
9
+ //Step3, run!
10
+ multiTasks.resume({
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
14
+ //autoCloseAfterCompletion: true,
15
+ shouldTerminate:(info)=>{
16
+ //if(info.taskCount > 30) return true;
17
+ },
18
+ onFinish: (report)=>{
19
+ console.log('finish callback', report);
20
+ }
21
+ });
@@ -0,0 +1,3 @@
1
+ #!/bin/bash
2
+ node step1_start_and_crash
3
+ node step2_resume
package/todo.txt CHANGED
@@ -1,4 +1,4 @@
1
- Features:
2
- - Continue execution from the point of interruption
3
- - Re-run
1
+ Features:
2
+ - Continue execution from the point of interruption
3
+ - Re-run
4
4
  - Show status in command line
@@ -1,32 +1,36 @@
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
-
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
+ let isPromise = result instanceof Promise;
22
+ //console.log('r=q',isPromise)
23
+ if(!isPromise) {
24
+ console.error('[ERR]Queue functions must return a promise object');
25
+ throw 'Queue functions must return a promise object';
26
+ }
27
+ try {
28
+ await result;
29
+ } catch (error) {
30
+ console.error('Error processing task:', error);
31
+ }
32
+ this.processNext();
33
+ }
34
+ }
35
+
32
36
  module.exports = AsyncQueue;
@@ -1,11 +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
-
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
11
  module.exports = {v1tov2};
package/utils/makedir.js CHANGED
@@ -1,51 +1,41 @@
1
- const fs = require('fs');
2
- const path = require('path');
3
-
4
- function sync(directoryPath) {
5
- directoryPath = directoryPath.replace(/\\/g, '/');
6
-
7
- let isAbsPath = false;
8
- if(directoryPath.startsWith('/')){
9
- isAbsPath = true;
10
- }
11
-
12
- const directories = directoryPath.split('/');
13
-
14
- let currentPath = isAbsPath ? '/':'';
15
- for (let directory of directories) {
16
- currentPath = path.join(currentPath, directory);
17
- if (!fs.existsSync(currentPath)) {
18
- try {
19
- fs.mkdirSync(currentPath);
20
- } catch (err) {
21
- if (err.code !== 'EEXIST') {
22
- throw err;
23
- }
24
- }
25
- }
26
- }
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
- }
50
-
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+
4
+ function sync(directoryPath) {
5
+ directoryPath = directoryPath.replace(/\\/g, '/');
6
+
7
+ let isAbsPath = false;
8
+ if(directoryPath.startsWith('/')){
9
+ isAbsPath = true;
10
+ }
11
+
12
+ fs.mkdirSync(directoryPath, { recursive: true });
13
+ }
14
+ function getDirectFiles(dir, callback) {
15
+ let childrenfiles = [];
16
+ if(!fs.existsSync(dir)){
17
+ console.log('FATAL: directory is missing:', dir);
18
+ return process.exit(1);
19
+ }
20
+ fs.readdir(dir, (err, files) => {
21
+ if (err) {
22
+ console.error("Could not list the directory.", err);
23
+ return callback([]);
24
+ }
25
+ files.forEach(file => {
26
+ const filePath = path.join(dir, file);
27
+ //console.log(filePath)
28
+ try{
29
+ let stats = fs.statSync(filePath);
30
+ if (stats.isFile()) {
31
+ childrenfiles.push(file);
32
+ }
33
+ }catch(e){
34
+ //
35
+ }
36
+ });
37
+ callback(childrenfiles);
38
+ });
39
+ }
40
+
51
41
  module.exports = {sync, getDirectFiles};
package/utils/randoms.js CHANGED
@@ -1,15 +1,21 @@
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};
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
+ const get36Id = ()=>{
16
+ let num1 = (Math.random().toString().match(/\d/g).join('')*1).toString(36);
17
+ let num2 = Date.now().toString(36);
18
+ let str = num2+num1;
19
+ return str;
20
+ };
21
+ module.exports = {getDateTimeTxt, get36Id};
@@ -1,116 +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
-
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
116
 
@@ -1,3 +1,3 @@
1
- let makedir = require('./makedir');
2
- makedir.sync('c:/aaa/bb')
1
+ let makedir = require('./makedir');
2
+ makedir.sync('c:/aaa/bb')
3
3
  makedir.sync('c:/cc/dd')