如何使用 NestJS 在上午 12:00 为多个国家/地区编写调度程序(在所有时区都相同)?

Bal*_*man 3 node.js express nestjs nestjs-config

我有一个调度程序,必须在印度时区的上午 12:00 和新加坡时区的上午 12:00 运行。我能够为印度用户编写一个 cron 作业,但如何在新加坡凌晨 12:00 触发同样的任务?

小智 10

如果使用这种方法,只需要编写一次代码,并从两个用@Cron()修饰的函数中调用它

服务.ts

@Injectable()
export class Service {
  @Cron('* * 0 * * *', {
    timeZone: 'Asia/Tehran', // use this website: https://momentjs.com/timezone/
  })
  async iran() {
    this.yourFunction();
  }

  @Cron('* * 0 * * *', {
    timeZone: 'Asia/Tokyo',
  })
  async japan() {
    this.yourFunction();
  }

  async yourFunction() {
    // write the schedule only one time
  }
}
Run Code Online (Sandbox Code Playgroud)