1

I am trying to force my cron task to stop and actually want the whole nodeapp to stop after 5 executions, but nothing happens, the message "time to quit" keeps appearing in log every minute.

What is the best way to stop the cron job and the quit the whole docker?

const crone = require('node-cron');
let mucount = 0;
export const update = crone.schedule('1 * * * * *', () => {
    
    if (mucount < 5) {
        new UpdateController();
        mucount++;
    } else
        
        {log.info("time to quit")
            update.cancel();
            update.stop();
             server.close();
             process.kill(process.pid);
             process.exit(1);
             throw new Error('Please exit');
            
        }
},{ scheduled: false });
Tom
  • 6,725
  • 24
  • 95
  • 159

2 Answers2

0

Maybe try calling update.stop() from outside the execution block?

Not familiar with this api.

Yisheng Jiang
  • 110
  • 1
  • 5
0

It looks like node-cron is spawning child processes, and trying to kill those child processes is not exiting the parent process, which makes sense for most use cases. You can try defining a kill method outside the cron function and then invoking it inside the cron function, though generally the whole reason you would use a scheduled cron function like this is to run something on a schedule without exiting after a few occurrences.

My advice would be to use a different method of running new UpdateController() five times, with a minute in between, which automatically exits after it finishes. Depending on your requirements this might be achieved using some of the answers to this question on waiting in NodeJS for a fixed amount of time.

Dakeyras
  • 1,829
  • 5
  • 26
  • 33