question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cronjobs getting called multiple times

See original GitHub issue

I’m submitting a…


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

I am using NestJs and trying to use the schedule module for cronjobs. One of the methods is decorated with @Cron('*/30 * * * * * ') and I expected it to run only once after 30 seconds. The issue is after every 30 seconds, the cron runs three times. This is a consistent behavior and happens all the time. However I have observed that other methods which are also cronjobs are working as expected(run only once). Have been trying to figure out the root cause but there is no luck so far.

Expected behavior

The cronjobs should always only run only once.

Minimal reproduction of the problem with instructions

@Cron('*/10 * *  * * *')
  async triggerSessionRemindersCron(): Promise<void> {
    LoggerService.logger.info(`Date:: ${new Date().getTime()}`);
    LoggerService.logger.info('CronjobService::triggerSessionReminders BEGIN');
    // await this.triggerEventsForSessionReminders().catch((e) => {
    //   LoggerService.logger.error(e);
    // });
  }

What is the motivation / use case for changing the behavior?

The current implementation for me results in sending multiple events to webengage which in turn sends multiple notifications to the users.

Environment


Nest version: 7.5.4

 
For Tooling issues:
- Node version: 12.18.2 -->
- Platform:   Mac

Others:
Using npm version 6.14.5

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
maxpetersoncommented, Apr 13, 2021

FYI: I found that adding a name fixed my duplicates

  @Cron('*/10 * * * * *')
  async test() {
    this.logger.log('Test unnamed');
  }

  @Cron('*/10 * * * * *', { name: 'test-named' })
  async testNamed() {
    this.logger.log('Test named');
  }

=>

[Nest] 9525   - 13/04/2021, 17:30:39   [NestApplication] Nest application successfully started
[Nest] 9525   - 13/04/2021, 17:30:40   [ScanService] Test unnamed
[Nest] 9525   - 13/04/2021, 17:30:40   [ScanService] Test named
[Nest] 9525   - 13/04/2021, 17:30:40   [ScanService] Test unnamed
[Nest] 9525   - 13/04/2021, 17:30:50   [ScanService] Test named
[Nest] 9525   - 13/04/2021, 17:30:50   [ScanService] Test unnamed
[Nest] 9525   - 13/04/2021, 17:30:50   [ScanService] Test unnamed
[Nest] 9525   - 13/04/2021, 17:31:00   [ScanService] Test unnamed
[Nest] 9525   - 13/04/2021, 17:31:00   [ScanService] Test unnamed
[Nest] 9525   - 13/04/2021, 17:31:00   [ScanService] Test named
[Nest] 9525   - 13/04/2021, 17:31:10   [ScanService] Test unnamed
[Nest] 9525   - 13/04/2021, 17:31:10   [ScanService] Test named
[Nest] 9525   - 13/04/2021, 17:31:10   [ScanService] Test unnamed

The order of the log entries makes it a bit confusing, but if you look at the time stamps you can see it calls Test named once every 10 seconds but Test unnamed twice every 10 seconds

1reaction
ronatorycommented, Apr 5, 2022

Also it seems to be that if this is an underlying issue in https://github.com/kelektiv/node-cron then this won’t be fixed quite soon since the project isn’t really maintained anymore. See here https://github.com/kelektiv/node-cron/issues/578

Read more comments on GitHub >

github_iconTop Results From Across the Web

Script was triggered multiple times by cron - Blog
I am using crontab to trigger a PHP-scraping script on regular basis, but the script gets called multiple times. A recherché revealed that...
Read more >
How can I prevent Cronjobs from running multiple times?
It is common to execute cronjobs frequently, for example, every minute. The execution time, in tendency, increases with a growing data set.
Read more >
Cron job being called multiple times : r/node - Reddit
We are looping through the clinic and have been trying to send emails at 9am based on the clinic timezone. In our case...
Read more >
Cronjob executing multiple times at once - Server Fault
The python script is simply outputting the current time so we can say for sure that its cronjob which is opening the python...
Read more >
Why do some cron jobs execute the same task multiple times ...
The cron daemon reads your crontab and uses that to plan to run the jobs. If all the times are identical it will...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found