Commit ce63d1f1 by ErgoLau

move method to module

parent 05b83e7e
require 'lib/sneakers_helper'
namespace :load do namespace :load do
task :defaults do task :defaults do
set :sneakers_default_hooks, true set :sneakers_default_hooks, true
...@@ -39,11 +41,11 @@ namespace :sneakers do ...@@ -39,11 +41,11 @@ namespace :sneakers do
desc 'Quiet sneakers (stop processing new tasks)' desc 'Quiet sneakers (stop processing new tasks)'
task :quiet do task :quiet do
on roles fetch(:sneakers_roles) do |role| on roles fetch(:sneakers_roles) do |role|
switch_user(role) do SneakersHelper.switch_user(role) do
if test("[ -d #{current_path} ]") if test("[ -d #{current_path} ]")
each_process_with_index(true) do |pid_file, idx| SneakersHelper.each_process_with_index(true) do |pid_file, idx|
if pid_file_exists?(pid_file) && process_exists?(pid_file) if SneakersHelper.pid_file_exists?(pid_file) && SneakersHelper.process_exists?(pid_file)
quiet_sneakers(pid_file) SneakersHelper.quiet_sneakers(pid_file)
end end
end end
end end
...@@ -54,11 +56,11 @@ namespace :sneakers do ...@@ -54,11 +56,11 @@ namespace :sneakers do
desc 'Stop sneakers' desc 'Stop sneakers'
task :stop do task :stop do
on roles fetch(:sneakers_roles) do |role| on roles fetch(:sneakers_roles) do |role|
switch_user(role) do SneakersHelper.switch_user(role) do
if test("[ -d #{current_path} ]") if test("[ -d #{current_path} ]")
each_process_with_index(true) do |pid_file, idx| SneakersHelper.each_process_with_index(true) do |pid_file, idx|
if pid_file_exists?(pid_file) && process_exists?(pid_file) if SneakersHelper.pid_file_exists?(pid_file) && SneakersHelper.process_exists?(pid_file)
stop_sneakers(pid_file) SneakersHelper.stop_sneakers(pid_file)
end end
end end
end end
...@@ -69,10 +71,10 @@ namespace :sneakers do ...@@ -69,10 +71,10 @@ namespace :sneakers do
desc 'Start sneakers' desc 'Start sneakers'
task :start do task :start do
on roles fetch(:sneakers_roles) do |role| on roles fetch(:sneakers_roles) do |role|
switch_user(role) do SneakersHelper.switch_user(role) do
each_process_with_index do |pid_file, idx| SneakersHelper.each_process_with_index do |pid_file, idx|
unless pid_file_exists?(pid_file) && process_exists?(pid_file) unless SneakersHelper.pid_file_exists?(pid_file) && SneakersHelper.process_exists?(pid_file)
start_sneakers(pid_file, idx) SneakersHelper.start_sneakers(pid_file, idx)
end end
end end
end end
...@@ -91,12 +93,12 @@ namespace :sneakers do ...@@ -91,12 +93,12 @@ namespace :sneakers do
desc 'Rolling-restart sneakers' desc 'Rolling-restart sneakers'
task :rolling_restart do task :rolling_restart do
on roles fetch(:sneakers_roles) do |role| on roles fetch(:sneakers_roles) do |role|
switch_user(role) do SneakersHelper.switch_user(role) do
each_process_with_index(true) do |pid_file, idx| SneakersHelper.each_process_with_index(true) do |pid_file, idx|
if pid_file_exists?(pid_file) && process_exists?(pid_file) if SneakersHelper.pid_file_exists?(pid_file) && SneakersHelper.process_exists?(pid_file)
stop_sneakers(pid_file) SneakersHelper.stop_sneakers(pid_file)
end end
start_sneakers(pid_file, idx) SneakersHelper.start_sneakers(pid_file, idx)
end end
end end
end end
...@@ -105,10 +107,10 @@ namespace :sneakers do ...@@ -105,10 +107,10 @@ namespace :sneakers do
# Delete any pid file not in use # Delete any pid file not in use
task :cleanup do task :cleanup do
on roles fetch(:sneakers_roles) do |role| on roles fetch(:sneakers_roles) do |role|
switch_user(role) do SneakersHelper.switch_user(role) do
each_process_with_index do |pid_file, idx| SneakersHelper.each_process_with_index do |pid_file, idx|
unless process_exists?(pid_file) unless SneakersHelper.process_exists?(pid_file)
if pid_file_exists?(pid_file) if SneakersHelper.pid_file_exists?(pid_file)
execute "rm #{pid_file}" execute "rm #{pid_file}"
end end
end end
...@@ -122,104 +124,13 @@ namespace :sneakers do ...@@ -122,104 +124,13 @@ namespace :sneakers do
task :respawn do task :respawn do
invoke 'sneakers:cleanup' invoke 'sneakers:cleanup'
on roles fetch(:sneakers_roles) do |role| on roles fetch(:sneakers_roles) do |role|
switch_user(role) do SneakersHelper.switch_user(role) do
each_process_with_index do |pid_file, idx| SneakersHelper.each_process_with_index do |pid_file, idx|
unless pid_file_exists?(pid_file) unless SneakersHelper.pid_file_exists?(pid_file)
start_sneakers(pid_file, idx) SneakersHelper.start_sneakers(pid_file, idx)
end
end
end
end
end
def each_process_with_index(reverse = false, &block)
_pid_files = pid_files
_pid_files.reverse! if reverse
_pid_files.each_with_index do |pid_file, idx|
within release_path do
yield(pid_file, idx)
end
end
end end
def pid_files
sneakers_roles = Array(fetch(:sneakers_roles))
sneakers_roles.select! { |role| host.roles.include?(role) }
sneakers_roles.flat_map do |role|
processes = fetch(:sneakers_processes)
if processes == 1
fetch(:sneakers_pid)
else
Array.new(processes) { |idx| fetch(:sneakers_pid).gsub(/\.pid$/, "-#{idx}.pid") }
end
end
end
def pid_file_exists?(pid_file)
test(*("[ -f #{pid_file} ]").split(' '))
end
def process_exists?(pid_file)
test(*("kill -0 $( cat #{pid_file} )").split(' '))
end
def quiet_sneakers(pid_file)
if fetch(:sneakers_use_signals) || fetch(:sneakers_run_config)
execute :kill, "-USR1 `cat #{pid_file}`"
else
begin
execute :bundle, :exec, :sneakersctl, 'quiet', "#{pid_file}"
rescue SSHKit::Command::Failed
# If gems are not installed eq(first deploy) and sneakers_default_hooks as active
warn 'sneakersctl not found (ignore if this is the first deploy)'
end end
end end
end end
def stop_sneakers(pid_file)
if fetch(:sneakers_run_config) == true
execute :kill, "-SIGTERM `cat #{pid_file}`"
else
if fetch(:stop_sneakers_in_background, fetch(:sneakers_run_in_background))
if fetch(:sneakers_use_signals)
background :kill, "-TERM `cat #{pid_file}`"
else
background :bundle, :exec, :sneakersctl, 'stop', "#{pid_file}", fetch(:sneakers_timeout)
end
else
execute :bundle, :exec, :sneakersctl, 'stop', "#{pid_file}", fetch(:sneakers_timeout)
end
end
end
def start_sneakers(pid_file, idx = 0)
if fetch(:sneakers_run_config) == true
# Use sneakers configuration prebuilt in
raise "[ set :workers, ['worker1', 'workerN'] ] not configured properly, please configure the workers you wish to use" if fetch(:sneakers_workers).nil? or fetch(:sneakers_workers) == false or !fetch(:sneakers_workers).kind_of? Array
workers = fetch(:sneakers_workers).compact.join(',')
info "Starting the sneakers processes"
with rails_env: fetch(:sneakers_env), workers: workers do
rake 'sneakers:run'
end
end
end
def switch_user(role, &block)
user = sneakers_user(role)
if user == role.user
block.call
else
as user do
block.call
end
end
end
def sneakers_user(role)
properties = role.properties
properties.fetch(:sneakers_user) || fetch(:sneakers_user) || properties.fetch(:run_as) || role.user
end end
end end
module SneakersHelper
def self.each_process_with_index(reverse = false, &block)
_pid_files = pid_files
_pid_files.reverse! if reverse
_pid_files.each_with_index do |pid_file, idx|
within release_path do
yield(pid_file, idx)
end
end
end
def self.pid_files
sneakers_roles = Array(fetch(:sneakers_roles))
sneakers_roles.select! { |role| host.roles.include?(role) }
sneakers_roles.flat_map do |role|
processes = fetch(:sneakers_processes)
if processes == 1
fetch(:sneakers_pid)
else
Array.new(processes) { |idx| fetch(:sneakers_pid).gsub(/\.pid$/, "-#{idx}.pid") }
end
end
end
def self.pid_file_exists?(pid_file)
test(*("[ -f #{pid_file} ]").split(' '))
end
def self.process_exists?(pid_file)
test(*("kill -0 $( cat #{pid_file} )").split(' '))
end
def self.quiet_sneakers(pid_file)
if fetch(:sneakers_use_signals) || fetch(:sneakers_run_config)
execute :kill, "-USR1 `cat #{pid_file}`"
else
begin
execute :bundle, :exec, :sneakersctl, 'quiet', "#{pid_file}"
rescue SSHKit::Command::Failed
# If gems are not installed eq(first deploy) and sneakers_default_hooks as active
warn 'sneakersctl not found (ignore if this is the first deploy)'
end
end
end
def self.stop_sneakers(pid_file)
if fetch(:sneakers_run_config) == true
execute :kill, "-SIGTERM `cat #{pid_file}`"
else
if fetch(:stop_sneakers_in_background, fetch(:sneakers_run_in_background))
if fetch(:sneakers_use_signals)
background :kill, "-TERM `cat #{pid_file}`"
else
background :bundle, :exec, :sneakersctl, 'stop', "#{pid_file}", fetch(:sneakers_timeout)
end
else
execute :bundle, :exec, :sneakersctl, 'stop', "#{pid_file}", fetch(:sneakers_timeout)
end
end
end
def self.start_sneakers(pid_file, idx = 0)
if fetch(:sneakers_run_config) == true
# Use sneakers configuration prebuilt in
raise "[ set :workers, ['worker1', 'workerN'] ] not configured properly, please configure the workers you wish to use" if fetch(:sneakers_workers).nil? or fetch(:sneakers_workers) == false or !fetch(:sneakers_workers).kind_of? Array
workers = fetch(:sneakers_workers).compact.join(',')
info "Starting the sneakers processes"
with rails_env: fetch(:sneakers_env), workers: workers do
rake 'sneakers:run'
end
end
end
def self.switch_user(role, &block)
user = sneakers_user(role)
if user == role.user
block.call
else
as user do
block.call
end
end
end
def self.sneakers_user(role)
properties = role.properties
properties.fetch(:sneakers_user) || fetch(:sneakers_user) || properties.fetch(:run_as) || role.user
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment