breejs / bree

Bree is a Node.js and JavaScript job task scheduler with worker threads, cron, Date, and human syntax. Built for @ladjs, @forwardemail, @spamscanner, @cabinjs.
https://jobscheduler.net
MIT License
3.01k stars 78 forks source link

Job exiting before finished, simple example, no console.log #123

Closed BertCatsburg closed 3 years ago

BertCatsburg commented 3 years ago

In continuation of #109 and #121

I have made a simple reproduction of the problem, this time without console.logs but a simple write to a file. Instead of the console.log (like I did in #121), I do a fs.createWriteStream and file.write to a file.

Very strange thing: The first run is working (messages appear in the output file), but from the second run onwards, no messages appear. It's not a case of overwriting, because I added a timestamp.

Relevant files

scheduler.ts

import Bree from 'bree';
import {typescript_worker} from "./lib/typescript_worker";

(async () => {
    try {

        const bree = new Bree({
            jobs: [
                {
                    name: 'HelloWorld2',
                    path: typescript_worker,
                    interval: 'every 10 seconds',
                    worker: {
                        workerData: {
                            filename: './jobs/hello_world_to_file.ts',
                            logfile: './logs/hello_world_2.log'
                        }
                    }
                },
            ],
            workerMessageHandler: (message) => {
                console.log(message);
            }
        });

        bree.start()
    } catch (error) {
        console.error(error);
    }
})();

hello_world_to_file.ts

(() => {

    const {parentPort, workerData} = require('worker_threads');
    const fs = require('fs');

    const logfile = parentPort ? workerData.logfile : './hello_world_2_not_a_worker.log';
    const logStream = fs.createWriteStream(logfile, {flags: 'a'});

    try {
        console.log('About to write to logfile');
        logStream.write('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA ' + new Date().toISOString() + '\n');
        logStream.write('BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB ' + new Date().toISOString() + '\n');
        logStream.write('CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC ' + new Date().toISOString() + '\n');
    } catch (error) {
        console.error('Something wrong in the HelloWorld Job');
        console.error(error);
    } finally {
        logStream.close();
        if (parentPort) {
            parentPort.postMessage('done');
        }
    }
})();

Result

The output-file shows only the first time the job is run.

But after that nothing is written to the output-file.

No console.log is used in this program.

BertCatsburg commented 3 years ago

And also the above problem is reproduced in the same Repo as in bug #121: https://github.com/BertCatsburg/BreeJS-Jobs-exit-before-finished

shadowgate15 commented 3 years ago

Alright so I have determined the issue with this. All of your log writes are not actually being awaited therefore the method is moving on to the finally and returning done. This will kill the worker in bree thus it doesn't complete.