nest-schedule icon indicating copy to clipboard operation
nest-schedule copied to clipboard

Set time from config service to decorator

Open grigori-gru opened this issue 5 years ago • 7 comments

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');
    }

grigori-gru avatar May 31 '19 10:05 grigori-gru

Issue Label Bot is not confident enough to auto-label this issue. See dashboard for more details.

issue-label-bot[bot] avatar May 31 '19 10:05 issue-label-bot[bot]

The this context is different between decorator and function, not use this in decorator

miaowing avatar Jun 04 '19 02:06 miaowing

Thanks, but how should I use it in this case?

grigori-gru avatar Jun 05 '19 07:06 grigori-gru

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.

grigori-gru avatar Aug 07 '19 09:08 grigori-gru

I have the same question, I want to set enable: true/false based on the environment parameters.

hiteshjoshi1 avatar Oct 16 '19 04:10 hiteshjoshi1

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.

Nosfistis avatar Oct 30 '19 12:10 Nosfistis

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

trexguo avatar Nov 29 '19 08:11 trexguo