Sometimes the speed at which Sidekiq processes jobs can be so fast that it can
Sometimes the speed at which Sidekiq processes jobs can be so fast that it can
...
@@ -260,9 +267,34 @@ end
...
@@ -260,9 +267,34 @@ end
### Remove Sidekiq jobs for given parameters (destructive)
### Remove Sidekiq jobs for given parameters (destructive)
The general method to kill jobs conditionally is the following:
```ruby
queue=Sidekiq::Queue.new('<queue name>')
queue.each{|job|job.deleteif<condition>}
```
NOTE: **Note:** This will remove jobs that are queued but not started, running jobs will not be killed. Have a look at the section below for cancelling running jobs.
In the method above, `<queue-name>` is the name of the queue that contains the job(s) you want to delete and `<condition>` will decide which jobs get deleted.
Commonly, `<condition>` references the job arguments, which depend on the type of job in question. To find the arguments for a specific queue, you can have a look at the `perform` function of the related worker file, commonly found at `/app/workers/<queue-name>_worker.rb`.
For example, `repository_import` has `project_id` as the job argument, while `update_merge_requests` has `project_id, user_id, oldrev, newrev, ref`.
NOTE: **Note:** Arguments need to be referenced by their sequence id using `job.args[<id>]` because `job.args` is a list of all arguments provided to the Sidekiq job.
Here are some examples:
```ruby
queue=Sidekiq::Queue.new('update_merge_requests')
# In this example, we want to remove any update_merge_requests jobs
# for the Project with ID 125 and ref `ref/heads/my_branch`