Starting sidekiq with capistrano

12,486

Solution 1

Your problem lies here:

  cd /home/project/current && bundle exec sidekiq -c 10 -e production -L log/sidekiq.log &

When you add & at the end command is being executed in a separate process, but this process is still a child of a current process and is terminated when current process stops. Instead you need to run sidekiq as a deamon.

bundle exec sidekiq -c 10 -e production -L log/sidekiq.log -d

Note the extra -d option

Solution 2

Without making use of any gem, here is my solution working perfectly with Capistrano 3.4.0

namespace :sidekiq do

  task :restart do
    invoke 'sidekiq:stop'
    invoke 'sidekiq:start'
  end

  before 'deploy:finished', 'sidekiq:restart'

  task :stop do
    on roles(:app) do
      within current_path do
        pid = p capture "ps aux | grep sidekiq | awk '{print $2}' | sed -n 1p"
        execute("kill -9 #{pid}")
      end
    end
  end

  task :start do
    on roles(:app) do
      within current_path do
        execute :bundle, "exec sidekiq -e #{fetch(:stage)} -C config/sidekiq.yml -d"
      end
    end
  end
end
Share:
12,486
sagar junnarkar
Author by

sagar junnarkar

Ruby on rails developer

Updated on July 24, 2022

Comments

  • sagar junnarkar
    sagar junnarkar almost 2 years

    I want to start sidekiq with capistrano. Below is code for that

    namespace :sidekiq do
      task :start do
        run "cd #{current_path} && bundle exec sidekiq -c 10 -e production -L log/sidekiq.log &"
        p capture("ps aux | grep sidekiq | awk '{print $2}' | sed -n 1p").strip!    
      end
    end
    

    It executes successfully but still sidekiq is not started on server.

    output:

    $ cap sidekiq:start
        triggering load callbacks
      * 2014-06-03 15:03:01 executing `sidekiq:start'
      * executing "cd /home/project/current && bundle exec sidekiq -c 10 -e production -L log/sidekiq.log &"
        servers: ["x.x.x.x"]
        [x.x.x.x] executing command
        command finished in 1229ms
      * executing "ps aux | grep sidekiq | awk '{print $2}' | sed -n 1p"
        servers: ["x.x.x.x"]
        [x.x.x.x] executing command
        command finished in 1229ms
    "19291"