Agenda runs only one scheduled job from an array

28 Views Asked by At

I'm trying to run multiple scheduled jobs using Agenda. This is my code:

  private defineJobs() {
    this.agenda.define('alert123', async (job: Job) => {
      const { alert } = job?.attrs?.data;
      const { name } = alert;
      console.log('name ', name);
    });
  }

  async start() {
    try {
      await this.agenda.start();
      const alerts = [
        {
          name: 'job1',
          schedule: '*/2 * * * * *',
        },
        {
          name: 'job2',
          schedule: '*/3 * * * * *',
        },
      ];
      alerts.forEach((alert: any) => {
        this.agenda.every(alert.schedule, 'alert123', { alert });
      });
    } catch (error) {
      console.error('Error in startAgenda:', error);
    }
  }

This logs only 'job1' every 2 seconds. I tried different cron expressions as well.

What am I doing wrong?

0

There are 0 best solutions below