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

问题描述

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

标签: node.jsexpressnestjsnestjs-config

解决方案


如果使用这种方法,只需要编写一次代码,并从@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
  }
}

推荐阅读