Skip to content

Agenda creates duplicate jobs for repeated scheduling of the same job name #40252

@NagajyothiChukka

Description

@NagajyothiChukka

Duplicate jobs created when scheduling the same job multiple times

Description

Duplicate jobs are created when scheduling the same job multiple times, particularly through methods like schedule(), which internally create jobs without enforcing uniqueness.

Steps to Reproduce

await agenda.schedule('in 1 minute', 'myJob');
await agenda.schedule('in 1 minute', 'myJob');

Expected Behavior

Only one job per job name should exist.

Actual Behavior

Multiple duplicate jobs are inserted into MongoDB.

Root Cause

The _createScheduledJob() method does not enforce uniqueness, leading to multiple insertions for the same job name.

Proposed Solution

A possible solution is to enforce uniqueness at the job level using Agenda’s job.unique() API:

job.unique({ name }, { insertOnly: true });

This would:

  • Prevent duplicate job insertions when scheduling the same job multiple times
  • Ensure only one job is created for identical scheduling requests

Trade-off

This approach prevents scheduling multiple jobs with the same name at different times.

Verification

After applying the fix:

  • Repeated scheduling of the same job results in only one database entry

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions