DEV: Allow custom value when pausing sidekiq to aid in debugging.

DEV: Allow custom value when pausing sidekiq to aid in debugging.

Sometimes, it is useful to know what caused Sidekiq to be paused.

diff --git a/lib/active_record/connection_adapters/postgresql_fallback_adapter.rb b/lib/active_record/connection_adapters/postgresql_fallback_adapter.rb
index d161253..f76d921 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 4810647..8007a35 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 dc0e0f9..1b4302f 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

GitHub sha: bf21ebae

1 Like