RocketChat / RocketChat/Rocket.Chat
Agenda creates duplicate jobs for repeated scheduling of the same job name
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 46.1k
- Forks
- 13.9k
- Avg merge
- 3d 3h
- Merged PRs (30d)
- 130
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at Agenda's _createScheduledJob() path, called by schedule(), and inspect how jobs are inserted into MongoDB. Review the job.unique({ name }, { insertOnly: true }) API and existing scheduling behavior before deciding how uniqueness should apply. Verify that repeated scheduling of the same job name produces only one database entry.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- mongodb, typescript
- Domain
- backend, databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 58/100