nest-schedule
nest-schedule copied to clipboard
Set time from config service to decorator
Is it possible to set time to decorator from configService which I use as dependency in my job module, for example this way:
export class JobService extends NestSchedule {
private readonly logger = new Logger(JobService.name);
constructor(
private readonly configSerice: ConfigService,
) {
super();
}
@Cron('10 0 * * *')
async saveVoteResult2Db() {
this.logger.log('Starting daily cronjob!!!');
}
@Interval(this.configSerice.syncInterval)
async saveToRedis() {
this.logger.debug('Data is synced with redis');
}
Issue Label Bot is not confident enough to auto-label this issue. See dashboard for more details.
The this context is different between decorator and function, not use this in decorator
Thanks, but how should I use it in this case?
That's all about @Cron
params of course. For example if I want to test how my schedule job works using less period, I can't do it.
I have the same question, I want to set enable: true/false based on the environment parameters.
Environment parameters can be used directly, since they are available already. However it's not possible to use a service in decorators. I ended up creating tasks manually. However, there are no helper functions for distributed locking there.
I have to create static methods and properties in ConfigService, then we can access them.
@Timeout(1000, {
key: ConfigService.getString('ABC_KEY'),
enable: ConfigService.getBoolean('ABC_ENABLE'),
})
Not sure if this is the best practice