namespace :load do task :defaults do set :unicorn_pid, -> { File.join(shared_path, "pids", "unicorn.pid") } set :unicorn_config_path, -> { File.join(current_path, "config", "unicorn", "#{fetch(:rails_env)}.rb") } set :unicorn_restart_sleep_time, 3 end end namespace :unicorn do desc 'Start Unicorn' task :start do on roles(:app) do within release_path do with rails_env: fetch(:rails_env) do if test("[ -e #{fetch(:unicorn_pid)} ] && kill -0 #{pid}") info "unicorn is running..." else execute :bundle, "exec unicorn", "-c", fetch(:unicorn_config_path), "-E", fetch(:rails_env), "-D" end end end end end desc 'Stop Unicorn (QUIT)' task :stop do on roles(:app) do within release_path do if test("[ -e #{fetch(:unicorn_pid)} ]") if test("kill -0 #{pid}") info "stopping unicorn..." execute :kill, "-s QUIT", pid else info "cleaning up dead unicorn pid..." execute :rm, fetch(:unicorn_pid) end else info "unicorn is not running..." end end end end desc 'Reload Unicorn (HUP); use this when preload_app: false' task :reload do invoke 'unicorn:start' on roles(:app) do within release_path do info "reloading..." execute :kill, "-s HUP", pid end end end desc 'Restart Unicorn (USR2 + QUIT); use this when preload_app: true' task :restart do invoke 'unicorn:start' on roles(:app) do within release_path do info "unicorn restarting..." execute :kill, "-s USR2", pid execute :sleep, fetch(:unicorn_restart_sleep_time) if test("[ -e #{fetch(:unicorn_pid)}.oldbin ]") execute :kill, "-s QUIT", pid_oldbin end end end end desc 'Add a worker (TTIN)' task :add_worker do on roles(:app) do within release_path do info "adding worker" execute :kill, "-s TTIN", pid end end end desc 'Remove a worker (TTOU)' task :remove_worker do on roles(:app) do within release_path do info "removing worker" execute :kill, "-s TTOU", pid end end end end def pid "`cat #{fetch(:unicorn_pid)}`" end def pid_oldbin "`cat #{fetch(:unicorn_pid)}.oldbin`" end