Начинаем сайдкик с капистрано
-
21-12-2019 - |
Вопрос
Я хочу начать sidekiq с capistrano.Ниже приведен код для этого
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
Он выполняется успешно, но sidekiq по-прежнему не запускается на сервере.
выход:
$ 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"
Решение
Ваша проблема заключается здесь:
cd /home/project/current && bundle exec sidekiq -c 10 -e production -L log/sidekiq.log &
Когда вы добавляете &
в конце команда выполняется в отдельном процессе, но этот процесс по-прежнему является дочерним по отношению к текущему процессу и завершается при остановке текущего процесса.Вместо этого вам нужно запустить sidekiq от имени deamon.
bundle exec sidekiq -c 10 -e production -L log/sidekiq.log -d
Обратите внимание на дополнительные -d
вариант
Другие советы
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
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