Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cron running twice for the same job. #37

Open
pabloherre opened this issue Mar 21, 2024 · 1 comment
Open

Cron running twice for the same job. #37

pabloherre opened this issue Mar 21, 2024 · 1 comment

Comments

@pabloherre
Copy link

I have a single job entry in the database, when the server time reach the sleepUnitl time, the job is being executed twice inside the onDocument method.

Here are my settings:

MongoDB Schema

const schema = new Schema(
    {
      name: { type: String, required: true },
      sleepUntil: { type: Date, default: new Date(), required: true },
      interval: { type: String, required: true },
      data: { type: Schema.Types.Mixed }
    },
    {
      timestamps: true
    }
  );

MondoDB Entry

{
    "_id": ObjectId("65d37bbf7933cf20f1064ee3"),
    "name": "JOB_TO_RUN",
    "interval": "0 0 6 * * *",
    "sleepUntil": ISODate("2024-03-19T09:00:00.000Z"),
    "data": {},
    "updatedAt": ISODate("2024-03-01T15:15:14.647Z")
}

Cron job execution code

const { MongoCron } = require('mongodb-cron');

module.exports = async app => {
  const mongooseClient = app.get('mongooseClient');
  const collection = await mongooseClient.connections[0].collections['cron-jobs'];

  const cron = new MongoCron({
    collection,
    onStart: () => console.info('\t✓ Cron jobs.'),
    onDocument: doc => console.count(), // You will get a 2 console logs here!
    onError: async err => console.error(err),
    nextDelay: 1000,
    reprocessDelay: 1000,
    idleDelay: 1000
  });

  cron.start();
};
@origooo
Copy link

origooo commented Jul 29, 2024

I'm potentially running into similar behaviour. Does it seem to stop doing it when you increase e.g. nextDelay? For me it seem to ignore the interval if I set a nextDelay. My interval while testing is set to run at 0, 15, 30, and 45 seconds, but with nextDelay (or idleDelay) set to e.g. 10 seconds, it runs every tenth second. The implication of this is that I can't have a reasonable nextDelay set and my CPU usage is getting high...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants