When doing a deployment, are the running sidekiq processes affected, stopped or killed when doing a deployment with capistrano?
Putting require 'sidekiq/capistrano in your deploy.rb will add these default tasks on deploy.
If you are doing something that is not purely vanilla, eg. using monit, then you may want to not add the require 'sidekiq/capistrano statement and write your own tasks and hooks.
eg.
Capistrano::Configuration.instance(:must_exist).load do
before "deploy:update_code", "sidekiq:quiet"
after "deploy:stop", "sidekiq:stop"
after "deploy:start", "sidekiq:start"
before "deploy:restart", "sidekiq:restart"
namespace :sidekiq do
desc "Quiet sidekiq (stop accepting new work)"
task :quiet, :roles => :app, :on_no_matching_servers => :continue do
run "/usr/sbin/service sidekiq quiet"
end
desc "Stop sidekiq"
task :stop, :roles => :app, :on_no_matching_servers => :continue do
run "sudo /usr/bin/monit stop sidekiq"
end
desc "Start sidekiq"
task :start, :roles => :app, :on_no_matching_servers => :continue do
run "sudo /usr/bin/monit start sidekiq"
end
desc "Restart sidekiq"
task :restart, :roles => :app, :on_no_matching_servers => :continue do
run "sudo /usr/bin/monit restart sidekiq"
end
end
end
Related
Due to the lack of documentation I resorted to reading the Capistrano 2 hanbook and came across 'default' task.
Example:
namespace :backup do
task :default do
web
db
end
task :web, :roles => :web do
puts "Backing Up Web Server"
end
task :db, :roles => :db do
puts "Backing Up DB Server"
end
end
The idea is that if I run cap backup it should execute the default task..which doesn't seem to work.
My code modified to suit the above:
namespace :status do
task :default do
webserver
db
end
desc "Check status of nginx on web server"
task :webserver do
on roles(:web) do |host|
execute 'hostname'
execute 'service nginx status'
end
end
desc "Check status of DB"
task :database do
on roles(:db) do |host|
execute 'hostname'
execute 'service postgresql status'
end
end
end
How do you run default tasks in Capistrano 3?
create a task outside a namespace and name that task the same as your namespace. Define that this task depends on your :default task within the name space
namespace :status do
task :default do
puts "Hello Default-Task!"
end
end
task :status => "status:default"
as you are using Rake within Capistrano 3 you can use that Rake trick.
I have a server that holds multiple rails apps, the majority of them are ruby, wheras there is one app that we use jruby
I always get this error after deploy under passenger:
Could not find rake-10.1.0 in any of the sources (Bundler::GemNotFound)
However, I don't get this when I locally rails s using WEBrick
the deploy script as following:
#server details
default_run_options[:pty] = true
set :user, "xxxxx"
set :scm_passphrase, "yyyyyy"
set :domain, "abc.abc.com"
set :application, "project_adsadsa"
#file path
set :repository, "git#github.com:jjjjjjjj/aaaaaaaa.git"
set :deploy_to, "/var/www/html/jdbc"
# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`
role :web, "ip_address" # Your HTTP server, Apache/etc
role :app, "ip_address" # This may be the same as your `Web` server
role :db, "ip_address", :primary => true # This is where Rails migrations will run
#miscellaneous options
set :rails_env, :development
set :deploy_via, :remote_cache
set :scm, 'git'
set :branch, 'master'
set :scm_verbose, true
set :use_sudo, false
ssh_options[:forward_agent] = true
namespace :deploy do
desc "cause Passenger to initiate a restart"
task :restart do
run "touch #{current_path}/tmp/restart.txt"
end
end
after "deploy:update_code", :bundle_install
after "deploy", :create_symlink
desc "do symlink the db"
task :create_symlink, :roles => :app do
run "ln -s #{shared_path}/development.sqlite3 #{current_path}/db/development.sqlite3"
end
desc "install the necessary prerequisites"
task :bundle_install, :roles => :app do
run "cd #{release_path} && bundle install --without production"
end
I have written a script named mailman_server using gem "mailman" placed in 'script/mailman_server'
#!/usr/bin/env ruby
require "rubygems"
require "bundler/setup"
require "mailman"
#Mailman.config.logger = Logger.new("log/mailman.log")
Mailman.config.poll_interval = 3
Mailman.config.pop3 = {
server: 'server', port: 110,
username: "loginid",
password: "password"
}
Mailman::Application.run do
default do
p "Found a new message"
# 'perform some action here'
end
end
It fetches all the emails from my account and then i do processing on them.
I have my deploy.rb file as
set :stages, %w(production) #various environments
load "deploy/assets" #precompile all the css, js and images... before deployment..
require "bundler/capistrano" # install all the new missing plugins...
require 'delayed/recipes' # load this for delayed job..
require 'capistrano/ext/multistage' # deploy on all the servers..
require "rvm/capistrano" # if you are using rvm on your server..
require './config/boot'
require 'airbrake/capistrano' # using airbrake in your application for crash notifications..
set :delayed_job_args, "-n 2" # number of delayed job workers
before "deploy:assets:symlink", "deploy:copy_database_file"
before "deploy:update_code", "delayed_job:stop" # stop the previous deployed job workers...
after "deploy:start", "delayed_job:start" #start the delayed job
after "deploy:restart", "delayed_job:restart" # restart it..
after "deploy:update", "deploy:cleanup" #clean up temp files etc.
set :rvm_ruby_string, '1.9.3' # ruby version you are using...
set :rvm_type, :user
server "my_server_ip", :app, :web, :db, :primary => true
set(:application) { "my_application_name" }
set (:deploy_to) { "/home/user/#{application}/#{stage}" }
set :user, 'user'
set :keep_releases, 3
set :repository, "git#bitbucket.org:my_random_git_repo_url"
set :use_sudo, false
set :scm, :git
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
set :deploy_via, :remote_cache
set :git_shallow_clone, 1
set :git_enable_submodules, 1
namespace :deploy do
task :start do ; end
task :stop do ; end
task :restart, :roles => :app, :except => { :no_release => true } do
run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
end
task :copy_database_file do
run "ln -sf #{shared_path}/database.yml #{release_path}/config/database.yml"
end
end
I want to execute this script every time I deploy to the server. Also I need to stop this script whenever I am deploying the code.
I am unable to figure out how can we start or stop this script using capistrano on server.
You could try to save pid of process on start with something like this
run "cd #{deploy_to}/current; ./script/mailman_server &; echo &! > /var/run/mailman_server.pid" #untested
and stop it with
run "kill `cat /var/run/mailman_server.pid`; rm /var/run/mailman_server.pid"
But I think you should check out Foreman, it provides handy way to run jobs in development and supports exporting your jobs to upstart or inid.d scripts for production, so you will need just to start or stop corresponding service with
run "sudo /etc/init.d/mailman_server start"
run "sudo /etc/init.d/mailman_server stop"
How can I add something to Capistrano's deploy task? I need to create a symlink in "public" directory.
Create a new task to create the symlink, then use a hook to add your task into the Capistrano deploy workflow where appropriate.
e.g.
namespace :deploy do
desc "symlink my file"
task :symlink_file, :roles => :app do
run "ln -s file public/file"
end
end
after 'deploy:update_code', 'deploy:symlink_file'
I'd like to stop some processes before running the deploy:migrate task. I know that I can redefine the deploy:migrate task by copying the existing code and adding the stop/start steps at the beginning and end of the task.
I'm wondering if there is a way to avoid copying the code from the default deploy:migrate task in my version of the task. Is there a way to refer to the existing deploy:migrate task when defining a new task of the same name?
Rather than redefining deploy:migrate, you should define a before or after hook for it. First, create a new task that does the stuff you need to do:
task :custom_name do
# whatever you need to do
end
And then set this new task to be run before or after the deploy:migrate task by doing one of the following:
before "deploy:migrate", :custom_name
after "deploy:migrate", :custom_name
For my requirements, I override the existing task in deploy.rb
namespace :deploy do
# to Override deploy:migrate task
task :precompile, :roles => :app, :except => { :no_release => true } do
run "your modified commands"
end
# to override deploy:assets:precompile task
namespace :assets do
task :precompile, :roles => :app, :except => { :no_release => true } do
run "your modified commands"
end
end
end