I am building a nestJs application, with scheduling and configuration. I want to be able to configure my Cron with my environment variable but it does not seems to work.
app.module.ts :
@Module({
imports: [
ConfigModule.forRoot(),
ScheduleModule.forRoot(),
SchedulingModule,
...
],
})
export class AppModule {}
scheduling.service.ts (from my SchedulingModule) :
@Cron(process.env.CRON_VALUE)
scheduledJob() {
this.logger.log('Scheduled : Job');
...
}
.env :
...
CRON_VALUE=0 4 * * *
...
Apparently at the moment the value is checked it's empty. I got the following error :
(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
...
I am building a nestJs application, with scheduling and configuration. I want to be able to configure my Cron with my environment variable but it does not seems to work.
app.module.ts :
@Module({
imports: [
ConfigModule.forRoot(),
ScheduleModule.forRoot(),
SchedulingModule,
...
],
})
export class AppModule {}
scheduling.service.ts (from my SchedulingModule) :
@Cron(process.env.CRON_VALUE)
scheduledJob() {
this.logger.log('Scheduled : Job');
...
}
.env :
...
CRON_VALUE=0 4 * * *
...
Apparently at the moment the value is checked it's empty. I got the following error :
(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
...
Share
Improve this question
edited Oct 6, 2021 at 12:46
Antoine Grenard
asked Oct 6, 2021 at 10:07
Antoine GrenardAntoine Grenard
1,8923 gold badges24 silver badges44 bronze badges
1
- Does this answer your question? How to pass configs from config service to Nest.js decorator? – HMilbradt Commented Oct 6, 2021 at 15:01
3 Answers
Reset to default 12Apparently it is not possible to get env values in decorators.
I had to do it this way :
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();
}
To fix this problem you should load the config on your service again:
require('dotenv').config();
A way I made it work was like this
import * as dotenv from 'dotenv';
const getCronInterval = () => {
dotenv.config();
return process.env.FETCH_AND_SEND_DATA_CRON_INTERVAL;
};
@Injectable()
export class AppService {
constructor(
private readonly service: MyService,
) {}
@Cron(getCronInterval()) // Adjust the cron schedule as needed
async doSomething() {
....