How to schedule cron job that run every Monday on next hours:
1 week - 6:00
2 week - 7:00
3 week - 8:00
...
n - 1 week 18:00
n week 19:00
n + 1 week 6:00
n + 2 week 7:00
I was try this one, but it doesn’t work correctly
@Injectable()
export class ProductsCron implements OnModuleInit {
constructor(
private schedulerRegistry: SchedulerRegistry,
) {}
private readonly dynamicProductsSuggestionCronJobName = 'DynamicProductsSuggestionCronJob';
private hour = 6;
private setCronSchedule(hour: number) {
try {
this.schedulerRegistry.deleteCronJob(this.dynamicProductsSuggestionCronJobName);
} catch (e) {
console.log(`${this.dynamicProductsSuggestionCronJobName} not found.`);
}
const job = new CronJob(`* */${hour} * * * 1`, () => {
this.suggestShifts(String(hour));
this.hour = this.hour < 19 ? this.hour + 1 : 6;
this.setCronSchedule(this.hour);
});
this.schedulerRegistry.addCronJob(this.dynamicProductsSuggestionCronJobName, job);
job.start();
}
public onModuleInit() {
this.setCronSchedule(this.hour);
}
}