cronnestjscron-tasknode-schedule

Dynamic Crons are executed many times at the same time


I'm creating new CronJobs and scheduling them to run in the future, but when the execution time arrives, the same Job is fired three times. After the execution of the job I am removing it from the registry and even so it does not avoid the tripling of the job.

localhost it's triggered onnly once
published it's triggered thrice

we have three pods behind kubernetes. i guess is something related with that.

 const date = dateFns.addMinutes(new Date(), 10);
 const job = new CronJob({
      cronTime: date,
      start: true,
      onTick: async () => {
      await this.sendEmail(params);
    }
 });
this.schedulerRegistry.addCronJob('job01', job);

Solution

  • thanks for the help! I was able to resolve this using cron from the yaml file:

       cronjob:
            use: true
            schedule: '*/5 * * * *'
            env:
                CRON: 1
    

    and call that on bootstrap app like that:

    appBootstrapInstance
        .bootstrap()
        .then((app) => {        
            return app;
        })
        .then(async (app) => {      
            const env = app.get(EnvService);
            if (env.getAsBoolean('CRON')) {          
                await new Promise((resolve) => setTimeout(resolve, CRON_TIMEOUT));
    
                const task = app.get(MyTaskService);
                await task.doSomething();
                await new Promise((resolve) => setTimeout(resolve, CRON_TIMEOUT));
    });