Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

intial support for sidekiq_user #97

Merged
merged 1 commit into from
Aug 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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