From bf21ebaeccd5c4b5d4fc2d33bb8b92b8ef754a86 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Tue, 19 Feb 2019 10:55:53 +0800 Subject: [PATCH] DEV: Allow custom value when pausing sidekiq to aid in debugging. Sometimes, it is useful to know what caused Sidekiq to be paused. --- .../postgresql_fallback_adapter.rb | 2 +- lib/sidekiq/pausable.rb | 13 ++++++++----- spec/multisite/pausable_spec.rb | 2 +- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/active_record/connection_adapters/postgresql_fallback_adapter.rb b/lib/active_record/connection_adapters/postgresql_fallback_adapter.rb index d161253ddf6..f76d921cb58 100644 --- a/lib/active_record/connection_adapters/postgresql_fallback_adapter.rb +++ b/lib/active_record/connection_adapters/postgresql_fallback_adapter.rb @@ -52,7 +52,7 @@ class PostgreSQLFallbackHandler def master_down synchronize do @masters_down[namespace] = true - Sidekiq.pause! if !Sidekiq.paused? + Sidekiq.pause!("pg_failover") if !Sidekiq.paused? MessageBus.publish(DATABASE_DOWN_CHANNEL, db: namespace, pid: Process.pid) end end diff --git a/lib/sidekiq/pausable.rb b/lib/sidekiq/pausable.rb index 48106478568..8007a354593 100644 --- a/lib/sidekiq/pausable.rb +++ b/lib/sidekiq/pausable.rb @@ -9,8 +9,8 @@ class SidekiqPauser @dbs ||= Set.new end - def pause! - $redis.setex PAUSED_KEY, TTL, "paused" + def pause!(value = "paused") + $redis.setex PAUSED_KEY, TTL, value @mutex.synchronize do extend_lease_thread @@ -36,9 +36,11 @@ class SidekiqPauser def paused_dbs dbs = [] + RailsMultisite::ConnectionManagement.each_connection do dbs << RailsMultisite::ConnectionManagement.current_db if paused? end + dbs end @@ -68,9 +70,9 @@ class SidekiqPauser def extend_lease_thread # should always be called from a mutex @dbs << RailsMultisite::ConnectionManagement.current_db + @extend_lease_thread ||= Thread.new do while true do - break if !@extend_lease_thread @dbs.each do |db| @@ -91,8 +93,9 @@ end module Sidekiq @pauser = SidekiqPauser.new - def self.pause! - @pauser.pause! + + def self.pause!(key = nil) + key ? @pauser.pause!(key) : @pauser.pause! end def self.paused? diff --git a/spec/multisite/pausable_spec.rb b/spec/multisite/pausable_spec.rb index dc0e0f9ee30..1b4302fc841 100644 --- a/spec/multisite/pausable_spec.rb +++ b/spec/multisite/pausable_spec.rb @@ -17,7 +17,7 @@ RSpec.describe "Pausing/Unpausing Sidekiq", type: :multisite do expect(Sidekiq.paused?).to eq(false) test_multisite_connection('second') do - Sidekiq.pause! + Sidekiq.pause!("test") expect(Sidekiq.paused?).to eq(true) end