Pregunta

I am using sidekiq for my workers. Let's say I have 10k jobs schedule for one individual worker, but 4k of those jobs are from only one user, how can I cancel only the jobs related to that user. (let`s say that the user_id is being sent as parameter for the worker).

¿Fue útil?

Solución

There's probably a more efficient way to do this, but what you can do is iterate over the jobs currently in the queue, and remove the ones that match your criteria, something like this:

def delete_jobs_by_user_id(queue, user_id)
  Sidekiq.redis do |r| 
    queue_key = "queue:#{queue}"
    jobs = r.lrange(queue_key, 0, -1) 

    jobs.each do |job|
      r.lrem(queue_key, 0, job) if JSON.load(job)['args'].include?(user_id)
    end 
  end
end

You'll want to add some more checks to only remove the job if it's the class you're looking for, the arg is in the right position, etc., but this should get you on the right track.

And obviously test this a bunch before running it on prod, and probably don't run it on a queue with a huge backlog. But in a pinch, it could work.

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