Pedr Pedr - 1 month ago 21
Ruby Question

Restarting Sidekiq

What is the correct way to restart sidekiq. It seems to cache my workers' code when I start it, so every time I make a change to my workers I need to restart it. I'm doing this with Ctrl/C, but the process takes a long time to wind down and return me to the prompt.

Is there a way to force a restart with immediate effect?

I'm using the latest version with Sinatra running via POW.

Answer

Sidekiq comes with the command sidekiqctl, which can stop the PID associated with your Sidekiq process. You pass in the PID file and the # of seconds to wait for all threads to finish.

Sample Usage:

sidekiqctl stop #{rails_root}/tmp/pids/sidekiq_website_crawler.pid 60

Here, 60 represents the number of seconds to wait until all Sidekiq threads are done processing. If 60 seconds pass, and all aren't done, they are killed automatically.

I also recommend using the God gem to monitor, stop, start and restart Sidekiq.

Once you do that, you can use bundle exec god stop to stop all sidekiq threads.

Here is my God file, as an example:

rails_env = ENV['RAILS_ENV'] || "development"
rails_root = ENV['RAILS_ROOT'] || "/home/hwc218/BuzzSumo"
 God.watch do |w|
     w.dir      = "#{rails_root}"
     w.name     = "website_crawler"
     w.interval = 30.seconds
     w.env      = {"RAILS_ENV" => rails_env}
     w.interval = 30.seconds
     w.start = "bundle exec sidekiq -C #{rails_root}/config/sidekiq_website_crawler.yml"
     w.stop = "sidekiqctl stop #{rails_root}/tmp/pids/sidekiq_website_crawler.pid 60"
     w.keepalive


    # determine the state on startup
     w.transition(:init, { true => :up, false => :start }) do |on|
    on.condition(:process_running) do |c|
      c.running = true
    end
    end

     # determine when process has finished starting
      w.transition([:start, :restart], :up) do |on|
      on.condition(:process_running) do |c|
      c.running = true
      c.interval = 5.seconds
    end

      # failsafe
       on.condition(:tries) do |c|
      c.times = 5
      c.transition = :start
      c.interval = 5.seconds
     end
    end

    # start if process is not running
     w.transition(:up, :start) do |on|
    on.condition(:process_running) do |c|
      c.running = false
    end
    end

    w.restart_if do |restart|
        restart.condition(:restart_file_touched) do |c|
          c.interval = 5.seconds
          c.restart_file = File.join(rails_root, 'tmp', 'restart.txt')
        end
    end
 end