diff --git a/lib/capistrano/tasks/sidekiq.rake b/lib/capistrano/tasks/sidekiq.rake index b975e31..4833e32 100644 --- a/lib/capistrano/tasks/sidekiq.rake +++ b/lib/capistrano/tasks/sidekiq.rake @@ -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) @@ -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) @@ -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 @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/lib/generators/capistrano/sidekiq/monit/templates/sidekiq_monit.conf.erb b/lib/generators/capistrano/sidekiq/monit/templates/sidekiq_monit.conf.erb index 3f923e1..67e2c35 100644 --- a/lib/generators/capistrano/sidekiq/monit/templates/sidekiq_monit.conf.erb +++ b/lib/generators/capistrano/sidekiq/monit/templates/sidekiq_monit.conf.erb @@ -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_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 %>