Skip to content

Commit

Permalink
Refactor to use mina-1.0
Browse files Browse the repository at this point in the history
Why:

Due to breaking API changes.
  • Loading branch information
scarfacedeb committed Oct 22, 2016
1 parent c7d675c commit 076f8d0
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 99 deletions.
27 changes: 13 additions & 14 deletions lib/mina/unicorn/tasks.rb
Original file line number Diff line number Diff line change
@@ -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

145 changes: 68 additions & 77 deletions lib/mina/unicorn/utility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 6 additions & 7 deletions mina-unicorn.gemspec
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion test/unicorn_test

0 comments on commit 076f8d0

Please sign in to comment.