how to delete a job in sidekiq

Ruby on-RailsSidekiq

Ruby on-Rails Problem Overview


I am using sidekiq in my rails app. Users of my app create reports that start a sidekiq job. However, sometimes users want to be able to cancel "processing" reports. Deleting the report is easy but I also need to be able to delete the sidekiq job as well.

So far I have been able to get a list of workers like so:

workers = Sidekiq::Workers.new

and each worker has args that include a report_id so I can identify which job belongs to which report. However, I'm not sure how to actually delete the job. It should be noted that I want to delete the job whether it is currently busy, or set in retry.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

According to this Sidekiq documentation page to delete a job with a single id you need to iterate the queue and call .delete on it.

queue = Sidekiq::Queue.new("mailer")
queue.each do |job|
  job.klass # => 'MyWorker'
  job.args # => [1, 2, 3]
  job.delete if job.jid == 'abcdef1234567890'
end

There is also a plugin called sidekiq-status that provides you the ability to cancel a single job

scheduled_job_id = MyJob.perform_in 3600
Sidekiq::Status.cancel scheduled_job_id #=> true

Solution 2 - Ruby on-Rails

The simplest way I found to do this is:

job = Sidekiq::ScheduledSet.new.find_job([job_id])

where [job_id] is the JID that pertains to the report. Followed by:

job.delete

I found no need to iterate through the entire queue as described by other answers here.

Solution 3 - Ruby on-Rails

I had the same problem, but the difference is that I needed to cancel a scheduled job, and my solution is:

Sidekiq::ScheduledSet.new.each do |_job|
  next unless [online_jid, offline_jid].include? _job.jid
  status = _job.delete
end

Solution 4 - Ruby on-Rails

If you want to cancel a scheduled job, I'm not sure about @KimiGao's answer, but this is what I adapted from Sidekiq's current API documentation:

jid = MyCustomWorker.perform_async

r = Sidekiq::ScheduledSet.new
jobs = r.select{|job| job.jid == jid }
jobs.each(&:delete)

Hope it helps.

Solution 5 - Ruby on-Rails

You can delete sidekiq job filtering by worker class and args:

class UserReportsWorker
  include Sidekiq::Worker
  def perform(report_id)
    # ...
  end
end


jobs = Sidekiq::ScheduledSet.new.select do |retri| 
  retri.klass == "UserReportsWorker" && retri.args == [42]
end
jobs.each(&:delete)

Solution 6 - Ruby on-Rails

I had the same problem. I solved it by registering the job id when I initialize it and by creating another function cancel! to delete it.

Here is the code:

after_enqueue do |job|
  sidekiq_job = nil

  queue = Sidekiq::Queue.new
  sidekiq_job = queue.detect do |j|
    j.item['args'][0]['job_id'] == job.job_id
  end

  if sidekiq_job.nil?
    scheduled = Sidekiq::ScheduledSet.new
    sidekiq_job = scheduled.detect do |j|
      j.item['args'][0]['job_id'] == job.job_id
    end
  end

  if sidekiq_job.present?
    booking = job.arguments.first
    booking.close_comments_jid = sidekiq_job.jid
    booking.save
  end
end


def perform(booking)
   # do something
end

def self.cancel!(booking)
  queue = Sidekiq::Queue.new
  sidekiq_job = queue.find_job(booking.close_comments_jid)

  if sidekiq_job.nil?
    scheduled = Sidekiq::ScheduledSet.new
    sidekiq_job = scheduled.find_job(booking.close_comments_jid)
  end

  if sidekiq_job.nil?
    # Report bug in my Bug Tracking tool
  else
    sidekiq_job.delete
    
  end
end

Solution 7 - Ruby on-Rails

Or you can use sidekiq page on rails server.

For example, http://localhost:3000/sidekiq, you can stop/remove the sidekiq jobs.

Before that, you have to updates the routes.rb.

require 'sidekiq/web'
mount Sidekiq::Web => '/sidekiq'

Solution 8 - Ruby on-Rails

There is simple way of deleting a job if you know the job_id:

job = Sidekiq::ScheduledSet.new.find_job(job_id)
begin
  job.delete
rescue
  Rails.logger.error "Job: (job_id: #{job_id}) not found while deleting jobs."
end

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMatthew BermanView Question on Stackoverflow
Solution 1 - Ruby on-RailsSimone CarlettiView Answer on Stackoverflow
Solution 2 - Ruby on-RailsRickView Answer on Stackoverflow
Solution 3 - Ruby on-RailsKimiGaoView Answer on Stackoverflow
Solution 4 - Ruby on-RailsthekingoftruthView Answer on Stackoverflow
Solution 5 - Ruby on-RailsMichał KurekView Answer on Stackoverflow
Solution 6 - Ruby on-RailsCéline Martinet SanchezView Answer on Stackoverflow
Solution 7 - Ruby on-RailshobbydevView Answer on Stackoverflow
Solution 8 - Ruby on-RailsPushp Raj SaurabhView Answer on Stackoverflow