Skip to content

Instantly share code, notes, and snippets.

@tiagoamaro
Forked from ayamomiji/deploy.rb
Last active December 21, 2015 12:38
Show Gist options
  • Save tiagoamaro/6306798 to your computer and use it in GitHub Desktop.
Save tiagoamaro/6306798 to your computer and use it in GitHub Desktop.
# Check if remote file exists
def remote_file_exists?(full_path)
'true' == capture("if [ -e #{full_path} ]; then echo 'true'; fi").strip
end
# Check if process is running
def remote_process_exists?(pid_file)
capture("ps -p $(cat #{pid_file}) ; true").strip.split("\n").size == 2
end
set(:rainbows_pid) { "#{current_path}/tmp/pids/rainbows.pid" }
set(:rainbows_old_pid) { "#{current_path}/tmp/pids/rainbows.pid.oldbin" }
set(:rainbows_bin) { "BUNDLE_GEMFILE=#{current_path}/Gemfile bundle exec rainbows" }
set(:rainbows_config) { "#{current_path}/config/rainbows.rb" }
namespace :rainbows do
task :start, roles: :app, except: {no_release: true} do
if remote_file_exists?(rainbows_pid)
if remote_process_exists?(rainbows_pid)
logger.important("Rainbows! is already running!", "rainbows")
next
else
run "rm #{rainbows_pid}"
end
end
if remote_file_exists?(rainbows_config)
logger.important("Starting...", "rainbows")
run "cd #{current_path}; #{rainbows_bin} -c #{rainbows_config} -E #{rails_env} -D"
else
logger.important("Config file was not found at \"#{rainbows_config}\"", "rainbows")
end
end
task :stop, roles: :app, except: {no_release: true} do
if remote_file_exists?(rainbows_pid)
if remote_process_exists?(rainbows_pid)
logger.important("Stopping...", "rainbows")
run "#{try_sudo} kill -s QUIT `cat #{rainbows_pid}`"
else
run "rm #{rainbows_pid}"
logger.important("Rainbows! is not running.", "rainbows")
end
else
logger.important("No PIDs found. Check if rainbows is running.", "rainbows")
end
end
task :restart, roles: :app, except: {no_release: true} do
if remote_file_exists?(rainbows_pid)
if remote_process_exists?(rainbows_pid)
logger.important("Stopping...", "rainbows")
run "#{try_sudo} kill -s USR2 `cat #{rainbows_pid}`"
# We now can shutdown old PID rainbows process, check documentation for signal order and shutdown http://rainbows.rubyforge.org/SIGNALS.html
run "sudo kill -s QUIT `cat #{rainbows_old_pid}`" if remote_process_exists?(rainbows_old_pid)
else
run "rm #{rainbows_pid}"
logger.important("Rainbows! is not running.", "rainbows")
start
end
else
logger.important("No PIDs found.", "rainbows")
start
end
end
end
# Set your full path to application.
app_path = File.expand_path(File.join(File.dirname(__FILE__), '..'))
# Set rainbows options
worker_processes 4
preload_app true
timeout 180
listen 4500
# Spawn rainbows master worker for user apps (group: apps)
# user 'apps', 'apps'
# Fill path to your app
working_directory app_path
# Should be 'production' by default, otherwise use other env
rails_env = ENV['RAILS_ENV'] || 'production'
# Log everything to one file
stderr_path 'log/rainbows.log'
stdout_path 'log/rainbows.log'
# Set master PID location
pid "#{app_path}/tmp/pids/rainbows.pid"
before_fork do |server, worker|
ActiveRecord::Base.connection.disconnect!
old_pid = "#{server.config[:pid]}.oldbin"
if File.exists?(old_pid) && server.pid != old_pid
begin
Process.kill("QUIT", File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
# someone else did our job for us
end
end
end
after_fork do |server, worker|
ActiveRecord::Base.establish_connection
end
Rainbows! do
use :EventMachine
worker_connections 1024
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment