don't start message bus in rake tasks

don’t start message bus in rake tasks

  • turn off message bus by rails railtie when running rake
  • don’t initialize middleware subscribed when off
  • method to detect if message bus is off
From 1c4620e4c898872cb392db5d053238fb151064ea Mon Sep 17 00:00:00 2001
From: Michal Cichra <michal@cichra.cz>
Date: Wed, 23 Aug 2017 11:55:02 +0200
Subject: [PATCH] don't start message bus in rake tasks

* turn off message bus by rails railtie when running rake
* don't initialize middleware subscribed when off
* method to detect if message bus is off

diff --git a/lib/message_bus.rb b/lib/message_bus.rb
index d1e6501..aad741d 100644
--- a/lib/message_bus.rb
+++ b/lib/message_bus.rb
@@ -149,6 +149,11 @@ module MessageBus::Implementation
     @config[:long_polling_interval] || 25 * 1000
   end
 
+  # @return [Boolean] whether the bus is disabled or not
+  def off?
+    @off
+  end
+
   # Disables publication to the bus
   # @return [void]
   def off
diff --git a/lib/message_bus/rack/middleware.rb b/lib/message_bus/rack/middleware.rb
index 931be35..46af77f 100644
--- a/lib/message_bus/rack/middleware.rb
+++ b/lib/message_bus/rack/middleware.rb
@@ -25,7 +25,8 @@ class MessageBus::Rack::Middleware
   end
 
   # Sets up the middleware to receive subscriber client requests and begins
-  # listening for messages published on the bus for re-distribution
+  # listening for messages published on the bus for re-distribution (unless
+  # the bus is disabled).
   #
   # @param [Proc] app the rack app
   # @param [Hash] config
@@ -34,7 +35,7 @@ class MessageBus::Rack::Middleware
     @app = app
     @bus = config[:message_bus] || MessageBus
     @connection_manager = MessageBus::ConnectionManager.new(@bus)
-    start_listener
+    start_listener unless @bus.off?
   end
 
   # Stops listening for messages on the bus
diff --git a/lib/message_bus/rails/railtie.rb b/lib/message_bus/rails/railtie.rb
index f37dce3..cde09ca 100644
--- a/lib/message_bus/rails/railtie.rb
+++ b/lib/message_bus/rails/railtie.rb
@@ -24,6 +24,10 @@ class MessageBus::Rails::Railtie < ::Rails::Railtie
     MessageBus.logger = Rails.logger
   end
 
+  rake_tasks do
+    MessageBus.off
+  end
+
   def api_only?(config)
     return false unless config.respond_to?(:api_only)
 
diff --git a/spec/lib/message_bus/rack/middleware_spec.rb b/spec/lib/message_bus/rack/middleware_spec.rb
index 0ee9a3c..ef823f3 100644
--- a/spec/lib/message_bus/rack/middleware_spec.rb
+++ b/spec/lib/message_bus/rack/middleware_spec.rb
@@ -111,6 +111,19 @@ describe MessageBus::Rack::Middleware do
     include LongPolling
   end
 
+  describe "start listener" do
+    let(:app) { ->(_){ [200, {}, []] } }
+
+
+    it "never subscribes" do
+      bus = Minitest::Mock.new
+      bus.expect(:off?, true)
+
+      MessageBus::Rack::Middleware.new(app,message_bus: bus)
+    end
+
+  end
+
   describe "diagnostics" do
     it "should return a 403 if a user attempts to get at the _diagnostics path" do
       get "/message-bus/_diagnostics"
diff --git a/spec/lib/message_bus_spec.rb b/spec/lib/message_bus_spec.rb
index 2bddfcb..da14def 100644
--- a/spec/lib/message_bus_spec.rb
+++ b/spec/lib/message_bus_spec.rb
@@ -16,6 +16,12 @@ describe MessageBus do
     @bus.destroy
   end
 
+  it "can be turned off" do
+    @bus.off
+
+    @bus.off?.must_equal true
+  end
+
   it "can subscribe from a point in time" do
     @bus.publish("/minion", "banana")

GitHub