From 076f8d0cc7a6c0eeec22e5626fe8b60d4df81b2f Mon Sep 17 00:00:00 2001 From: Andrew Volozhanin Date: Sun, 23 Oct 2016 01:02:41 +0300 Subject: [PATCH] Refactor to use mina-1.0 Why: Due to breaking API changes. --- lib/mina/unicorn/tasks.rb | 27 ++++--- lib/mina/unicorn/utility.rb | 145 +++++++++++++++++------------------- mina-unicorn.gemspec | 13 ++-- test/unicorn_test | 2 +- 4 files changed, 88 insertions(+), 99 deletions(-) diff --git a/lib/mina/unicorn/tasks.rb b/lib/mina/unicorn/tasks.rb index f6b89be..36ad796 100644 --- a/lib/mina/unicorn/tasks.rb +++ b/lib/mina/unicorn/tasks.rb @@ -1,36 +1,35 @@ -require 'mina/bundler' -require 'mina/rails' -require 'mina/unicorn/utility' +require "mina/bundler" +require "mina/rails" +require "mina/unicorn/utility" namespace :unicorn do include Mina::Unicorn::Utility # Following recommendations from http://unicorn.bogomips.org/unicorn_1.html#rack-environment - set_default :unicorn_env, -> { fetch(:rails_env) == 'development' ? 'development' : 'deployment' } - set_default :unicorn_config, -> { "#{deploy_to}/#{current_path}/config/unicorn.rb" } - set_default :unicorn_pid, -> { "#{deploy_to}/#{current_path}/tmp/pids/unicorn.pid" } - set_default :unicorn_cmd, -> { "#{bundle_prefix} unicorn" } - set_default :unicorn_restart_sleep_time, -> { 2 } - set_default :bundle_gemfile, -> { "#{deploy_to}/#{current_path}/Gemfile" } + set :unicorn_env, -> { fetch(:rails_env) == "development" ? "development" : "deployment" } + set :unicorn_config, -> { "#{fetch(:current_path)}/config/unicorn.rb" } + set :unicorn_pid, -> { "#{fetch(:current_path)}/tmp/pids/unicorn.pid" } + set :unicorn_cmd, -> { "#{fetch(:bundle_prefix)} unicorn" } + set :unicorn_restart_sleep_time, -> { 2 } + set :bundle_gemfile, -> { "#{fetch(:current_path)}/Gemfile" } desc "Start Unicorn master process" task start: :environment do - queue start_unicorn + command start_unicorn end desc "Stop Unicorn" task stop: :environment do - queue kill_unicorn('QUIT') + command kill_unicorn("QUIT") end desc "Immediately shutdown Unicorn" task shutdown: :environment do - queue kill_unicorn('TERM') + command kill_unicorn("TERM") end desc "Restart unicorn service" task restart: :environment do - queue restart_unicorn + command restart_unicorn end end - diff --git a/lib/mina/unicorn/utility.rb b/lib/mina/unicorn/utility.rb index 1e4a381..73f5ff2 100644 --- a/lib/mina/unicorn/utility.rb +++ b/lib/mina/unicorn/utility.rb @@ -3,114 +3,105 @@ module Mina module Unicorn module Utility + def start_unicorn + %{ + if [ -e "#{unicorn_pid}" ]; then + if #{unicorn_user} kill -0 `cat #{unicorn_pid}` > /dev/null 2>&1; then + echo "-----> Unicorn is already running!"; + exit 0; + fi; - # Run a command as the :unicorn_user user if :unicorn_user is a string. - # Otherwise run as default (:user) user. - # - def try_unicorn_user - "sudo -u #{unicorn_user}" if unicorn_user.kind_of?(String) - end + #{unicorn_user} rm #{unicorn_pid}; + fi; - # Check if a remote process exists using its pid file - # - def remote_process_exists?(pid_file) - "[ -e #{pid_file} ] && #{try_unicorn_user} kill -0 `cat #{pid_file}` > /dev/null 2>&1" + echo "-----> Starting Unicorn..."; + cd #{fetch(:current_path)} && #{unicorn_user} BUNDLE_GEMFILE=#{fetch(:bundle_gemfile)} #{fetch(:unicorn_cmd)} -c #{fetch(:unicorn_config)} -E #{fetch(:unicorn_env)} -D; + } end - # Stale Unicorn process pid file - # - def old_unicorn_pid - "#{unicorn_pid}.oldbin" + def kill_unicorn(signal) + %{ + if #{unicorn_is_running?}; then + echo "-----> Stopping Unicorn..."; + #{unicorn_send_signal(signal)}; + else + echo "-----> Unicorn is not running."; + fi; + } end - # Command to check if Unicorn is running - # - def unicorn_is_running? - remote_process_exists?(unicorn_pid) - end + def restart_unicorn + %{ + #{duplicate_unicorn} - # Command to check if stale Unicorn is running - # - def old_unicorn_is_running? - remote_process_exists?(old_unicorn_pid) + sleep #{fetch(:unicorn_restart_sleep_time)}; # in order to wait for the (old) pidfile to show up + + if #{old_unicorn_is_running?}; then + #{unicorn_send_signal("QUIT", get_old_unicorn_pid)}; + fi; + } end - # Get unicorn master process PID (using the shell) - # - def get_unicorn_pid(pid_file=unicorn_pid) - "`cat #{pid_file}`" + # Send a signal to a unicorn master processes + def unicorn_send_signal(signal, pid=get_unicorn_pid) + "#{unicorn_user} kill -s #{signal} #{pid}" end - # Get unicorn master (old) process PID - # - def get_old_unicorn_pid - get_unicorn_pid(old_unicorn_pid) + private + + # Run a command as the :unicorn_user user if :unicorn_user is set + # Otherwise run without sudo + def unicorn_user + "sudo -u #{fetch(:unicorn_user)}" if set?(:unicorn_user) end - # Send a signal to a unicorn master processes + # Check if a remote process exists using its pid file # - def unicorn_send_signal(signal, pid=get_unicorn_pid) - "#{try_unicorn_user} kill -s #{signal} #{pid}" + def remote_process_exists?(pid_file) + "[ -e #{pid_file} ] && #{unicorn_user} kill -0 `cat #{pid_file}` > /dev/null 2>&1" end - # Kill Unicorns in multiple ways O_O - # - def kill_unicorn(signal) - script = <<-END + def duplicate_unicorn + %{ if #{unicorn_is_running?}; then - echo "-----> Stopping Unicorn..."; - #{unicorn_send_signal(signal)}; + echo "-----> Duplicating Unicorn..."; + #{unicorn_send_signal("USR2")}; else - echo "-----> Unicorn is not running."; + #{start_unicorn} fi; - END - - script + } end - # Start the Unicorn server + # Command to check if Unicorn is running # - def start_unicorn - %Q% - if [ -e "#{unicorn_pid}" ]; then - if #{try_unicorn_user} kill -0 `cat #{unicorn_pid}` > /dev/null 2>&1; then - echo "-----> Unicorn is already running!"; - exit 0; - fi; - - #{try_unicorn_user} rm #{unicorn_pid}; - fi; - - echo "-----> Starting Unicorn..."; - cd #{deploy_to}/#{current_path} && #{try_unicorn_user} BUNDLE_GEMFILE=#{bundle_gemfile} #{unicorn_cmd} -c #{unicorn_config} -E #{unicorn_env} -D; - % + def unicorn_is_running? + remote_process_exists?(unicorn_pid) end - # Restart the Unicorn server + # Command to check if stale Unicorn is running # - def restart_unicorn - %Q% - #{duplicate_unicorn} + def old_unicorn_is_running? + remote_process_exists?(old_unicorn_pid) + end - sleep #{unicorn_restart_sleep_time}; # in order to wait for the (old) pidfile to show up + def unicorn_pid + fetch(:unicorn_pid) + end - if #{old_unicorn_is_running?}; then - #{unicorn_send_signal('QUIT', get_old_unicorn_pid)}; - fi; - % + # Stale Unicorn process pid file + def old_unicorn_pid + "#{unicorn_pid}.oldbin" end - def duplicate_unicorn - %Q% - if #{unicorn_is_running?}; then - echo "-----> Duplicating Unicorn..."; - #{unicorn_send_signal('USR2')}; - else - #{start_unicorn} - fi; - % + # Get unicorn master (old) process PID + def get_old_unicorn_pid + get_unicorn_pid(old_unicorn_pid) end + # Get unicorn master process PID (using the shell) + def get_unicorn_pid(pid_file=unicorn_pid) + "`cat #{pid_file}`" + end end end end diff --git a/mina-unicorn.gemspec b/mina-unicorn.gemspec index fdcf9e8..eff2624 100644 --- a/mina-unicorn.gemspec +++ b/mina-unicorn.gemspec @@ -1,24 +1,23 @@ -# coding: utf-8 -lib = File.expand_path('../lib', __FILE__) +lib = File.expand_path("../lib", __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require 'mina/unicorn/version' + +require "mina/unicorn/version" Gem::Specification.new do |spec| spec.name = "mina-unicorn" spec.version = Mina::Unicorn::VERSION spec.authors = ["tab", "Andrew Volozhanin"] - spec.email = ["linuxheadrus@gmail.com"] + spec.email = ["scarfacedeb@gmail.com"] spec.summary = %q{Unicorn tasks for Mina} spec.description = %q{Unicorn tasks for Mina} spec.homepage = "https://github.com/scarfacedeb/mina-unicorn" spec.license = "MIT" spec.files = `git ls-files`.split($/) - spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } - spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) + spec.test_files = [] spec.require_paths = ["lib"] - spec.add_dependency "mina", "~> 0.3", "< 1.0" + spec.add_dependency "mina", "~> 1.0" spec.add_development_dependency "bundler" spec.add_development_dependency "rake" diff --git a/test/unicorn_test b/test/unicorn_test index e399aee..8dc2e27 160000 --- a/test/unicorn_test +++ b/test/unicorn_test @@ -1 +1 @@ -Subproject commit e399aeed2c951d2d87f74efe037a63894239ea14 +Subproject commit 8dc2e276edfcf8668c670ad9a86b1927f8554066