I'm using Express, from within app.js I'm calling a worker thread which is in another file named service.js. everything runs smoothly when I run it normally via node app.js, but as soon as I switch to PM2, pm2 start app.js nothing happens. it won't even try to call the service.js file. I'm running node v17.4.0 on an Ubuntu 18.04. any idea on what might be going on?
app.js
// app.js
const { Worker } = require('worker_threads')
function runService(workerData) {
return new Promise((resolve, reject) => {
const worker = new Worker('./service', { workerData });
worker.on('message', resolve);
worker.on('error', reject);
worker.on('exit', (code) => {
if (code !== 0)
reject(new Error(`Worker stopped with exit code ${code}`));
})
})
}
async function run() {
const result = await runService('world')
console.log(result);
}
run().catch(err => console.error(err))
service.js
//service.js
const { workerData, parentPort } = require('worker_threads')
// You can do any heavy stuff here, in a synchronous way
// without blocking the "main thread"
parentPort.postMessage({ hello: workerData })