We have a single instance running in Google Cloud Run and the CronJobs are run in production (but not locally) three times.
There's multiple issues about this, but I haven't been able to find a solution and I am FAIRLY sure that it's not a PEBCAK-issue.
Eg.
https://github.com/nestjs/schedule/issues/454
https://github.com/nestjs/schedule/issues/445
"Why are cron jobs run at 3-4 times at..." here on #nestjs-help
"My cron runs 3 times in a row in a single" here on #nestjs-help
"Dynamic Crons are executed many times" here on #nestjs-help
"Cron Job Triggers multiple times" here on #nestjs-help
https://github.com/kelektiv/node-cron/issues/543
https://github.com/kelektiv/node-cron/issues/605
etc.
My app.module.ts looks like this:
@Module({
imports: [
ScheduleModule.forRoot(),
TypeOrmModule.forRootAsync({
inject: [ConfigService],
useClass: TypeOrmConfigService,
}),
PollerModule,
],
})
export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply(RequestPathHandlerMiddleware)
.forRoutes({ path: '*', method: RequestMethod.ALL });
}
}
The PollerModule has the PollerService that has the CronJobs:
@Module({
imports: [FooModule, BarModule, BizModule],
providers: [PollerService],
controllers: [PollerWebhooksController],
})
export class PollerModule {}
PollerService:
@Injectable()
export class PollerService {
@Cron(CronExpression.EVERY_5_MINUTES, {
name: 'fooCronJob',
disabled: process.env.NODE_ENV !== 'production',
timeZone: 'Europe/Helsinki',
})
async fooJob() {
console.log('I am run 3 times!')
}
}
One of the issues recommended setting name, which fixed it for some, it did not for us. One suggestion was that the service should be only added to providers once, which it is. Additionally, the initialization scripts, onModuleInit() commands are ran only once, so there's only one instance running.
Any other ideas?