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

Cronjobs getting called multiple times #445

Closed
aedelkhanday opened this issue Jan 25, 2021 · 9 comments
Closed

Cronjobs getting called multiple times #445

aedelkhanday opened this issue Jan 25, 2021 · 9 comments

Comments

@aedelkhanday
Copy link

aedelkhanday commented Jan 25, 2021

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
@kamilmysliwiec
Copy link
Member

kamilmysliwiec commented Jan 25, 2021

Let's track this here kelektiv/node-cron#489 (comment)

@maxpeterson
Copy link

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

@ronatory
Copy link

ronatory commented Apr 5, 2022

For me adding a name unfortunately doesn't fix the issue. I still get duplicates

@ronatory
Copy link

ronatory commented 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 kelektiv/node-cron#578

@danielnadar75
Copy link

@maxpeterson Thanks a lot man, it seemed silly at first but I did add a name and it solved the issue and it's running just once now. Thanks a lot I've been stuck with this from last couple of hours.

@joshkrab
Copy link

@maxpeterson Yes, thanks a lot. It worked for me too :)
Just added a name: @Cron('10 * * * * *', { name: 'redis' })

@xegulon
Copy link

xegulon commented Mar 2, 2023

@kamilmysliwiec then the bug is still present in @nestjs/schedule, isn't it?

@saurabh-maske-03
Copy link

@kamilmysliwiec then the bug is still present in @nestjs/schedule, isn't it?

Yes still i am able to reproduce it over server and giving name to cron job didn't solve issue for me

@sheerlox
Copy link
Contributor

sheerlox commented Apr 7, 2024

Yes still i am able to reproduce it over server and giving name to cron job didn't solve issue for me

Hi! I'm one of cron's maintainers. Could you please give us more info on the @nestjs/schedule and cron versions you're using? A lot of work has been done in the past year, so if you're still running old versions, upgrading might just solve your issue.

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

9 participants