How to kill Sidekiq worker?

10,104

Solution 1

Once a worker has started processing a job, you cannot stop it externally without shutting down the entire process. There is no safe way to stop a thread.

You can add the ability to stop a worker to your own code by having the worker code check regularly to see if it should stop, similar to job cancellation.

Solution 2

I haven't tried it myself, but based on the documentation:

You could add all the workers meant to be deleted as part of a queue (eg. post_deploy)

class MyWorker
  include Sidekiq::Worker
  sidekiq_options :retry => 5, :dead => false, queue: :post_deploy

  def perform()
  ...
  end
end

For deleting the workers:

require 'sidekiq/api'
Sidekiq::Queue.new("post_deploy").clear

To find the number of jobs under the queue, you need to find it this way

Sidekiq::Queue.new("post_deploy").size

Only to find number of Sidekiq threads that are currently running, you will be using Sidekiq::Worker.size

Let me know if I have misunderstood your question.

Share:
10,104
yzalavin
Author by

yzalavin

Updated on June 12, 2022

Comments

  • yzalavin
    yzalavin almost 2 years

    I've got an application that after each deploy needs to kill specific Sidekiq worker. As it's api suggests I can do it by removing all jobs from queue Sidekiq::Queue.new.clear.

    However, after running this command the number of worker sizes Sidekiq::Workers.new.size still the same. Actually, I've tried a lot of methods but nothing works. Please, help!