Skip to content

Instantly share code, notes, and snippets.

@mitio
Created August 23, 2013 15:38
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save mitio/6320725 to your computer and use it in GitHub Desktop.
Save mitio/6320725 to your computer and use it in GitHub Desktop.
Sidekiq + Capistrano + Ubuntu Upstart
# config/deploy.rb
namespace :upstart do
desc 'Generate and upload Upstard configs for daemons needed by the app'
task :update_configs, except: {no_release: true} do
upstart_config_files = File.expand_path('../upstart/*.conf.erb', __FILE__)
upstart_root = '/etc/init'
Dir[upstart_config_files].each do |upstart_config_file|
config = ERB.new(IO.read(upstart_config_file)).result(binding)
path = "#{upstart_root}/#{File.basename upstart_config_file, '.erb'}"
put config, path
end
end
end
after 'deploy:update_code', 'upstart:update_configs'
# Add this to your /etc/sudoers file in order to allow the user
# www-data to control the Sidekiq worker daemon via Upstart:
#
# www-data ALL = (root) NOPASSWD: /sbin/start sidekiq, /sbin/stop sidekiq, /sbin/status sidekiq
namespace :sidekiq do
desc 'Start the sidekiq workers via Upstart'
task :start do
sudo 'start sidekiq'
end
desc 'Stop the sidekiq workers via Upstart'
task :stop do
sudo 'stop sidekiq || true'
end
desc 'Restart the sidekiq workers via Upstart'
task :restart do
sudo 'stop sidekiq || true'
sudo 'start sidekiq'
end
desc "Quiet sidekiq (stop accepting new work)"
task :quiet do
pid_file = "#{current_path}/tmp/pids/sidekiq.pid"
sidekiqctl_cmd = "bundle exec sidekiqctl"
run "if [ -d #{current_path} ] && [ -f #{pid_file} ] && kill -0 `cat #{pid_file}`> /dev/null 2>&1; then cd #{current_path} && #{sidekiqctl_cmd} quiet #{pid_file} ; else echo 'Sidekiq is not running'; fi"
end
end
before 'deploy:update_code', 'sidekiq:quiet'
after 'deploy:stop', 'sidekiq:stop'
after 'deploy:start', 'sidekiq:start'
before 'deploy:restart', 'sidekiq:restart'
# config/upstart/sidekiq.conf.erb
# /etc/init/sidekiq.conf - Sidekiq config for Ubuntu's Upstart
description "Sidekiq Background Workers"
start on runlevel [2345]
stop on runlevel [06]
respawn
respawn limit 3 30
exec su - <%= user %> -c 'cd <%= current_path %> && exec bundle exec sidekiq -i 0 -e <%= rails_env %> -C <%= current_path %>/config/sidekiq.yml'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment