From 5f76e5062dd9bb04534c7464fed29d629bbc692c Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Mon, 16 Oct 2017 19:48:31 +0800 Subject: [PATCH] Pause Sidekiq when postgres failovers. --- .../connection_adapters/postgresql_fallback_adapter.rb | 6 +++++- .../connection_adapters/postgresql_fallback_adapter_spec.rb | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/active_record/connection_adapters/postgresql_fallback_adapter.rb b/lib/active_record/connection_adapters/postgresql_fallback_adapter.rb index 1e2e84f8c6a..f151830c5fa 100644 --- a/lib/active_record/connection_adapters/postgresql_fallback_adapter.rb +++ b/lib/active_record/connection_adapters/postgresql_fallback_adapter.rb @@ -32,7 +32,10 @@ class PostgreSQLFallbackHandler end def master_down=(args) - synchronize { @masters_down[namespace] = args } + synchronize do + @masters_down[namespace] = args + Sidekiq.pause! if args + end end def master_up(namespace) @@ -53,6 +56,7 @@ class PostgreSQLFallbackHandler self.master_up(key) Discourse.disable_readonly_mode(Discourse::PG_READONLY_MODE_KEY) + Sidekiq.unpause! end rescue => e logger.warn "#{log_prefix}: Connection to master PostgreSQL server failed with '#{e.message}'" diff --git a/spec/components/active_record/connection_adapters/postgresql_fallback_adapter_spec.rb b/spec/components/active_record/connection_adapters/postgresql_fallback_adapter_spec.rb index 924e44390e3..0013cf2be50 100644 --- a/spec/components/active_record/connection_adapters/postgresql_fallback_adapter_spec.rb +++ b/spec/components/active_record/connection_adapters/postgresql_fallback_adapter_spec.rb @@ -71,6 +71,7 @@ describe ActiveRecord::ConnectionHandling do .to change { Discourse.readonly_mode? }.from(false).to(true) expect(postgresql_fallback_handler.master_down?).to eq(true) + expect(Sidekiq.paused?).to eq(true) with_multisite_db(multisite_db) do expect(postgresql_fallback_handler.master_down?).to eq(nil) @@ -92,6 +93,7 @@ describe ActiveRecord::ConnectionHandling do expect(Discourse.readonly_mode?).to eq(false) expect(postgresql_fallback_handler.master_down?).to eq(nil) + expect(Sidekiq.paused?).to eq(false) expect(ActiveRecord::Base.connection_pool.connections.count).to eq(0) skip("Figuring out why the following keeps failing to obtain a connection on Travis")