17

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.

Undistraction
  • 42,754
  • 56
  • 195
  • 331

1 Answers1

17

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
Aamir
  • 16,329
  • 10
  • 59
  • 65
Henley
  • 21,258
  • 32
  • 119
  • 207
  • What should my sidekiq.pid file look like? – Undistraction Jan 16 '13 at 10:44
  • 2
    It should just contain the PID(number) of the Sidekiq process ID. Run ps -ef | grep "sidekiq" to get it. – Henley Jan 16 '13 at 13:49
  • Would you mind explaining that in more detail. When should I run it? Surely I would need to run it every time I started Sidekiq? Then add it to god.config. – Undistraction Jan 16 '13 at 14:00
  • When you run sidekiq, you pass it a yml file where you specify the PID FILE. Here's an example: https://github.com/mperham/sidekiq/blob/master/examples/config.yml . So you'd run bundle exec sidekiq -C – Henley Jan 16 '13 at 23:02
  • Thanks. I was configuring it, then starting it, but needed to do both at the same time. – Undistraction Jan 18 '13 at 10:00
  • To clarify @HenleyChiu's answer, you can use `ps -ef | grep sidekiq` to find the pid, then create a file (e.g., `sidekiq.pid`) with the only contents being the pid you just found, then `sidekiqctl stop ` – eebbesen Aug 15 '13 at 20:13