Developer
Developer

Reputation: 63

Multiple job schedules are missing using agenda

I have saved some schedules in the Mongo database. On service start, I get all the schedule details and pass them to the agenda. Currently, 100 jobs need to run 5 mins apart, but some entries are getting missed in the "agendaJobs" collection, and that is why missing entries are not running. And on every restart, random jobs get missed.

Below is a sample code, Any idea what I am missing?

export default class class {
private readonly agenda: Agenda;
constructor() {
    this.agenda = new Agenda(
        {
            db: {
                address: this.dbURI,
            }
        });
}

public async scheduleJob(job: Job) {
    this.agenda.cancel({ name: job.id });
    if (job.status === JobStatus.ACTIVE) {
        this.agenda.define(job.id, (agendaJob, done) => {
            this.logger.debug(`--- Job Defined: Job ${job.name}-${job.schedule}-${job.id}`);
            this.executeJob(job);
            done();
        });
    }
    await this.agenda.start({ name: job.id });
    await this.agenda.every(job.schedule, job.id);
}

public triggerJobs(): void {
    this.repostory.findJobs().then((jobs: Job[]) => {
        jobs.forEach((job: Job) => {
            this.scheduleJob(job);
        });
    }).catch((err: any) => {
        this.logger.error(err);
    });
}

}

Upvotes: 1

Views: 30

Answers (0)

Related Questions