9

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"
sagar junnarkar
  • 1,240
  • 2
  • 10
  • 18

3 Answers3

10

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
Vivek Tripathi
  • 267
  • 1
  • 4
  • 12
  • 1
    Wont this create some issues when sidekiq is not already running? – Zia Ul Rehman Mughal Jul 26 '17 at 05:02
  • Its safer to do `pgrep sidekiq` instead of `grep sidekiq | awk '{print $2}'`. This makes sure you wont take grep command pid instead of sidekiq. Also in case there are multiple sidekiq processes, you better kill all processes that you find – Meysam Feghhi May 14 '18 at 17:55
9

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

BroiSatse
  • 44,031
  • 8
  • 61
  • 86
  • @sagarjunnarkar - What is your sidekiq version? – BroiSatse Jun 03 '14 at 10:13
  • Sidekiq version is 2.6.0 – sagar junnarkar Jun 03 '14 at 10:14
  • @sagarjunnarkar - `-d` option has been added in 2.7.0. You can try running `sidekiq help` and check whether there is any equivalent (like --deamon) – BroiSatse Jun 03 '14 at 10:21
  • Thanks its working. Just last question. Is it ok if I just change sidekiq version in Gemfile or do I have to make lots of changes in code? – sagar junnarkar Jun 03 '14 at 10:34
  • @sagarjunnarkar - TO use 2.7 instead? I am not entirely sure what else have change between those two versions, but I wouldn't say much. Give it a go for a couple of weeks and search for issues with workers but it should go pretty smoothly. – BroiSatse Jun 03 '14 at 10:38
0

Just in case somehow trying to start/restart/stop environment with capistrano:

bundle exec cap production sidekiq:start
bundle exec cap production sidekiq:stop
bundle exec cap production sidekiq:restart
#staging
bundle exec cap staging sidekiq:start
bundle exec cap staging sidekiq:stop
bundle exec cap staging sidekiq:restart
#same with other dependencies
bundle exec cap production puma:restart
bundle exec cap staging puma:stop

Brief explanation

(in case you are hitting a repo online, like github remember to run your ssh agent to connect via ssh in the repo and pull latest version of the code/branch)

  • setup your own github ssh key locally
  • run ssh agent with the key eval $(ssh-agent) && ssh-add ~/.ssh/id_rsa
  • check agent with ssh -T git@github.com

After that i always use this to deploy

  • run capistrano targeting env bundle exec cap staging deploy

And these are really handy when you are already in prod and had issues but specially for staging, you could do individual exec depending on your Capfile (for instance most of the time i use puma as rack middleware server and sidekiq for scheculed-jobs)

Capfile

require "capistrano/setup"

# Include default deployment tasks
require "capistrano/deploy"

# Load the SCM plugin appropriate to your project:
#
# require "capistrano/scm/hg"
# install_plugin Capistrano::SCM::Hg
# or
# require "capistrano/scm/svn"
# install_plugin Capistrano::SCM::Svn
# or
require "capistrano/scm/git"
install_plugin Capistrano::SCM::Git

# Include tasks from other gems included in your Gemfile
#
# For documentation on these, see for example:
#
#   https://github.com/capistrano/rvm
#   https://github.com/capistrano/rbenv
#   https://github.com/capistrano/chruby
#   https://github.com/capistrano/bundler
#   https://github.com/capistrano/rails
#   https://github.com/capistrano/passenger
#
require "capistrano/rvm"
# require "capistrano/rbenv"
# require "capistrano/chruby"
require "capistrano/bundler"
require "capistrano/rails/assets"
require "capistrano/rails/migrations"
require "capistrano/yarn"
require "capistrano/puma"
install_plugin Capistrano::Puma  # Default puma tasks
require 'capistrano/sidekiq'
require 'slackistrano/capistrano'
require_relative 'lib/capistrano/slack_deployment_message'
# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }

So in the end for executing start|stop|restart on these features enabled/installed/configured by capistrano

I could always restart puma with capistrano in production:

  • bundle exec cap production sidekiq:restart
  • bundle exec cap production puma:restart

As well as in staging:

  • bundle exec cap staging sidekiq:restart
  • bundle exec cap staging puma:restart

Hope this helps! :D

d1jhoni1b
  • 7,497
  • 1
  • 51
  • 37