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
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:
Trade-off
This approach prevents scheduling multiple jobs with the same name at different times.
Verification
After applying the fix: