diff --git a/.github/workflows/rspec.yml b/.github/workflows/rspec.yml index c8580a1a..e0a11c09 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/Gemfile.lock b/Gemfile.lock index 822a51c4..9f170674 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - event_source (0.5.7) + event_source (0.6.0) addressable (>= 2.8.0) bunny (>= 2.14) deep_merge (~> 1.2.0) @@ -323,7 +323,7 @@ GEM ruby2_keywords (0.0.4) rufus-scheduler (3.6.0) fugit (~> 1.1, >= 1.1.6) - set (1.0.2) + set (1.0.3) sinatra (2.1.0) mustermann (~> 1.0) rack (~> 2.2) diff --git a/lib/event_source/configure.rb b/lib/event_source/configure.rb index e529e23b..387fde69 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 5c4423ce..42980564 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 00000000..d5ae0c84 --- /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 01f75efa..ee359c86 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 2b431e55..840a362c 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 diff --git a/lib/event_source/version.rb b/lib/event_source/version.rb index a2989776..02db54a8 100644 --- a/lib/event_source/version.rb +++ b/lib/event_source/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module EventSource - VERSION = "0.5.7" + VERSION = "0.6.0" end