You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Node schedule duplicates when reaching 1st of August #477
cannot reproduceCould not reproduce the bug in a clean environment. May depend on local setup.reliabilityMissed, late or duplicated executions (long timers, sleep, clock drift)
export const scheduleBotAccountJobs = cron.schedule(
// "*/1 * * * *",
//run at every day 06:00 am time
"0 6 * * *",
async () => {
// Only run this scheduled job if this is the main worker (worker.ts)
if (process.env.WORKER_TYPE === "worker") {
console.log("Scheduled job: scheduleBotAccountJobs - Running on worker");
} else {
console.log(
"Scheduled job: scheduleBotAccountJobs - Skipping execution - not worker"
);
return;
}
Above is my code, now I have this code running parallelly in 2 different servers (pm2), now they both have different .env worker type (this is verified 10000%). Somehow, starting 1st of August, the function runs into the line below even though it is of different worker_type .env variables:
if (process.env.WORKER_TYPE === "worker") {
console.log("Scheduled job: scheduleBotAccountJobs - Running on worker");
}
Above image shows the duplicate record in my database, the red circle shows the duplicated records starting from 01/08, the blue circles shows the records before 01/08, I did not perform any changes on my pm2 server within this period of time
Hi @wenkangwow, sorry for the very late reply on this one. 🙏
I looked into it and I'm fairly confident this isn't a node-cron issue. node-cron runs entirely within a single process, it has no state shared across processes and never reads process.env, so it can't make your WORKER_TYPE === "worker" guard pass on a process where it shouldn't. I also tried to reproduce a duplicate by running a 0 6 * * * job with the clock crossing Jul 31 → Aug 1, and it fired exactly once per day, there's no behaviour that changes on a specific date.
Since it worked until Aug 1 and then started duplicating, my best guess is that something in your deployment changed around then and both processes ended up as worker, a very common case being PM2 exec_mode: cluster (which runs the same app, with the same env, twice). Logging { pid: process.pid, worker: process.env.WORKER_TYPE } at startup on each instance should make it obvious.
I'll close this for now, but if you ever catch the same schedule firing twice within a single process, please drop a minimal repro here and I'll reopen right away. Thanks for the report and the chart, it genuinely helped narrow it down!
cannot reproduceCould not reproduce the bug in a clean environment. May depend on local setup.reliabilityMissed, late or duplicated executions (long timers, sleep, clock drift)
Activity
merencia commentedon Aug 5, 2025
@wenkangwow is it duplicated on 03/08 and 04/08?
It looks like a extra process running.
Don't know how to simulate that.
merencia commentedon Jun 16, 2026
Hi @wenkangwow, sorry for the very late reply on this one. 🙏
I looked into it and I'm fairly confident this isn't a node-cron issue. node-cron runs entirely within a single process, it has no state shared across processes and never reads
process.env, so it can't make yourWORKER_TYPE === "worker"guard pass on a process where it shouldn't. I also tried to reproduce a duplicate by running a0 6 * * *job with the clock crossing Jul 31 → Aug 1, and it fired exactly once per day, there's no behaviour that changes on a specific date.Since it worked until Aug 1 and then started duplicating, my best guess is that something in your deployment changed around then and both processes ended up as
worker, a very common case being PM2exec_mode: cluster(which runs the same app, with the same env, twice). Logging{ pid: process.pid, worker: process.env.WORKER_TYPE }at startup on each instance should make it obvious.I'll close this for now, but if you ever catch the same schedule firing twice within a single process, please drop a minimal repro here and I'll reopen right away. Thanks for the report and the chart, it genuinely helped narrow it down!