nestjs / schedule

Schedule module for Nest framework (node.js) ⏰

Home Page:https://nestjs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cronjobs getting called multiple times

aedelkhanday opened this issue · comments

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

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

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

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

@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.

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

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

@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

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.

We have the same issue with "@nestjs/schedule": "^4.0.1" and it's deps:

    "node_modules/@nestjs/schedule": {
      "version": "4.0.1",
      "resolved": "https://registry.npmjs.org/@nestjs/schedule/-/schedule-4.0.1.tgz",
      "integrity": "sha512-cz2FNjsuoma+aGsG0cMmG6Dqg/BezbBWet1UTHtAuu6d2mXNTVcmoEQM2DIVG5Lfwb2hfSE2yZt8Moww+7y+mA==",
      "dependencies": {
        "cron": "3.1.6",
        "uuid": "9.0.1"
      },
      "peerDependencies": {
        "@nestjs/common": "^8.0.0 || ^9.0.0 || ^10.0.0",
        "@nestjs/core": "^8.0.0 || ^9.0.0 || ^10.0.0"
      }
    },

There's been a single new version after that that bumps the minor versions of both.

Adding an unique name to the Cronjobs DID NOT fix our issue. Running on a Google Cloud Cloud Run instance.

Hey @petetnt, I see you've mentioned this issue on kelektiv/node-cron#543, but I cannot see your comment there.

Were you by any chance running multiple instances like suggested in the comments on our issue, and so decided to delete yours?

Sorry @sheerlox, I debugged it for quite long and the same issue affected @interval in NestJS which is not implemented via node-cron so I deleted the comment. For future reference I was only running a single instance and it only happened when ran in GCP, but not locally even in same Docker container. Eventually I managed to work around the issue by only having one @Cron-decorator per file, which removed the double calls for whatever reason. And the build output didn’t have double decorator calls either 🤷

Hmm, weird one ... thanks for the context though!

To anyone stumbling on this issue in the future: we're now pretty sure this isn't related to the underlying cron library, so if you're able to share a minimal reproduction example maybe we'll be able to find a solution if there's indeed a bug in @nestjs/schedule.

Otherwise, please refer to the comments here and at kelektiv/node-cron#543 for potential fixes.