Ant*_*ine 2 javascript node.js node-cron nestjs
我正在构建一个 NestJs 应用程序,具有调度和配置功能。我希望能够使用环境变量配置我的 Cron,但它似乎不起作用。
应用程序模块.ts:
@Module({
imports: [
ConfigModule.forRoot(),
ScheduleModule.forRoot(),
SchedulingModule,
...
],
})
export class AppModule {}
Run Code Online (Sandbox Code Playgroud)
Schedule.service.ts(来自我的SchedulingModule):
@Cron(process.env.CRON_VALUE)
scheduledJob() {
this.logger.log('Scheduled : Job');
...
}
Run Code Online (Sandbox Code Playgroud)
.env:
...
CRON_VALUE=0 4 * * *
...
Run Code Online (Sandbox Code Playgroud)
显然,目前检查该值时它是空的。我收到以下错误:
(node:55016) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '_isAMomentObject' of undefined
at new CronTime (/Users/antoinegrenard/Documents/Projet/b4finance/service-scheduling/node_modules/cron/lib/cron.js:42:50)
at new CronJob (/Users/antoinegrenard/Documents/Projet/b4finance/service-scheduling/node_modules/cron/lib/cron.js:527:19)
at /Users/antoinegrenard/Documents/Projet/b4finance/service-scheduling/node_modules/@nestjs/schedule/dist/scheduler.orchestrator.js:56:29
...
Run Code Online (Sandbox Code Playgroud)
显然不可能在装饰器中获取环境值。
我必须这样做:
constructor(private schedulerRegistry: SchedulerRegistry) {}
onModuleInit() {
const job = new CronJob(process.env. CRON_VALUE, () => {
// What you want to do here
});
this.schedulerRegistry.addCronJob(name, job);
job.start();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5460 次 |
| 最近记录: |