Skip to content

Commit

Permalink
Merge pull request seuros#122 from jhollinger/monit-use-sidekiq_user
Browse files Browse the repository at this point in the history
Respect both local and global puma_user setting everywhere
  • Loading branch information
seuros committed Nov 10, 2015
2 parents 71a4278 + fba43f4 commit b515567
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
6 changes: 6 additions & 0 deletions lib/capistrano/tasks/monit.rake
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ namespace :sidekiq do
end
end

def sidekiq_require
if fetch(:sidekiq_require)
"--require #{fetch(:sidekiq_require)}"
end
end

def sidekiq_options_per_process
fetch(:sidekiq_options_per_process) || []
end
Expand Down
42 changes: 25 additions & 17 deletions lib/capistrano/tasks/sidekiq.rake
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ namespace :sidekiq do

desc 'Quiet sidekiq (stop processing new tasks)'
task :quiet do
on roles fetch(:sidekiq_role) do
switch_user do
on roles fetch(:sidekiq_role) do |role|
switch_user(role) do
if test("[ -d #{release_path} ]") # fixes #11
for_each_process(true) do |pid_file, idx|
if pid_process_exists?(pid_file)
Expand All @@ -143,8 +143,8 @@ namespace :sidekiq do

desc 'Stop sidekiq'
task :stop do
on roles fetch(:sidekiq_role) do
switch_user do
on roles fetch(:sidekiq_role) do |role|
switch_user(role) do
if test("[ -d #{release_path} ]")
for_each_process(true) do |pid_file, idx|
if pid_process_exists?(pid_file)
Expand All @@ -158,8 +158,8 @@ namespace :sidekiq do

desc 'Start sidekiq'
task :start do
on roles fetch(:sidekiq_role) do
switch_user do
on roles fetch(:sidekiq_role) do |role|
switch_user(role) do
for_each_process do |pid_file, idx|
start_sidekiq(pid_file, idx) unless pid_process_exists?(pid_file)
end
Expand All @@ -175,8 +175,8 @@ namespace :sidekiq do

desc 'Rolling-restart sidekiq'
task :rolling_restart do
on roles fetch(:sidekiq_role) do
switch_user do
on roles fetch(:sidekiq_role) do |role|
switch_user(role) do
for_each_process(true) do |pid_file, idx|
if pid_process_exists?(pid_file)
stop_sidekiq(pid_file)
Expand All @@ -189,8 +189,8 @@ namespace :sidekiq do

# Delete any pid file not in use
task :cleanup do
on roles fetch(:sidekiq_role) do
switch_user do
on roles fetch(:sidekiq_role) do |role|
switch_user(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)
Expand All @@ -204,8 +204,8 @@ namespace :sidekiq do
desc 'Respawn missing sidekiq processes'
task :respawn do
invoke 'sidekiq:cleanup'
on roles fetch(:sidekiq_role) do
switch_user do
on roles fetch(:sidekiq_role) do |role|
switch_user(role) do
for_each_process do |pid_file, idx|
unless pid_file_exists?(pid_file)
start_sidekiq(pid_file, idx)
Expand All @@ -215,15 +215,23 @@ namespace :sidekiq do
end
end

def switch_user(&block)
su_user = fetch(:sidekiq_user)
if su_user
def switch_user(role, &block)
su_user = sidekiq_user(role)
if su_user == role.user
block.call
else
as su_user do
yield
block.call
end
end
end

yield
def sidekiq_user(role)
properties = role.properties
properties.fetch(:sidekiq_user) || # local property for sidekiq only
fetch(:sidekiq_user) ||
properties.fetch(:run_as) || # global property across multiple capistrano gems
role.user
end

def upload_sidekiq_template(from, to, role)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<% processes_pids.each_with_index do |pid_file, idx| %>
check process <%= sidekiq_service_name(idx) %>
with pidfile "<%= pid_file %>"
start program = "/bin/su - <%= @role.user %> -c 'cd <%= current_path %> && <%= SSHKit.config.command_map[:sidekiq] %> <%= sidekiq_config %> --index <%= idx %> --pidfile <%= pid_file %> --environment <%= fetch(:sidekiq_env) %> <%= sidekiq_concurrency %> <%= sidekiq_logfile %> <%= sidekiq_queues %> <%= sidekiq_options_per_process[idx] %> -d'" with timeout 30 seconds
start program = "/bin/su - <%= sidekiq_user(@role) %> -c 'cd <%= current_path %> && <%= SSHKit.config.command_map[:sidekiq] %> <%= sidekiq_config %> --index <%= idx %> --pidfile <%= pid_file %> --environment <%= fetch(:sidekiq_env) %> <%= sidekiq_concurrency %> <%= sidekiq_logfile %> <%= sidekiq_require %> <%= sidekiq_queues %> <%= sidekiq_options_per_process[idx] %> -d'" with timeout 30 seconds

stop program = "/bin/su - <%= @role.user %> -c 'cd <%= current_path %> && <%= SSHKit.config.command_map[:sidekiqctl] %> stop <%= pid_file %>'" with timeout <%= fetch(:sidekiq_timeout).to_i + 10 %> seconds
stop program = "/bin/su - <%= sidekiq_user(@role) %> -c 'cd <%= current_path %> && <%= SSHKit.config.command_map[:sidekiqctl] %> stop <%= pid_file %>'" with timeout <%= fetch(:sidekiq_timeout).to_i + 10 %> seconds
group <%= fetch(:sidekiq_monit_group, fetch(:application)) %>-sidekiq

<% end %>

0 comments on commit b515567

Please sign in to comment.