#Disable Cron
5 messages · Page 1 of 1 (latest)
are you talking about the @nestjs/schedule package?
if yes, either you can write a decorator that wraps the Cron decorator exposed by the @nestjs/schedule package
and use the applyDecorators from the @nestjs/common to conditionally apply it based on the environment variable.
or you can write the environment check logic inside the handler method.
I recommend the first approach; here's a sample:
// decorators/conditional-cron.decorator.ts
import { applyDecorators } from '@nestjs/common';
import { Cron, CronExpression } from '@nestjs/schedule';
export ConditionalCron = (envKey: string, expression: CronExpression) => {
if (process.env[envKey] == 'true') {
return applyDecorators(Cron(expression));
}
return applyDecorators(
// apply something else...
);
}
// X.service.ts
import { Injectable } from '@nestjs/common';
import { ConditionalCron } from './decorators/conditional-cron.decorator';
@Injectable()
export class XService {
@ConditionalCron('ENABLE_X_CRON', CronExpression.EVERY_5_SECONDS)
async handleCron() {
// ...
}
}
# .env
ENABLE_X_CRON=true
But is there a way to fully disable it on local , from the CronExpression enum i dont see that enum value
if you believe there is any, and you know the string value of it
why don't you open a new Pull Request for that?