Skip to content

Commit

Permalink
Merge pull request seuros#97 from mcb/mcb/add-sidekiq-user
Browse files Browse the repository at this point in the history
intial support for sidekiq_user
  • Loading branch information
seuros committed Aug 19, 2015
2 parents 3f200fd + e2908e4 commit e03c69b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 23 deletions.
11 changes: 8 additions & 3 deletions lib/capistrano/tasks/capistrano2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,17 @@ def for_each_role
end
end

def run_as(cmd)
su_user = fetch(:sidekiq_user)
run cdm, roles: sidekiq_role, shell: "su - #{su_user}"
end

def quiet_process(pid_file, idx, sidekiq_role)
run "if [ -d #{current_path} ] && [ -f #{pid_file} ] && kill -0 `cat #{pid_file}`> /dev/null 2>&1; then cd #{current_path} && #{fetch(:sidekiqctl_cmd)} quiet #{pid_file} ; else echo 'Sidekiq is not running'; fi", roles: sidekiq_role
run_as "if [ -d #{current_path} ] && [ -f #{pid_file} ] && kill -0 `cat #{pid_file}`> /dev/null 2>&1; then cd #{current_path} && #{fetch(:sidekiqctl_cmd)} quiet #{pid_file} ; else echo 'Sidekiq is not running'; fi"
end

def stop_process(pid_file, idx, sidekiq_role)
run "if [ -d #{current_path} ] && [ -f #{pid_file} ] && kill -0 `cat #{pid_file}`> /dev/null 2>&1; then cd #{current_path} && #{fetch(:sidekiqctl_cmd)} stop #{pid_file} #{fetch :sidekiq_timeout} ; else echo 'Sidekiq is not running'; fi", roles: sidekiq_role
run_as "if [ -d #{current_path} ] && [ -f #{pid_file} ] && kill -0 `cat #{pid_file}`> /dev/null 2>&1; then cd #{current_path} && #{fetch(:sidekiqctl_cmd)} stop #{pid_file} #{fetch :sidekiq_timeout} ; else echo 'Sidekiq is not running'; fi"
end

def start_process(pid_file, idx, sidekiq_role)
Expand Down Expand Up @@ -90,7 +95,7 @@ def start_process(pid_file, idx, sidekiq_role)
args.push '--daemon'
end

run "if [ -d #{current_path} ] && [ ! -f #{pid_file} ] || ! kill -0 `cat #{pid_file}` > /dev/null 2>&1; then cd #{current_path} ; #{fetch(:sidekiq_cmd)} #{args.compact.join(' ')} ; else echo 'Sidekiq is already running'; fi", pty: false, roles: sidekiq_role
run_as "if [ -d #{current_path} ] && [ ! -f #{pid_file} ] || ! kill -0 `cat #{pid_file}` > /dev/null 2>&1; then cd #{current_path} ; #{fetch(:sidekiq_cmd)} #{args.compact.join(' ')} ; else echo 'Sidekiq is already running'; fi", pty: false
end

desc 'Quiet sidekiq (stop accepting new work)'
Expand Down
64 changes: 44 additions & 20 deletions lib/capistrano/tasks/sidekiq.cap
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace :load do
task :defaults do
set :sidekiq_user, nil
set :sidekiq_default_hooks, -> { true }

set :sidekiq_pid, -> { File.join(shared_path, 'tmp', 'pids', 'sidekiq.pid') }
Expand Down Expand Up @@ -129,10 +130,12 @@ namespace :sidekiq do
desc 'Quiet sidekiq (stop processing new tasks)'
task :quiet do
on roles fetch(:sidekiq_role) do
if test("[ -d #{release_path} ]") # fixes #11
for_each_process(true) do |pid_file, idx|
if pid_process_exists?(pid_file)
quiet_sidekiq(pid_file)
switch_user do
if test("[ -d #{release_path} ]") # fixes #11
for_each_process(true) do |pid_file, idx|
if pid_process_exists?(pid_file)
quiet_sidekiq(pid_file)
end
end
end
end
Expand All @@ -142,10 +145,12 @@ namespace :sidekiq do
desc 'Stop sidekiq'
task :stop do
on roles fetch(:sidekiq_role) do
if test("[ -d #{release_path} ]")
for_each_process(true) do |pid_file, idx|
if pid_process_exists?(pid_file)
stop_sidekiq(pid_file)
switch_user do
if test("[ -d #{release_path} ]")
for_each_process(true) do |pid_file, idx|
if pid_process_exists?(pid_file)
stop_sidekiq(pid_file)
end
end
end
end
Expand All @@ -155,8 +160,10 @@ namespace :sidekiq do
desc 'Start sidekiq'
task :start do
on roles fetch(:sidekiq_role) do
for_each_process do |pid_file, idx|
start_sidekiq(pid_file, idx) unless pid_process_exists?(pid_file)
switch_user do
for_each_process do |pid_file, idx|
start_sidekiq(pid_file, idx) unless pid_process_exists?(pid_file)
end
end
end
end
Expand All @@ -170,21 +177,25 @@ namespace :sidekiq do
desc 'Rolling-restart sidekiq'
task :rolling_restart do
on roles fetch(:sidekiq_role) do
for_each_process(true) do |pid_file, idx|
if pid_process_exists?(pid_file)
stop_sidekiq(pid_file)
switch_user do
for_each_process(true) do |pid_file, idx|
if pid_process_exists?(pid_file)
stop_sidekiq(pid_file)
end
start_sidekiq(pid_file, idx)
end
start_sidekiq(pid_file, idx)
end
end
end

# Delete any pid file not in use
task :cleanup do
on roles fetch(:sidekiq_role) do
for_each_process do |pid_file, idx|
if pid_file_exists?(pid_file)
execute "rm #{pid_file}" unless pid_process_exists?(pid_file)
switch_user do
for_each_process do |pid_file, idx|
if pid_file_exists?(pid_file)
execute "rm #{pid_file}" unless pid_process_exists?(pid_file)
end
end
end
end
Expand All @@ -195,14 +206,27 @@ namespace :sidekiq do
task :respawn do
invoke 'sidekiq:cleanup'
on roles fetch(:sidekiq_role) do
for_each_process do |pid_file, idx|
unless pid_file_exists?(pid_file)
start_sidekiq(pid_file, idx)
switch_user do
for_each_process do |pid_file, idx|
unless pid_file_exists?(pid_file)
start_sidekiq(pid_file, idx)
end
end
end
end
end

def switch_user(&block)
su_user = fetch(:puma_user)
if su_user
as su_user do
yield
end
end

yield
end

def upload_sidekiq_template(from, to, role)
template = sidekiq_template(from, role)
upload!(StringIO.new(ERB.new(template).result(binding)), to)
Expand Down

0 comments on commit e03c69b

Please sign in to comment.