Pregunta

I've accidentally enqueued a bunch of jobs in Sidekiq. I do not want to wipe my entire Redis store (and reset ALL Sidekiq data and enqueued jobs to nil) but I would like to remove all enqueued jobs that can be identified by a given class. How would I do this?

¿Fue útil?

Solución

These answers were helpful, but didn't answer the original question for me. It's possible those solutions are out of date.

You have to access the job's args and grab it's actual job class within the loop scope. I tried the above and it did not work as expected because job.klass does not return what you'd expect it to.

This is what it returns in the terminal currently:

queue.each do |job|
  puts job.klass
end

ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper
=> nil

So my solution was to dig into the job's arguments like so:

queue = Sidekiq::Queue.new("job_queue_name")

queue.each do |job|
  puts job.args.first['job_class']
  job.delete if job.args.first['job_class'] == "Things::DoesThatThingJob"
end

I'm sure there's a way to write this more elegantly (maybe with a select?) But it's readable. Hope I was able to help others like me that were looking for something like this.

Otros consejos

I found the Sidekiq API provides an easy way to do what I need:

queue = Sidekiq::Queue.new(queue_name)
queue.each do |job|
    puts job.klass
    job.delete if job.klass == job_class
end

try a method like this in a helper module, where klass is the Worker class.

 def self.delete_jobs_for_worker(klass)

  jobs = Sidekiq::ScheduledSet.new
  jobs.select do |job|
    job.klass == 'Sidekiq::Extensions::DelayedClass' &&
        ((job_klass, job_method, args) = YAML.load(job.args[0])) &&
        job_klass == klass 
  end.map(&:delete)

end
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top