From 3a41ec0625874e7eab4d730987a2af54fb440e96 Mon Sep 17 00:00:00 2001 From: Jordan Hollinger Date: Mon, 9 Nov 2015 19:23:56 -0500 Subject: [PATCH 1/2] Respect both local and global puma_user setting everywhere --- lib/capistrano/tasks/sidekiq.rake | 42 +++++++++++-------- .../monit/templates/sidekiq_monit.conf.erb | 4 +- 2 files changed, 27 insertions(+), 19 deletions(-) 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 %> From fba43f479f13b84cd9580bd680f00ccc52e616eb Mon Sep 17 00:00:00 2001 From: Jordan Hollinger Date: Tue, 10 Nov 2015 14:41:48 -0500 Subject: [PATCH 2/2] Respect sidekiq_require setting in monit config --- lib/capistrano/tasks/monit.rake | 6 ++++++ .../sidekiq/monit/templates/sidekiq_monit.conf.erb | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/capistrano/tasks/monit.rake b/lib/capistrano/tasks/monit.rake index dd2ad39..9987d68 100644 --- a/lib/capistrano/tasks/monit.rake +++ b/lib/capistrano/tasks/monit.rake @@ -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 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 67e2c35..03cdfd5 100644 --- a/lib/generators/capistrano/sidekiq/monit/templates/sidekiq_monit.conf.erb +++ b/lib/generators/capistrano/sidekiq/monit/templates/sidekiq_monit.conf.erb @@ -2,7 +2,7 @@ <% processes_pids.each_with_index do |pid_file, idx| %> check process <%= sidekiq_service_name(idx) %> with pidfile "<%= pid_file %>" - 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 + 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 - <%= 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