diff --git a/.github/workflows/rspec.yml b/.github/workflows/rspec.yml index c8580a1..e0a11c0 100644 --- a/.github/workflows/rspec.yml +++ b/.github/workflows/rspec.yml @@ -35,4 +35,4 @@ jobs: # bundle exec rubocop - name: Run rspec run: | - bundle exec rspec + ES_HOSTING_MODE=listener bundle exec rspec diff --git a/lib/event_source/configure.rb b/lib/event_source/configure.rb index e529e23..387fde6 100644 --- a/lib/event_source/configure.rb +++ b/lib/event_source/configure.rb @@ -4,6 +4,7 @@ require_relative "configure/servers" require_relative "configure/contracts" require_relative "configure/operations" +require_relative "configure/mode" require_relative "configure/config" module EventSource diff --git a/lib/event_source/configure/config.rb b/lib/event_source/configure/config.rb index 5c4423c..4298056 100644 --- a/lib/event_source/configure/config.rb +++ b/lib/event_source/configure/config.rb @@ -26,6 +26,10 @@ def server_key=(value) @server_key = value&.to_sym end + def mode + @mode ||= ::EventSource::Configure::Mode.parse(ENV['ES_HOSTING_MODE']) + end + attr_writer :async_api_schemas def servers diff --git a/lib/event_source/configure/mode.rb b/lib/event_source/configure/mode.rb new file mode 100644 index 0000000..d5ae0c8 --- /dev/null +++ b/lib/event_source/configure/mode.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +module EventSource + module Configure + # Server boot mode. Used for performance and configuration settings. + class Mode + + attr_reader :value + + def initialize(val) + @value = val + end + + def listener? + @value == :listener + end + + def publisher? + @value == :publisher + end + + def self.publisher + self.new(:publisher) + end + + def self.parse(mode_string) + return publisher if mode_string.blank? + mode_sym = mode_string.to_sym + raise ::EventSource::Error::InvalidModeError, "\"#{mode_string}\" is an invalid mode. Must be empty, null, \"publisher\", or \"listener\"." if ![:publisher, :listener].include?(mode_sym) + self.new(mode_string.to_sym) + end + end + end +end diff --git a/lib/event_source/error.rb b/lib/event_source/error.rb index 01f75ef..ee359c8 100644 --- a/lib/event_source/error.rb +++ b/lib/event_source/error.rb @@ -21,6 +21,7 @@ class Error < StandardError ConstantNotDefined = Class.new(Error) ContractNotFound = Class.new(Error) EventNameUndefined = Class.new(Error) + InvalidModeError = Class.new(Error) FileAccessError = Class.new(Error) InvalidChannelsResourceError = Class.new(Error) PublisherAlreadyRegisteredError = Class.new(Error) diff --git a/lib/event_source/protocols/amqp/bunny_queue_proxy.rb b/lib/event_source/protocols/amqp/bunny_queue_proxy.rb index 2b431e5..840a362 100644 --- a/lib/event_source/protocols/amqp/bunny_queue_proxy.rb +++ b/lib/event_source/protocols/amqp/bunny_queue_proxy.rb @@ -73,6 +73,12 @@ def subscribe(subscriber_klass, bindings) @channel_proxy.subject.prefetch(prefetch) + # Do not spawn consumers in the 'publisher' mode + unless ::EventSource.config.mode.listener? + logger.debug "In publisher mode, not booting subscription" + return + end + if options[:block] spawn_thread(options) { add_consumer(subscriber_klass, options) } else