-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
68 lines (59 loc) · 2.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const { Worker } = require('worker_threads');
const { join } = require('path');
//const WorkerThread = require("./worker.js");
/* Export a function that queues pending work. */
const queue = [];
const MyWorkerTask = (operator, parameters) => {
queue.push({operator, parameters});
drainQueue();
};
/* Instruct workers to drain the queue. */
let workers = [];
function drainQueue() {
for (const worker of workers) {
worker.takeWork();
}
}
function spawn() {
// this requires the file to exist after pkg has built it
//const worker = new Worker("./worker.js"); // Error: Cannot find module 'worker.js'
// so instead I tried this
//const thread = WorkerThread.toString();
//const functionBegin = thread.indexOf("{") + 1;
//const worker = new Worker(thread.substring(functionBegin, thread.length-1), {
// eval: true
//});
// but running it this way you will get the error in worker.js SyntaxError: Unexpected identifier because it can not find the node module
const worker = new Worker(join(__dirname, "worker.js"));
let job = null; // Current item from the queue
let error = null; // Error that caused the worker to crash
function takeWork() {
if (!job && queue.length) {
// If there's a job in the queue, send it to the worker
job = queue.shift();
worker.postMessage(job);
}
}
worker.on('online', () => {
workers.push({ takeWork });
takeWork();
}).on('message', (result) => {
console.log("WORKER task completed task:", job.operator, result);
job = null;
takeWork(); // Check if there's more work to do
}).on('error', (err) => {
console.error(err);
error = err;
}).on('exit', (code) => {
workers = workers.filter(w => w.takeWork !== takeWork);
if (code !== 0) {
console.error(`worker exited with code ${code}`);
spawn(); // Worker died, so spawn a new one
}
});
}
spawn();
setInterval(() => {
console.log(`timeout add MyWorkerTask`);
MyWorkerTask("TEST", { id: 1 });
}, 1000);