diff --git a/lib/capistrano/tasks/unicorn.rake b/lib/capistrano/tasks/unicorn.rake index 2200dd6cb..d747dfb2b 100644 --- a/lib/capistrano/tasks/unicorn.rake +++ b/lib/capistrano/tasks/unicorn.rake @@ -1,16 +1,25 @@ +# https://bogomips.org/unicorn/SIGNALS.html namespace :unicorn do desc "Terminate unicorn processes (blocks until complete)" task :terminate do on roles(:app) do - execute "[[ -n $(pgrep -f unicorn) ]] && pgrep -f unicorn | xargs kill -SIGTERM" - sleep(5) + within current_path do + kill_unicorn("SIGQUIT") + sleep(10) + kill_unicorn("SIGTERM") + sleep(2) + kill_unicorn("SIGKILL") + end end end - desc "Kills unicorn processes (blocks until complete)" - task :kill do - on roles(:app) do - execute "[[ -n $(pgrep -f unicorn) ]] && pgrep -f unicorn | xargs kill -SIGKILL" + def unicorn_running? + test("[ -f #{fetch(:unicorn_pid)} ] && pkill --count --pidfile #{fetch(:unicorn_pid)}") + end + + def kill_unicorn(signal) + if unicorn_running? + execute :pkill, "--signal #{signal}", "--pidfile #{fetch(:unicorn_pid)}" end end end