Skip to content

Node schedule duplicates when reaching 1st of August #477

Closed as not planned

Description

@wenkangwow
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");
    }
Image 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

Activity

  1. merencia commented on Aug 5, 2025

    @merencia
    Member

    @wenkangwow is it duplicated on 03/08 and 04/08?

    It looks like a extra process running.

    Don't know how to simulate that.

  2. added
    cannot reproduce
    on Aug 8, 2025
  3. added
    reliability
    on Jun 16, 2026
  4. merencia commented on Jun 16, 2026

    @merencia
    Member

    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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    cannot reproducereliability

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions