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
2.96k stars 80 forks source link

[fix] Job does not repeat itself #186

Closed omar-dulaimi closed 2 years ago

omar-dulaimi commented 2 years ago

Describe the bug

Node.js version: v14.19.3

OS version: Ubuntu 18.04

Description:

Hello

I'm facing a weird issue, where a job with 1m interval does not repeat itself.

Actual behavior

It currently exits the program once it finishes executing the job.

Expected behavior

It should not exit, knowing that the interval is 1m

Code to reproduce

import Bree from "bree";
import path from "path";
import { fileURLToPath } from "url";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const bree = new Bree({ doRootCheck: false });

(async () => {
  await bree.start();
  const jobName = `createUrlJobs`;
  await bree.add({
    name: jobName,
    path: path.resolve(__dirname, "..", "jobs", "createUrlJobs.js"),
    interval: "1m",
    timeout: 0, // start immediately
  });
  await bree.run(jobName);
  console.log(JSON.stringify(bree.config.jobs));
})();

Checklist

Note: I have tested the issue with both node 14 and 16, I couldn't with node 18 since it requires me to upgrade my OS.

titanism commented 2 years ago

What does your job createUrlJobs look like?

Also, do you have a typo here?

It currently exists the program once it finishes executing the job.

"Exists" -> "Exits"?

Have you tried running NODE_DEBUG=bree node app.js?

What version of Bree are you using?

omar-dulaimi commented 2 years ago

@titanism I meant exits, my bad.

The createUrlJobs job has another bree instance.

const bree = new Bree({
   doRootCheck: false,
});

(async () => {
  await bree.start();
  for (const clientEndpoint of clientEndpoints) {
    const url = `https://${clientEndpoint}`;
    const duration = randomDuration(1, 59);
    await bree.add([
      {
        name: `capture-url-${clientEndpoint}`,
        path: path.resolve(__dirname, "capture-url.js"),
        date: dayjs().add(duration, "seconds").toDate(),
        worker: {
          workerData: {
            clientEndpoint,
            url,
          },
        },
      },
    ]);

    await bree.run(`capture-url-${clientEndpoint}`);
  }
})();

Using ^9.0.1

titanism commented 2 years ago

bree.run will not consider interval, it only runs a job once

If you want jobs to start on an interval or schedule, use bree.start

titanism commented 2 years ago

Ref https://github.com/breejs/bree/blob/master/src/index.js (look at the start and run functions)

omar-dulaimi commented 2 years ago

@titanism Thanks man. I missed this part in the README:

// run all jobs (this does not abide by timeout/interval/cron and spawns workers immediately)