Closed BertCatsburg closed 3 years ago
Keep in mind that console
hijacks stdout which means that anytime the main thread is busy it won't output from the worker. This is a known issue/functionality in nodejs. We suggest passing any console messages back to parent port and use the provided messageHandler
functions.
Yes, that's it. Sorry I did not see that.
I've changed the code as follows and now it works:
const bree = new Bree({
jobs: [
{
name: 'HelloWorld',
path: typescript_worker,
interval: 'every 10 seconds',
worker: {workerData: { filename: './jobs/hello_world.ts'}}
}
],
workerMessageHandler: (message) => {
console.log(message);
}
});
const {parentPort} = require('worker_threads');
(async () => {
try {
l('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA');
l('BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB');
l('CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC');
} catch (error) {
console.error('Something wrong in the HelloWorld Job');
console.error(error);
} finally {
if (parentPort) {
parentPort.postMessage('done');
} else {
process.exit(0);
}
}
})();
function l(message: string) {
if (parentPort) {
parentPort.postMessage(message);
}
}
And now it works.
This is almost the same issue as #109, however simpler code.
I have a Bree scheduler written in Typescript. Each job does a lot of async things, therefore the Scheduler and the Jobs (workers) are enclosed in an IFFE
One example Job has just 3 console.log statement. And the job runs every 10 seconds.
First run all 3 console.logs are shown, but starting from the second run only the first is shown. (See output below)
There is a complete reproducable environment on https://github.com/BertCatsburg/BreeJS-Jobs-exit-before-finished
Relevant files
scheduler.ts
typescript_worker.ts
Taken from the documentation
hello_world.ts
Output
When doing a npm start the following output appears:
It is strange that the first run the output first shows the ending message and then the last 2 log messages.
And the problem is that starting from the second run the last 2 log messages do not appear.