I have tried to implement cronjob in my loopback 4 application. I have followed the given github repository
https://github.com/dhmlau/cron-test/tree/master
cronjob.ts:
import { CronJob, cronJob } from "@loopback/cron";
import { repository } from "@loopback/repository";
import { Schedule } from "../models/schedule.model";
import { ScheduleRepository } from "../repositories/schedule.repository";
@cronJob()
export class MyCronJob extends CronJob {
constructor(
@repository(ScheduleRepository)
public scheduleRepository: ScheduleRepository
) {
super({
name: "job-B",
onTick: async () => {
// do the work
let schedule: Schedule[] = await this.findSchedule();
console.log(new Date());
console.log(schedule);
},
cronTime: "*/1 * * * *",
start: false
});
}
async findSchedule(): Promise<Schedule[]> {
console.log(new Date());
const dailyRuns = await this.scheduleRepository.find({ where: { isEnabled: true } });
console.log(dailyRuns)
return dailyRuns
}
}
application.ts:
this.component(CronComponent);
this.add(createBindingFromClass(MyCronJob));
I didn't get the response from the find method. On each minute the log for new Date() has been logged, but dailyRuns not getting logged