Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running Job is not getting disabled #1546

Open
rahuljaswani opened this issue Aug 1, 2023 · 2 comments
Open

Running Job is not getting disabled #1546

rahuljaswani opened this issue Aug 1, 2023 · 2 comments

Comments

@rahuljaswani
Copy link

Background:
We have 2 types of agenda jobs running

  1. Monitor Job(scheduled every 30 min) - This job apply changes(change in schedule time, enable or disable a job) in the jobs based on some event
  2. Worker Job(let say scheduled every hour) - This is a job which is having some business logic and its scheduling is managed by the above Monitor job

Problem:

  1. When the Worker and Monitor jobs are running simultaneously and if the Monitor job tries to disable the Worker Job with
    agenda.disable({name: "worker-job"})
    The job gets updated with the disabled: true flag in the agenda collection but this flag gets overridden by the agenda when the Worker Job execution gets completed which makes the Worker job run next time which we don't want

Note: We don't want to stop the current execution of the Worker job we want to control the future run's only

Solutions we thought and rejected
In Monitor job use agenda.drain() to drain the agenda queue before disabling the job and once the job is disabled we set the agenda.processEvery() again
Rejected above approach because of 2 reasons:

  1. We could have multiple instances of the application(which contains Monitor and Worker Job)
  2. Since Monitor Job is an agenda job agenda.drain() will lead us to deadlock

Any help on disabling the running agenda job will be helpful.

@Jauki
Copy link

Jauki commented Aug 22, 2023

I have got the same problem...

const jobs = await agenda.jobs({
        name: jobName,
        'data.someID': new mongoose.Types.ObjectId(someID),
});

for (const job of jobs) {
        if (pause) {
          job.disable();
        } else {
          job.enable();
        }
}

I find the correct job, but when i set it to disable it still executes

@rahulkrrastogi
Copy link

rahulkrrastogi commented Jan 18, 2024

cancel the job and then stop the job instance(if the job instance is created for that job only)
By cancelling, the job record is removed from db.
If any other server is running the job. The job will not get created as the findoneandupdate query is triggered with upsert false.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants