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.02k stars 78 forks source link

Using cron #32

Closed pinguxx closed 4 years ago

pinguxx commented 4 years ago

Can somebody give me a simple example, im trying to use bree with a cron, but it just exit and nothing happens

const Bree = require('bree');

const bree = new Bree({
    jobs: [{
        name: 'test1',
        cron : '* * * * *'
    }]

});

bree.start();

job/test1

const { parentPort } = require("worker_threads");

console.log("working")
if (parentPort) parentPort.postMessage("done");
else process.exit(0);
pinguxx commented 4 years ago

After looking at the code i found that this works

const Bree = require('bree');
const later = require('@breejs/later');
var s = later.parse.cron('* * * * *');

const bree = new Bree({
    cronValidate: {},
    jobs: [{
        name: 'test1',
        cron : s
    }]

});

bree.start();

Two problems

  1. Looks like on parsing the cron is generating some functions that cant be send to the worker but it never generates a correct scheduel
  2. on start it never checks cron options since is assuming it will have an interval or similar

I assume that changing this piece of code may fix the problem

if (result.isValid()) {
          const schedule = later.schedule(
            later.parse.cron(
              job.cron,
              boolean(
                typeof job.hasSeconds === 'undefined'
                  ? this.config.hasSeconds
                  : job.hasSeconds
              )
            )
          );

So the schedule is correct and not functions, but i dont know later so not sure change it to what :P

niftylettuce commented 4 years ago

Taking a look, sec

niftylettuce commented 4 years ago

Ok, release inbound, one moment.

niftylettuce commented 4 years ago

v3.2.0 released

https://github.com/breejs/bree/releases/tag/v3.2.0

pinguxx commented 4 years ago

seems to be working now :D Thanks for the quick fix