5

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!

yzalavin
  • 1,788
  • 1
  • 13
  • 19
  • Duplicate of: http://stackoverflow.com/questions/12143350/gracefully-shutting-down-sidekiq-processes – aliibrahim Feb 01 '16 at 08:49
  • Maybe I do not understand enough, but I do not need to stop the whole sidekiq process. Instead, I want to remove specific worker. – yzalavin Feb 01 '16 at 08:57
  • 1
    I dont think that is possible because sidekiq workers run as process. – aliibrahim Feb 01 '16 at 09:15
  • 1
    Why do you need to remove a worker? As far as I understand sidekiq each worker is a process that preboots the app and wait for a job that is pushed into your queue. So if there's no job in a queue the worker just waits for some. So emptying jobs queue doesn't do anything to workers that just wait for a job. As answered by Prashanth you're able to cancel a running job though, but I am almost sure this will not remove the worker itself – Jakub Feb 01 '16 at 11:50
  • Thanks for explanation! I need to remove worker because this worker implements long-polling technique for telegram bot. And after each deploy another worker is started that response twice. Each next deploy - another worker. Currently, I'm stopping the sidekiq from terminal and launching it. But this is not a best practice, I guess :) – yzalavin Feb 01 '16 at 13:02
  • @trnc sidekiq jobs are meant to have finite runtime, by your description it looks like you need a background service, not a sidekiq job – Vasfed Feb 01 '16 at 16:56

3 Answers3

8

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.

Mike Perham
  • 21,300
  • 6
  • 59
  • 61
3

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.

Prashanth
  • 241
  • 1
  • 7
  • In case you are interested in deleting workers that are running, this should suffice,https://github.com/mperham/sidekiq/wiki/FAQ#how-do-i-cancel-a-sidekiq-job – Prashanth Feb 01 '16 at 10:14
0

Short answer. Kill your process. and run this.

Sidekiq::Queue.new("default").clear

"default" is your queue name.

Jin Lim
  • 1,759
  • 20
  • 24