concurrency.js 0.0.2 → 0.0.3-beta
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 +117 -1
- package/demos/cluster.js +22 -0
- package/demos/demos.cluster.js +66 -0
- package/demos/demos.js +5 -5
- package/demos/demos.process.js +37 -0
- package/demos/demos.threads.async.js +22 -0
- package/demos/demos.threads.js +40 -0
- package/index.js +15 -4
- package/index.mjs +7 -8
- package/package.json +4 -4
- package/src/worker.cluster.js +122 -0
- package/src/worker.process.js +114 -43
- package/src/worker.thread.async.js +84 -0
- package/src/worker.threads.js +167 -8
- package/test/_.test-template.js +3 -3
- package/test/test_demos_cluster.js +109 -0
- package/test/test_demos_process.js +49 -0
- package/test/test_demos_promise.js +38 -0
- package/test/test_demos_threads.js +59 -0
- package/test/test_demos_threads_async.js +68 -0
- package/src/concurrency.js +0 -97
package/README.md
CHANGED
|
@@ -1,3 +1,119 @@
|
|
|
1
1
|
# concurrency.js
|
|
2
|
+
|
|
2
3
|
npm module to work with concurrency - worker threads and worker processes easily using simple functions and script files
|
|
3
|
-
|
|
4
|
+
|
|
5
|
+
Find the demos in the [demos folder](./demos)
|
|
6
|
+
|
|
7
|
+
#### Cluster Methods
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
`_concurrencyClusters(filename = __filename, num = cpus().length, options = {}, greet = false)`
|
|
11
|
+
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
const path = require("path");
|
|
15
|
+
let { _concurrencyClusters } = require("concurrency.js");
|
|
16
|
+
|
|
17
|
+
function concurrency() {
|
|
18
|
+
return new Promise(function (resolve, reject) {
|
|
19
|
+
_concurrencyClusters(
|
|
20
|
+
path.join("C:\\Users\\GB\\Documents\\projects\\requireurl\\concurrency\\src\\worker.cluster.js"),
|
|
21
|
+
8,
|
|
22
|
+
{
|
|
23
|
+
data: {
|
|
24
|
+
data: "Testing parent data",
|
|
25
|
+
url: "https://www.google.com"
|
|
26
|
+
},
|
|
27
|
+
childData: "Test data from child"
|
|
28
|
+
}
|
|
29
|
+
).then((d) => {
|
|
30
|
+
console.log("Data fetched", JSON.stringify(d));
|
|
31
|
+
resolve(d);
|
|
32
|
+
}).catch((e) => {
|
|
33
|
+
console.log(e.toString());
|
|
34
|
+
reject(e);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
concurrency();
|
|
39
|
+
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
#### Process Methods
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
`_concurrencyProcesses(filename = __filename, options = {}, greet = false)`
|
|
46
|
+
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
const path = require("path");
|
|
50
|
+
let { _concurrencyProcesses } = require("concurrency.js");
|
|
51
|
+
_concurrencyProcesses(
|
|
52
|
+
path.join(
|
|
53
|
+
"C:\\Users\\GB\\Documents\\projects\\requireurl\\concurrency\\src\\worker.process.js"),
|
|
54
|
+
{
|
|
55
|
+
data: {
|
|
56
|
+
message: "Testing data",
|
|
57
|
+
url: "https://www.google.com"
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
true
|
|
61
|
+
).then((d) => { console.log("Data fetched: ", JSON.stringify(d)); })
|
|
62
|
+
.catch((e) => { console.log(e.toString()); setTimeout(() => { process.exit(e); }, 5000) })
|
|
63
|
+
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
#### Threads Methods
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
`_concurrencyThreads(filename = __filename, options = {}, greet = false)`
|
|
70
|
+
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
const path = require("path");
|
|
74
|
+
let { _concurrencyThreads } = require("concurrency.js");
|
|
75
|
+
_concurrencyThreads(
|
|
76
|
+
__filename,
|
|
77
|
+
{
|
|
78
|
+
data: {
|
|
79
|
+
url: "https://www.google.com",
|
|
80
|
+
data: "Testing data"
|
|
81
|
+
},
|
|
82
|
+
childData: "Testing child data"
|
|
83
|
+
},
|
|
84
|
+
true
|
|
85
|
+
).then((d) => console.log(JSON.stringify(d)));
|
|
86
|
+
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
#### Thread Async Methods
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
`_concurrencyThreadsAsync(command, options)`
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
const path = require("path");
|
|
98
|
+
let { _concurrencyThreadsAsync } = require("concurrency.js");
|
|
99
|
+
|
|
100
|
+
let threads = _concurrencyThreadsAsync(
|
|
101
|
+
"C:\\Users\\GB\\Documents\\projects\\requireurl\\concurrency\\demos\\demos.threads.js",
|
|
102
|
+
{
|
|
103
|
+
data: {
|
|
104
|
+
data: "Testing parent data",
|
|
105
|
+
url: "https://www.google.com"
|
|
106
|
+
},
|
|
107
|
+
childData: "Test data from child"
|
|
108
|
+
}
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### Contributions
|
|
114
|
+
|
|
115
|
+
Contributions, Feature Improvements, Bugs, and Issues are invited. [raising an issue](https://github.com/ganeshkbhat/concurrency.js/issues)
|
|
116
|
+
|
|
117
|
+
# License
|
|
118
|
+
|
|
119
|
+
[MIT License](./LICENSE)
|
package/demos/cluster.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const { _concurrencyClusters } = require("../index.js");
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
async function cluster() {
|
|
7
|
+
let filename = "C:\\Users\\GB\\Documents\\projects\\requireurl\\concurrency\\src\\worker.cluster.js";
|
|
8
|
+
return _concurrencyClusters(
|
|
9
|
+
path.join(filename),
|
|
10
|
+
// __filename,
|
|
11
|
+
8,
|
|
12
|
+
{
|
|
13
|
+
data: {
|
|
14
|
+
url: "https://www.google.com",
|
|
15
|
+
message: "Testing parent data"
|
|
16
|
+
},
|
|
17
|
+
childData: "Test data from child"
|
|
18
|
+
}
|
|
19
|
+
)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
module.exports = cluster;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
|
|
2
|
+
const path = require("path");
|
|
3
|
+
let { _concurrencyClusters } = require("../index.js");
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
// console.log(_concurrencyClusters(
|
|
7
|
+
// path.join("C:\\Users\\GB\\Documents\\projects\\requireurl\\concurrency\\src\\worker.process.js"),
|
|
8
|
+
// 8,
|
|
9
|
+
// { url: "https://www.google.com", data: "Testing parent data", childData: "Test data from child" }
|
|
10
|
+
// ).then((d) => {
|
|
11
|
+
// console.log("Data fetched", JSON.stringify(d));
|
|
12
|
+
// }).catch((e) => {
|
|
13
|
+
// console.log(e.toString());
|
|
14
|
+
// }))
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
// async function concurrency() {
|
|
20
|
+
// let result = await _concurrencyClusters(
|
|
21
|
+
// path.join("C:\\Users\\GB\\Documents\\projects\\requireurl\\concurrency\\src\\worker.cluster.js"),
|
|
22
|
+
// 8,
|
|
23
|
+
// { url: "https://www.google.com", data: "Testing parent data", childData: "Test data from child" }
|
|
24
|
+
// )
|
|
25
|
+
// console.log(result);
|
|
26
|
+
// }
|
|
27
|
+
|
|
28
|
+
// function concurrency() {
|
|
29
|
+
// _concurrencyClusters(
|
|
30
|
+
// path.join("C:\\Users\\GB\\Documents\\projects\\requireurl\\concurrency\\src\\worker.cluster.js"),
|
|
31
|
+
// 8,
|
|
32
|
+
// { url: "https://www.google.com", data: "Testing parent data", childData: "Test data from child" }
|
|
33
|
+
// ).then((d) => {
|
|
34
|
+
// console.log("Data fetched", JSON.stringify(d));
|
|
35
|
+
// }).catch((e) => {
|
|
36
|
+
// console.log(e.toString());
|
|
37
|
+
// })
|
|
38
|
+
// }
|
|
39
|
+
|
|
40
|
+
function concurrency() {
|
|
41
|
+
let filename = "C:\\Users\\GB\\Documents\\projects\\requireurl\\concurrency\\src\\worker.cluster.js";
|
|
42
|
+
return new Promise(function (resolve, reject) {
|
|
43
|
+
_concurrencyClusters(
|
|
44
|
+
path.join(filename),
|
|
45
|
+
8,
|
|
46
|
+
{
|
|
47
|
+
data: {
|
|
48
|
+
message: "Testing parent data",
|
|
49
|
+
url: "https://www.google.com",
|
|
50
|
+
},
|
|
51
|
+
childData: "Test data from child"
|
|
52
|
+
}
|
|
53
|
+
).then((d) => {
|
|
54
|
+
console.log("Data fetched", JSON.stringify(d));
|
|
55
|
+
resolve(d);
|
|
56
|
+
}).catch((e) => {
|
|
57
|
+
console.log(e.toString());
|
|
58
|
+
reject(e);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
concurrency();
|
|
64
|
+
|
|
65
|
+
setTimeout(() => console.log(`demo.cluster.js: run file PID ${process.pid}: Interval 2: 10000 `, process.pid), 10000);
|
|
66
|
+
setTimeout(() => console.log(`demo.cluster.js: Closing process ${process.pid}: Timeout 1: 10000 `, process.exit()), 20000);
|
package/demos/demos.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
*
|
|
3
|
-
* Package:
|
|
3
|
+
* Package: concurrency.js
|
|
4
4
|
* Author: Ganesh B
|
|
5
|
-
* Description:
|
|
6
|
-
* Install: npm i
|
|
5
|
+
* Description: npm module to work with concurrency - worker threads and worker processes easily using simple functions and script files
|
|
6
|
+
* Install: npm i concurrency.js --save
|
|
7
7
|
* Github: https://github.com/ganeshkbhat/concurrency
|
|
8
8
|
* npmjs Link: https://www.npmjs.com/package/
|
|
9
9
|
* File: index.js
|
|
@@ -16,6 +16,6 @@
|
|
|
16
16
|
'use strict';
|
|
17
17
|
|
|
18
18
|
|
|
19
|
-
let { _concurrencyThreads, _concurrencyProcesses } = require("../index.js");
|
|
20
|
-
console.log(_concurrencyThreads, _concurrencyProcesses);
|
|
19
|
+
let { _concurrencyThreads, _concurrencyProcesses, _concurrencyClusters, _concurrencyThreadsAsync } = require("../index.js");
|
|
20
|
+
console.log(_concurrencyThreads, _concurrencyProcesses, _concurrencyClusters, _concurrencyThreadsAsync);
|
|
21
21
|
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* Package: concurrency.js
|
|
4
|
+
* Author: Ganesh B
|
|
5
|
+
* Description: npm module to work with concurrency - worker threads and worker processes easily using simple functions and script files
|
|
6
|
+
* Install: npm i concurrency.js --save
|
|
7
|
+
* Github: https://github.com/ganeshkbhat/concurrency
|
|
8
|
+
* npmjs Link: https://www.npmjs.com/package/
|
|
9
|
+
* File: demo.processes.js
|
|
10
|
+
* File Description:
|
|
11
|
+
*
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/* eslint no-console: 0 */
|
|
15
|
+
|
|
16
|
+
'use strict';
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
const path = require("path");
|
|
20
|
+
let { _concurrencyProcesses } = require("../index.js");
|
|
21
|
+
|
|
22
|
+
let filename = "C:\\Users\\GB\\Documents\\projects\\requireurl\\concurrency\\src\\worker.process.js";
|
|
23
|
+
_concurrencyProcesses(
|
|
24
|
+
path.join(filename), {
|
|
25
|
+
data: {
|
|
26
|
+
message: "Testing data",
|
|
27
|
+
url: "https://www.google.com"
|
|
28
|
+
}
|
|
29
|
+
}, true).then((d) => {
|
|
30
|
+
console.log("Data fetched: ", JSON.stringify(d));
|
|
31
|
+
}).catch((e) => {
|
|
32
|
+
console.log(e.toString()); setTimeout(() => { process.exit(e); }, 5000)
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
setTimeout(() => console.log(`demo.processes.js: Run file PID ${process.pid}: Interval 2: 10000 `, process.pid), 10000);
|
|
36
|
+
setTimeout(() => console.log(`demo.processes.js: Closing process ${process.pid}: Timeout 1: 10000 `, process.exit()), 20000);
|
|
37
|
+
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
const path = require("path");
|
|
4
|
+
let { _concurrencyThreadsAsync } = require("../index.js");
|
|
5
|
+
|
|
6
|
+
let filename = "C:\\Users\\GB\\Documents\\projects\\requireurl\\concurrency\\demos\\demos.threads.js";
|
|
7
|
+
|
|
8
|
+
let threads = _concurrencyThreadsAsync(filename, {
|
|
9
|
+
data: {
|
|
10
|
+
message: "Testing parent data",
|
|
11
|
+
url: "https://www.google.com"
|
|
12
|
+
},
|
|
13
|
+
childData: "Test data from child"
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
console.log(` STDOUT: console.log(threads.stderr); console.log(threads.stdout); `);
|
|
17
|
+
|
|
18
|
+
console.log(threads.stderr);
|
|
19
|
+
console.log(threads.stdout);
|
|
20
|
+
|
|
21
|
+
// setTimeout(() => console.log(`demo.cluster.js: run file PID ${process.pid}: Interval 2: 10000 `, process.pid), 10000);
|
|
22
|
+
// setTimeout(() => console.log(`demo.cluster.js: Closing process ${process.pid}: Timeout 1: 10000 `, process.exit()), 20000);
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* Package: concurrency.js
|
|
4
|
+
* Author: Ganesh B
|
|
5
|
+
* Description: npm module to work with concurrency - worker threads and worker processes easily using simple functions and script files
|
|
6
|
+
* Install: npm i concurrency.js --save
|
|
7
|
+
* Github: https://github.com/ganeshkbhat/concurrency
|
|
8
|
+
* npmjs Link: https://www.npmjs.com/package/
|
|
9
|
+
* File: demo.threads.js
|
|
10
|
+
* File Description:
|
|
11
|
+
*
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/* eslint no-console: 0 */
|
|
15
|
+
|
|
16
|
+
'use strict';
|
|
17
|
+
|
|
18
|
+
const path = require("path");
|
|
19
|
+
let { _concurrencyThreads } = require("../index.js");
|
|
20
|
+
|
|
21
|
+
// _concurrencyThreads(path.join("C:\\Users\\GB\\Documents\\projects\\requireurl\\concurrency\\src\\worker.threads.js"), { data: { url: "https://www.google.com", message: "Testing data" } });
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
_concurrencyThreads(__filename, {
|
|
25
|
+
data: {
|
|
26
|
+
url: "https://www.google.com",
|
|
27
|
+
message: "Testing data"
|
|
28
|
+
},
|
|
29
|
+
childData: "Testing child data"
|
|
30
|
+
}, true).then((d) => console.log(JSON.stringify(d)))
|
|
31
|
+
// .catch((e) => { console.log(e.toString()); setTimeout(() => {process.exit(e);}, 5000) })
|
|
32
|
+
|
|
33
|
+
// setTimeout(() => {
|
|
34
|
+
// console.log(`demo.threads.js: Closing process ${process.pid}: Timeout 1: 20000 `, __filename);
|
|
35
|
+
// process.exit(0);
|
|
36
|
+
// }, 20000);
|
|
37
|
+
|
|
38
|
+
setTimeout(() => console.log(`demo.processes.js: Run file PID ${process.pid}: Interval 2: 10000 `, process.pid), 10000);
|
|
39
|
+
setTimeout(() => console.log(`demo.processes.js: Closing process ${process.pid}: Timeout 1: 10000 `, process.exit()), 20000);
|
|
40
|
+
|
package/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
*
|
|
3
|
-
* Package:
|
|
3
|
+
* Package: concurrency.js
|
|
4
4
|
* Author: Ganesh B
|
|
5
|
-
* Description:
|
|
6
|
-
* Install: npm i
|
|
5
|
+
* Description: npm module to work with concurrency - worker threads and worker processes easily using simple functions and script files
|
|
6
|
+
* Install: npm i concurrency.js --save
|
|
7
7
|
* Github: https://github.com/ganeshkbhat/concurrency
|
|
8
8
|
* npmjs Link: https://www.npmjs.com/package/
|
|
9
9
|
* File: index.js
|
|
@@ -16,9 +16,20 @@
|
|
|
16
16
|
'use strict';
|
|
17
17
|
|
|
18
18
|
|
|
19
|
-
const { _concurrencyThreads
|
|
19
|
+
const { _concurrencyThreads } = require("./src/worker.threads.js");
|
|
20
|
+
const { _concurrencyProcesses } = require("./src/worker.process.js");
|
|
21
|
+
const { _concurrencyClusters } = require("./src/worker.cluster.js");
|
|
22
|
+
const { _concurrencyThreadsAsync } = require("./src/worker.thread.async.js");
|
|
20
23
|
|
|
21
24
|
|
|
22
25
|
module.exports._concurrencyThreads = _concurrencyThreads;
|
|
23
26
|
module.exports._concurrencyProcesses = _concurrencyProcesses;
|
|
27
|
+
module.exports._concurrencyClusters = _concurrencyClusters;
|
|
28
|
+
module.exports._concurrencyThreadsAsync = _concurrencyThreadsAsync;
|
|
24
29
|
|
|
30
|
+
module.exports.default = {
|
|
31
|
+
_concurrencyThreads,
|
|
32
|
+
_concurrencyProcesses,
|
|
33
|
+
_concurrencyClusters,
|
|
34
|
+
_concurrencyThreadsAsync
|
|
35
|
+
};
|
package/index.mjs
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
*
|
|
3
|
-
* Package:
|
|
3
|
+
* Package: concurrency.js
|
|
4
4
|
* Author: Ganesh B
|
|
5
|
-
* Description:
|
|
6
|
-
* Install: npm i
|
|
5
|
+
* Description: npm module to work with concurrency - worker threads and worker processes easily using simple functions and script files
|
|
6
|
+
* Install: npm i concurrency.js --save
|
|
7
7
|
* Github: https://github.com/ganeshkbhat/concurrency
|
|
8
8
|
* npmjs Link: https://www.npmjs.com/package/
|
|
9
|
-
* File: index.
|
|
9
|
+
* File: index.mjs
|
|
10
10
|
* File Description:
|
|
11
11
|
*
|
|
12
12
|
*/
|
|
@@ -15,8 +15,7 @@
|
|
|
15
15
|
|
|
16
16
|
'use strict';
|
|
17
17
|
|
|
18
|
-
import { _concurrencyThreads, _concurrencyProcesses } from "./
|
|
19
|
-
|
|
20
|
-
export { _concurrencyThreads, _concurrencyProcesses }
|
|
21
|
-
export default _concurrencyThreads
|
|
18
|
+
import { _concurrencyThreads, _concurrencyProcesses, _concurrencyThreadsAsync, _concurrencyThreadsAsync } from "./index.js";
|
|
22
19
|
|
|
20
|
+
export { _concurrencyThreads, _concurrencyProcesses, _concurrencyClusters, _concurrencyThreadsAsync };
|
|
21
|
+
export default _concurrencyProcesses;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "concurrency.js",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "npm module to work with concurrency - worker threads and worker processes easily using simple functions and script files",
|
|
3
|
+
"version": "0.0.3-beta",
|
|
4
|
+
"description": "npm module to work with concurrency - worker threads and worker processes (currrently only fork method) easily using simple functions and script files",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"directories": {
|
|
7
7
|
"test": "test"
|
|
@@ -10,10 +10,10 @@
|
|
|
10
10
|
"require": "./index.js",
|
|
11
11
|
"import": "./index.mjs"
|
|
12
12
|
},
|
|
13
|
-
"dependencies": {},
|
|
14
13
|
"devDependencies": {
|
|
15
14
|
"chai": "^4.3.6",
|
|
16
15
|
"mocha": "^10.0.0",
|
|
16
|
+
"npm-check": "^6.0.1",
|
|
17
17
|
"sinon": "^14.0.0",
|
|
18
18
|
"unimported": "^1.22.0"
|
|
19
19
|
},
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
},
|
|
27
27
|
"homepage": "https://github.com/ganeshkbhat/concurrency#readme",
|
|
28
28
|
"scripts": {
|
|
29
|
-
"test": "mocha --reporter spec --recursive"
|
|
29
|
+
"test": "mocha --reporter spec --recursive --timeout 60000"
|
|
30
30
|
},
|
|
31
31
|
"author": "Ganesh B",
|
|
32
32
|
"license": "MIT"
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* Package: concurrency.js
|
|
4
|
+
* Author: Ganesh B
|
|
5
|
+
* Description: npm module to work with concurrency - worker threads and worker processes easily using simple functions and script files
|
|
6
|
+
* Install: npm i concurrency.js --save
|
|
7
|
+
* Github: https://github.com/ganeshkbhat/concurrency
|
|
8
|
+
* npmjs Link: https://www.npmjs.com/package/
|
|
9
|
+
* File: worker.process.js
|
|
10
|
+
* File Description:
|
|
11
|
+
*
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/* eslint no-console: 0 */
|
|
15
|
+
|
|
16
|
+
'use strict';
|
|
17
|
+
|
|
18
|
+
const cluster = require("node:cluster");
|
|
19
|
+
const http = require("node:http");
|
|
20
|
+
const { cpus } = require("node:os");
|
|
21
|
+
const process = require("node:process");
|
|
22
|
+
|
|
23
|
+
function _concurrencyClusters(filename = __filename, num = cpus().length, options = {}, greet = false) {
|
|
24
|
+
var worker, workers = {}, result = [];
|
|
25
|
+
var messageData = {}, childMessageData = [];
|
|
26
|
+
|
|
27
|
+
if (!options.handlers) {
|
|
28
|
+
options["handlers"] = {};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return new Promise((resolve, reject) => {
|
|
32
|
+
if (cluster.isPrimary) {
|
|
33
|
+
num = num || cpus().length;
|
|
34
|
+
for (let i = 0; i < num; i++) {
|
|
35
|
+
cluster.fork(filename, { env: { ...process.env, FORK: 1, childData: options.childData, handlers: { ...options.handlers } } });
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
for (const id in cluster.workers) {
|
|
39
|
+
messageData[id] = [];
|
|
40
|
+
cluster.workers[id].on("message", (msg) => {
|
|
41
|
+
if (!messageData[id]) {
|
|
42
|
+
messageData[id] = [];
|
|
43
|
+
}
|
|
44
|
+
messageData[id].push(msg);
|
|
45
|
+
if (!!options.handlers.message) {
|
|
46
|
+
const cbFunction = require(options.handlers.message);
|
|
47
|
+
result.push({ return: cbFunction(msg), id: id, pid: process.pid, event: "message" });
|
|
48
|
+
}
|
|
49
|
+
if (!!msg.closeChild) {
|
|
50
|
+
childMessageData.push(msg);
|
|
51
|
+
cluster.workers[id].disconnect();
|
|
52
|
+
}
|
|
53
|
+
if (!Object.keys(cluster.workers).length) {
|
|
54
|
+
resolve({ message: messageData, result: result });
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
if (!!greet) {
|
|
59
|
+
cluster.workers[id].send({ pid: process.pid, message: "Message from Parent: " + process.pid.toString() });
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
(!!options.data) ? cluster.workers[id].send({ id: id, pid: process.pid, message: options.data }) : null;
|
|
63
|
+
|
|
64
|
+
cluster.workers[id].on("error", function (e) {
|
|
65
|
+
if (!!options.handlers.error) {
|
|
66
|
+
const cbFunction = require(options.handlers.error);
|
|
67
|
+
result.push({ return: cbFunction(e), id: id, pid: process.pid, event: "error" });
|
|
68
|
+
}
|
|
69
|
+
reject(e);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
cluster.workers[id].on("close", function (code, signal) {
|
|
73
|
+
if (!!options.handlers.close) {
|
|
74
|
+
let connected = cluster.workers[id].isConnected();
|
|
75
|
+
const cbFunction = require(options.handlers.close);
|
|
76
|
+
// result.push(cbFunction(code, signal, pid, connected));
|
|
77
|
+
result.push({ return: cbFunction(code, signal, pid, connected), id: id, pid: process.pid, event: "close" });
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
cluster.workers[id].on("exit", (code) => {
|
|
82
|
+
if (!!options.handlers.exit) {
|
|
83
|
+
const cbFunction = require(options.handlers.exit);
|
|
84
|
+
result.push({ return: cbFunction(code), id: id, pid: process.pid, event: "exit" });
|
|
85
|
+
}
|
|
86
|
+
if (!Object.keys(cluster.workers).length) {
|
|
87
|
+
// console.log("exit called");
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
cluster.workers[id].send({ closeChild: true })
|
|
92
|
+
}
|
|
93
|
+
} else if (cluster.isWorker) {
|
|
94
|
+
// } else {
|
|
95
|
+
// return new Promise((resolve, reject) => {
|
|
96
|
+
process.on("message", (msg) => {
|
|
97
|
+
childMessageData.push(msg);
|
|
98
|
+
// if (!!process.env.handlers.childMessage) {
|
|
99
|
+
// const childCBFunction = require(process.env.handlers.childMessage);
|
|
100
|
+
// result.push({ return: cbFunction(msg), pid: process.pid, event: "message" });
|
|
101
|
+
// }
|
|
102
|
+
if (!!msg.closeChild) {
|
|
103
|
+
process.send({ closeChild: true, pid: process.pid, childMessageData: childMessageData, result: result });
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
if (!!greet) {
|
|
108
|
+
process.send({ pid: process.pid, message: "Message from worker: " + process.pid.toString() });
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
(!!process.env.childData) ? child.send({ pid: process.pid, message: process.env.childData }) : null;
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (process.env.FORK) {
|
|
118
|
+
_concurrencyClusters();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
module.exports._concurrencyClusters = _concurrencyClusters;
|