lib/capistrano/tasks/sidekiq.rake in capistrano-sidekiq-0.5.4 vs lib/capistrano/tasks/sidekiq.rake in capistrano-sidekiq-0.10.0

- old
+ new

@@ -126,12 +126,12 @@ after 'deploy:published', 'sidekiq:start' end 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) quiet_sidekiq(pid_file) end @@ -141,27 +141,28 @@ end end 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) stop_sidekiq(pid_file) end end end end end + Rake::Task["sidekiq:stop"].reenable end 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 end end @@ -173,12 +174,12 @@ invoke 'sidekiq:start' end 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) end start_sidekiq(pid_file, idx) @@ -187,12 +188,12 @@ end end # 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) end end @@ -202,29 +203,37 @@ # TODO : Don't start if all processes are off, raise warning. 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) end end end 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) template = sidekiq_template(from, role) upload!(StringIO.new(ERB.new(template).result(binding)), to)