From 06b35bdd97acdcba540a7dbc0170b9c8b9522523 Mon Sep 17 00:00:00 2001 From: Takashi Masuda Date: Sat, 25 Jul 2015 18:30:23 +0900 Subject: [PATCH] Add cap process `Restart 29hours` --- config/deploy.rb | 10 ++++- lib/capistrano/tasks/twenty_nine_hours.rake | 45 +++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 lib/capistrano/tasks/twenty_nine_hours.rake diff --git a/config/deploy.rb b/config/deploy.rb index 6e06e94..67c3b8d 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -26,7 +26,7 @@ # set :linked_files, fetch(:linked_files, []).push('config/database.yml', 'config/secrets.yml') # Default value for linked_dirs is [] -set :linked_dirs, %w{29hours/vendor/bundle} +set :linked_dirs, %w{29hours/log 29hours/tmp/pids 29hours/vendor/bundle} # Default value for default_env is {} # set :default_env, { path: "/opt/ruby/bin:$PATH" } @@ -46,4 +46,12 @@ capture "cd #{release_path}/29hours && curl -Ls -o settings.yml #{ENV.fetch('SETTINGS_FILE_PATH')}" end end + + desc 'Restart 29hours' + after :publishing, :restart do + on roles(:app) do + invoke 'twenty_nine_hours:stop' + invoke 'twenty_nine_hours:start' + end + end end diff --git a/lib/capistrano/tasks/twenty_nine_hours.rake b/lib/capistrano/tasks/twenty_nine_hours.rake new file mode 100644 index 0000000..d9f25ab --- /dev/null +++ b/lib/capistrano/tasks/twenty_nine_hours.rake @@ -0,0 +1,45 @@ +namespace :load do + task :defaults do + set :twenty_nine_hours_log, -> { shared_path.join('29hours', 'log', '29hours.log') } + set :twenty_nine_hours_pid, -> { shared_path.join('29hours', 'tmp', 'pids', '29hours.pid') } + end +end + +namespace :twenty_nine_hours do + desc 'Start 29hours' + task :start do + on roles(:app) do + within current_path.join('29hours') do + if test("[ -e #{fetch(:twenty_nine_hours_pid)} ] && kill -0 #{twenty_nine_hours_pid}") + info '29hours is running...' + else + execute :bundle, 'exec ruby 29hours.rb --production >>', fetch(:twenty_nine_hours_log), '2>&1 &; echo $! > ', fetch(:twenty_nine_hours_pid) + end + end + end + end + + desc 'Stop 29hours' + task :stop do + on roles(:app) do + within current_path.join('29hours') do + if test("[ -e #{fetch(:twenty_nine_hours_pid)} ]") + if test("kill -0 #{twenty_nine_hours_pid}") + info 'stopping 29hours...' + execute :kill, twenty_nine_hours_pid + execute :rm, fetch(:twenty_nine_hours_pid) + else + info 'cleaning up dead 29hours pid...' + execute :rm, fetch(:twenty_nine_hours_pid) + end + else + info '29hours is not running...' + end + end + end + end + + def twenty_nine_hours_pid + "`cat #{fetch(:twenty_nine_hours_pid)}`" + end +end