concurrency.js 0.0.1 → 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/.github/ISSUE_TEMPLATE/bug-report-ticket.md +38 -0
- package/.github/ISSUE_TEMPLATE/feature-request-ticket.md +43 -0
- package/.github/ISSUE_TEMPLATE/general-request-ticket.md +20 -0
- package/.github/ISSUE_TEMPLATE/security-report-ticket.md +51 -0
- package/README.md +118 -1
- package/demos/cluster.js +22 -0
- package/demos/demos.cluster.js +66 -0
- package/demos/demos.js +21 -0
- 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 +20 -3
- package/index.mjs +8 -4
- package/package.json +14 -8
- 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
|
@@ -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;
|
package/src/worker.process.js
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:
|
|
9
|
+
* File: worker.process.js
|
|
10
10
|
* File Description:
|
|
11
11
|
*
|
|
12
12
|
*/
|
|
@@ -15,43 +15,114 @@
|
|
|
15
15
|
|
|
16
16
|
'use strict';
|
|
17
17
|
|
|
18
|
+
const path = require("path");
|
|
19
|
+
const fs = require("fs");
|
|
20
|
+
const { sign } = require("crypto");
|
|
18
21
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
// process.
|
|
40
|
-
//
|
|
41
|
-
//
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
//
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
22
|
+
|
|
23
|
+
function _concurrencyProcesses(filename = __filename, options = {}, greet = false) {
|
|
24
|
+
var messageData = [], childMessageData = [], result = [];
|
|
25
|
+
if (!options.handlers) {
|
|
26
|
+
options["handlers"] = {};
|
|
27
|
+
}
|
|
28
|
+
if (process.env.FORK) {
|
|
29
|
+
if (!options.callback) {
|
|
30
|
+
options["callback"] = function (data) {
|
|
31
|
+
childMessageData.push(data);
|
|
32
|
+
if (!!process.env.handlers.childMessage) {
|
|
33
|
+
const childCBFunction = require(process.env.handlers.childMessage);
|
|
34
|
+
result.push({ message: childCBFunction(data), pid: process.pid, event: "message" });
|
|
35
|
+
}
|
|
36
|
+
if (!!data.closeChild) {
|
|
37
|
+
process.send({ closeChild: true, pid: process.pid, childMessageData: childMessageData, result: result });
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// process.stdout.on("message", console.log);
|
|
43
|
+
// process.stdin.on("message", console.log);
|
|
44
|
+
// process.stderr.on("message", console.log);
|
|
45
|
+
|
|
46
|
+
process.on("message", options.callback);
|
|
47
|
+
|
|
48
|
+
process.on("exit", (code) => {
|
|
49
|
+
// console.log(`worker.process.js: _concurrencyProcesses: Child Process PID:${process.pid}: EXIT CODE ${code} CHILD: `, childMessageData);
|
|
50
|
+
if (!!process.env.handlers.childExit) {
|
|
51
|
+
const cbFunction = require(process.env.handlers.childExit);
|
|
52
|
+
result.push({ message: cbFunction(code), pid: process.pid, event: "exit" });
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
if (!!greet) {
|
|
57
|
+
process.send({ pid: process.pid, message: `Child Process PID:${process.pid}: Hello from Child Process` });
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
(!!process.env.childData) ? child.send({ pid: process.pid, message: process.env.childData }) : null;
|
|
61
|
+
|
|
62
|
+
} else {
|
|
63
|
+
|
|
64
|
+
return new Promise((resolve, reject) => {
|
|
65
|
+
const { fork, spawn } = require("child_process");
|
|
66
|
+
const controller = new AbortController();
|
|
67
|
+
const { signal } = controller;
|
|
68
|
+
|
|
69
|
+
const child = fork(filename, {
|
|
70
|
+
signal: signal, silent: false, env: {
|
|
71
|
+
...process.env, FORK: 1, childData: options.childData, handlers: { ...options.handlers }
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
child.on("error", (e) => {
|
|
76
|
+
if (!!options.handlers.error) {
|
|
77
|
+
const cbFunction = require(options.handlers.error);
|
|
78
|
+
result.push({ message: cbFunction(e), pid: process.pid, event: "error" });
|
|
79
|
+
}
|
|
80
|
+
reject(e);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
child.on("close", function (code, signal) {
|
|
84
|
+
let pid = child.pid, connected = child.connected;
|
|
85
|
+
if (!!options.handlers.close) {
|
|
86
|
+
const cbFunction = require(options.handlers.close);
|
|
87
|
+
result.push({ message: cbFunction(code, signal, pid, connected), pid: process.pid, event: "close" });
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
child.on("message", (data) => {
|
|
92
|
+
messageData.push(data);
|
|
93
|
+
if (!!options.handlers.message) {
|
|
94
|
+
const cbFunction = require(options.handlers.message);
|
|
95
|
+
result.push({ message: cbFunction(data), pid: process.pid, event: "message" });
|
|
96
|
+
}
|
|
97
|
+
if (!!data.closeChild) {
|
|
98
|
+
//
|
|
99
|
+
// try {
|
|
100
|
+
// // Stops the child process on message from child
|
|
101
|
+
// // Getting Error:
|
|
102
|
+
// // AbortError: The operation was aborted
|
|
103
|
+
// controller.abort();
|
|
104
|
+
// } catch (e) {
|
|
105
|
+
// child.kill(0);
|
|
106
|
+
// } finally {
|
|
107
|
+
// resolve({ messageData, result });
|
|
108
|
+
// }
|
|
109
|
+
//
|
|
110
|
+
child.kill(0);
|
|
111
|
+
resolve({ message: messageData, result: result });
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
if (!!greet) {
|
|
116
|
+
child.send({ pid: process.pid, message: `Master Process PID:${process.pid}: Hello from Master Process` });
|
|
117
|
+
}
|
|
118
|
+
options.data ? child.send({ pid: process.pid, message: options.data }) : null;
|
|
119
|
+
child.send({ closeChild: true });
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (process.env.FORK) {
|
|
125
|
+
_concurrencyProcesses();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
module.exports._concurrencyProcesses = _concurrencyProcesses;
|
|
@@ -0,0 +1,84 @@
|
|
|
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.threads.async.js
|
|
10
|
+
* File Description:
|
|
11
|
+
*
|
|
12
|
+
* https://github.com/danday74/shelljs.exec
|
|
13
|
+
*
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/* eslint no-console: 0 */
|
|
18
|
+
|
|
19
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
var cp = require('child_process');
|
|
22
|
+
|
|
23
|
+
function isObject(obj) {
|
|
24
|
+
return obj !== null && typeof obj === 'object' && !Array.isArray(obj);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function toBoolean(bool) {
|
|
28
|
+
if (bool === 'false') bool = false;
|
|
29
|
+
return !!bool;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function normaliseOptions(options) {
|
|
33
|
+
|
|
34
|
+
var DEFAULTS = Object.freeze({
|
|
35
|
+
encoding: 'utf8',
|
|
36
|
+
silent: false
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
if (!isObject(options)) {
|
|
40
|
+
options = {}
|
|
41
|
+
} else {
|
|
42
|
+
if (typeof options.silent !== 'undefined') {
|
|
43
|
+
options.silent = toBoolean(options.silent);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
options = Object.assign({}, DEFAULTS, options);
|
|
48
|
+
|
|
49
|
+
if (options.silent && typeof options.stdio === 'undefined') {
|
|
50
|
+
options.stdio = 'pipe';
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return options;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function _concurrencyThreadsAsync(command, options) {
|
|
57
|
+
|
|
58
|
+
options = normaliseOptions(options);
|
|
59
|
+
var error, stdout, stderr, code, ok;
|
|
60
|
+
|
|
61
|
+
try {
|
|
62
|
+
error = null
|
|
63
|
+
stdout = cp.execSync("node " + command, options)
|
|
64
|
+
stderr = ''
|
|
65
|
+
code = 0
|
|
66
|
+
ok = true
|
|
67
|
+
} catch (e) {
|
|
68
|
+
error = e
|
|
69
|
+
stdout = e.stdout
|
|
70
|
+
stderr = e.stderr
|
|
71
|
+
code = e.status || /* istanbul ignore next */ 1
|
|
72
|
+
ok = false
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
error: error,
|
|
77
|
+
stdout: stdout,
|
|
78
|
+
stderr: stderr,
|
|
79
|
+
code: code,
|
|
80
|
+
ok: ok
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
module.exports._concurrencyThreadsAsync = _concurrencyThreadsAsync;
|
package/src/worker.threads.js
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:
|
|
9
|
+
* File: worker.threads.js
|
|
10
10
|
* File Description:
|
|
11
11
|
*
|
|
12
12
|
*/
|
|
@@ -15,9 +15,168 @@
|
|
|
15
15
|
|
|
16
16
|
'use strict';
|
|
17
17
|
|
|
18
|
+
const path = require('path');
|
|
19
|
+
const fs = require('fs');
|
|
18
20
|
|
|
19
|
-
const { parentPort } = require("worker_threads");
|
|
20
21
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
|
|
23
|
+
function _concurrencyThreads(filename = __filename, options = {}, greet = false) {
|
|
24
|
+
const { Worker, isMainThread, parentPort } = require('worker_threads');
|
|
25
|
+
|
|
26
|
+
var messageData = [], childMessageData = [], result = [];
|
|
27
|
+
if (!options.handlers) { options["handlers"] = {}; }
|
|
28
|
+
|
|
29
|
+
return new Promise((resolve, reject) => {
|
|
30
|
+
if (isMainThread) {
|
|
31
|
+
// return new Promise((resolve, reject) => {
|
|
32
|
+
const worker = new Worker(filename);
|
|
33
|
+
if (!!greet) {
|
|
34
|
+
worker.postMessage({ pid: process.pid, message: `Hello, world! - Server: ${process.pid}` });
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
worker.on("connection", (e) => {
|
|
38
|
+
// console.log(`Main thread ${process.pid} due to connection event`, e.toString());
|
|
39
|
+
if (!!options.handlers.connection) {
|
|
40
|
+
const cbFunction = require(options.handlers.connection);
|
|
41
|
+
result.push({ message: cbFunction(e), pid: process.pid, event: "connection" });
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
worker.on("message", function (mainData) {
|
|
46
|
+
messageData.push({ ...mainData, threadId: worker.threadId });
|
|
47
|
+
if (!!options.handlers.message) {
|
|
48
|
+
const cbFunction = require(options.handlers.message);
|
|
49
|
+
result.push({ message: cbFunction(mainData), pid: process.pid, event: "message" });
|
|
50
|
+
}
|
|
51
|
+
if (!!mainData.closeChild) {
|
|
52
|
+
mainData = { ...mainData, threadId: worker.threadId };
|
|
53
|
+
childMessageData.push(mainData);
|
|
54
|
+
try {
|
|
55
|
+
worker.unref();
|
|
56
|
+
} catch (e) {
|
|
57
|
+
worker.terminate();
|
|
58
|
+
}
|
|
59
|
+
resolve({ message: messageData, result: result });
|
|
60
|
+
}
|
|
61
|
+
// console.log(`demo.threads.js:_concurrencyThreads: Main Thread PID ${process.pid}, filename: ${filename}, data: ${mainData}`);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
worker.on("online", () => {
|
|
65
|
+
if (!!options.handlers.online) {
|
|
66
|
+
const cbFunction = require(options.handlers.online);
|
|
67
|
+
result.push({ message: cbFunction(), pid: process.pid, event: "online" });
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
worker.on("error", (e) => {
|
|
72
|
+
if (!!options.handlers.error) {
|
|
73
|
+
const cbFunction = require(options.handlers.error);
|
|
74
|
+
result.push({ message: cbFunction(e), pid: process.pid, event: "error" });
|
|
75
|
+
}
|
|
76
|
+
reject(e)
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
worker.on("messageerror", (e) => {
|
|
80
|
+
if (!!options.handlers.messageerror) {
|
|
81
|
+
const cbFunction = require(options.handlers.messageerror);
|
|
82
|
+
result.push({ message: cbFunction(e), pid: process.pid, event: "messageerror" });
|
|
83
|
+
}
|
|
84
|
+
reject(e)
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
worker.on("close", (e) => {
|
|
88
|
+
console.log(`demo.threads.js:_concurrencyThreads: Main Thread PID ${process.pid} threadID:${worker.threadId} : Closing main thread `, e.toString(), messageData);
|
|
89
|
+
worker.unref();
|
|
90
|
+
if (!!options.handlers.close) {
|
|
91
|
+
const cbFunction = require(options.handlers.close);
|
|
92
|
+
result.push({ message: cbFunction(e), pid: process.pid, event: "close" });
|
|
93
|
+
}
|
|
94
|
+
// resolve({ messageData, result });
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
worker.on("exit", (code) => {
|
|
98
|
+
console.log(`demo.threads.js:_concurrencyThreads: Main Thread PID ${process.pid} threadID:${worker.threadId}, filename: ${filename}, ExitCode: ${code}, messageData`, messageData);
|
|
99
|
+
if (!!options.handlers.exit) {
|
|
100
|
+
const cbFunction = require(options.handlers.exit);
|
|
101
|
+
result.push({ message: cbFunction(code), pid: process.pid, event: "exit" });
|
|
102
|
+
}
|
|
103
|
+
if (code !== 0) {
|
|
104
|
+
reject(new Error(`Worker (PID ${process.pid}) threadID:${worker.threadId} stopped with exit code ${code}`));
|
|
105
|
+
}
|
|
106
|
+
// reject(new Error(`Worker (PID ${process.pid}) threadID:${worker.threadId} stopped with exit code ${code}`));
|
|
107
|
+
});
|
|
108
|
+
worker.postMessage({ closeChild: true });
|
|
109
|
+
// });
|
|
110
|
+
|
|
111
|
+
} else {
|
|
112
|
+
|
|
113
|
+
if (!options.callback) {
|
|
114
|
+
|
|
115
|
+
// options.callback = function (contents) {
|
|
116
|
+
// console.log("Testing worker");
|
|
117
|
+
// const { get } = (options.protocol === "https") ? require("https") : require("http");
|
|
118
|
+
// get(contents.url, (res) => {
|
|
119
|
+
// let result = "";
|
|
120
|
+
// res.on("data", (chunk) => result += chunk);
|
|
121
|
+
// res.on("end", () => {
|
|
122
|
+
// parentPort.postMessage(result);
|
|
123
|
+
// });
|
|
124
|
+
// }).on("error", (err) => parentPort.postMessage(err));
|
|
125
|
+
// }.bind(null, null, options);
|
|
126
|
+
|
|
127
|
+
options.callback = function (mainData) {
|
|
128
|
+
// return options.callback(data, parentPort);
|
|
129
|
+
// console.log(`demo.threads.js:_concurrencyThreads: Thread: Child Thread PID ${process.pid}, messageData: `, mainData);
|
|
130
|
+
childMessageData.push(mainData);
|
|
131
|
+
if (!!options.handlers.childMessage) {
|
|
132
|
+
const childCBFunction = require(options.handlers.childMessage);
|
|
133
|
+
result.push({ message: childCBFunction(mainData), pid: process.pid, event: "message" });
|
|
134
|
+
}
|
|
135
|
+
if (!!mainData.closeChild) {
|
|
136
|
+
parentPort.postMessage({ closeChild: true, pid: process.pid, childMessageData: childMessageData, result: result });
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
parentPort.on("connection", (e) => {
|
|
142
|
+
console.log(`demo.threads.js:_concurrencyThreads: Thread PID ${process.pid}: Child thread due to connection event `, e.toString())
|
|
143
|
+
if (!!options.handlers.childConnection) {
|
|
144
|
+
const childCBFunction = require(options.handlers.childConnection);
|
|
145
|
+
result.push({ message: childCBFunction(e), pid: process.pid, event: "connection" });
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
parentPort.on("message", options.callback);
|
|
150
|
+
|
|
151
|
+
parentPort.on("error", (e) => {
|
|
152
|
+
console.log(`demo.threads.js:_concurrencyThreads: Thread PID ${process.pid}:Closing child thread due to error`, e.toString());
|
|
153
|
+
if (!!options.handlers.childError) {
|
|
154
|
+
const cbFunction = require(options.handlers.childError);
|
|
155
|
+
result.push({ message: cbFunction(e), pid: process.pid, event: "error" });
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
parentPort.on("messageerror", (e) => {
|
|
160
|
+
console.log(`demo.threads.js:_concurrencyThreads: Thread PID ${process.pid}: Closing child thread due to messageerror`, e.toString());
|
|
161
|
+
if (!!options.handlers.messageerror) {
|
|
162
|
+
const cbFunction = require(options.handlers.messageerror);
|
|
163
|
+
result.push({ message: cbFunction(e), pid: process.pid, event: "messageerror" });
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
parentPort.on("close", (e) => {
|
|
168
|
+
console.log(`demo.threads.js:_concurrencyThreads: Thread PID ${process.pid}: Closing child thread due to close event`, e.toString());
|
|
169
|
+
if (!!options.handlers.close) {
|
|
170
|
+
const cbFunction = require(options.handlers.close);
|
|
171
|
+
result.push({ message: cbFunction(e), pid: process.pid, event: "close" });
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
if (!!greet) {
|
|
176
|
+
parentPort.postMessage({ pid: process.pid, message: `"Hello from child. - Thread: ${process.pid}` });
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
module.exports._concurrencyThreads = _concurrencyThreads;
|
package/test/_.test-template.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
// * Description:
|
|
6
6
|
// * Install: npm i --save
|
|
7
7
|
// * Github: https://github.com/ganeshkbhat/concurrency
|
|
8
|
-
// * npmjs Link: https://www.npmjs.com/package/
|
|
8
|
+
// * npmjs Link: https://www.npmjs.com/package/concurrency.js
|
|
9
9
|
// * File: index.js
|
|
10
10
|
// * File Description:
|
|
11
11
|
// *
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
|
|
19
19
|
// const expect = require('chai').expect;
|
|
20
20
|
|
|
21
|
-
// describe('test-.mjs::
|
|
21
|
+
// describe('test-.mjs::concurrency.js: Test Suite for concurrency.js Files', function() {
|
|
22
22
|
|
|
23
23
|
|
|
24
24
|
// before(async function(){
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
// });
|
|
27
27
|
|
|
28
28
|
|
|
29
|
-
// describe ('test-.js::
|
|
29
|
+
// describe ('test-.js::concurrency.js: [Test A] Test Suite for concurrency.js in main repo directory', function() {
|
|
30
30
|
// // it('[Test A] Test for ', function(done){
|
|
31
31
|
// // expect(200).to.equal(200);
|
|
32
32
|
// // done();
|