Agenda js - updating specific job doesn't work

41 Views Asked by At

Tried using previous posts with no solution. I got an array of jobs, and for every job I perform scheduleJob:

  private constructor(mongoUri: string) {
    const isDev = process.env.NODE_ENV === 'development';
    this.agenda = new Agenda({
      db: {
        address: mongoUri,
        collection: isDev ? 'agenda-dev' : 'agenda-prod',
      },
    });
  }

  private defineJob(name: string) {
    if (!this.agenda._definitions[name]) {
      this.agenda.define(name, async (job: AgendaJob) => {
        const { job: currentJob } = job.attrs.data;
         //... business logic
      });
    }
  }
  private async scheduleJob(job: JobDocument) {
    const { isActive, schedule, name, _id } = job;
    if (isActive) {
      this.defineJob(name);
      const agendaJob = this.agenda.create(name, { job });
      await agendaJob
        .unique({ 'data.job._id': _id })
        .repeatEvery(schedule)
        .save();

      console.log(
        `Job ${name} with ID ${_id} is scheduled to run every ${schedule}.`
      );
    } else{
      await this.agenda.cancel({ name });
      console.log(`Job ${name} with ID ${_id} has been canceled.`);
    }
  }

This doesn't update the job. What am I missing?

0

There are 0 best solutions below