Scheduler in Kubernetes runs twice in node application

Viewed 292

I have an node application running in Kubernetes scheduled a job in node scheduler to run every day by midnight, but scheduler is running twice a day.

Kubernetes
Version - 1.21.5
Node - 13.0
Server
I have two nodes with an load balancer.

Update1

var scheduler = require('node-schedule');
function getRule() {
    var rule = new scheduler.RecurrenceRule();
    // rule.minute = new scheduler.Range(0, 59, 59);
    rule.hour = 23;
    rule.minute = 45;
    return rule;
}

exports.createSchedule = function () {
    var rule = getRule();
    scheduler.scheduleJob(rule, send1DayActivationReminderEmail);
};

This same set of code works in Heroku application, which runs only once.

1 Answers

I have two nodes with an load balancer

Every instance of the application will run this job, there is no way for them to know about each other. So if you run two Pods you will get two executions. One solution is to scale the application to just one instance as I guess was the case with the Heroku deployment. Another solution is to instead trigger the job using a kubernetes cronjob https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/.

Related