Skip to main content

Cronjobs

This tool allows you to run recurrent jobs in your X Framework ecosystem. The cronjobs which are running will have their result stored in "crons" collection.

Keep in mind that if you have multiple instances deployed, the cronjob will run only once by whatever instance manages to start it first.

Setting up

npm i -S @bluelibs/x-cron-bundle
import { XCronBundle } from "@bluelibs/x-cron-bundle";
const kernel = new Kernel({  bundles: [    // the others    new XCronBundle(),  ],});

Adding a Cronjob

import { CronService } from "@bluelibs/x-cron-bundle";
class AppBundle extends Bundle {  async init() {    const cronService = this.container.get(CronService);    cronService.add({      // Names should be unique across all cronjobs      name: "Send reminders",      schedule(parser) {        // Parser is an instance of Later.parse(), can be configured in many forms        // http://bunkat.github.io/later/parsers.html#cron        return parser.text("every 2 hours");      },      async job(container) {        const myService = container.get(MyService);        await myService.process();
        // Ensure the job is blocking (use await) so we can properly keep track the execution      },    });  }}

Consider storing your cronjobs in a separate file and just add them here:

cronjobs.ts
import { ICronConfig } from "@bluelibs/x-cron-bundle";
export const cronfigs: ICronConfig[] = [  {    name: "Do this, do that",    // ... the rest  },];

If you want to schedule to run at 8:00 am in your local time zone, please note the time in parser is in UTC and adapt accordingly.