miaowing / nest-schedule

A cron-like and not-cron-like job distributed scheduler for Nest.js by decorators.
MIT License
435 stars 35 forks source link

Set time from config service to decorator #24

Open grigori-gru opened 5 years ago

grigori-gru commented 5 years ago

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[bot] commented 5 years ago

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

miaowing commented 5 years ago

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

grigori-gru commented 5 years ago

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

grigori-gru commented 5 years ago

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.

hiteshjoshi1 commented 5 years ago

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

Nosfistis commented 5 years ago

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.

trexguo commented 4 years ago

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