Skip to content

Commit

Permalink
Don't boot async subscribers in web mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
TreyE committed Oct 16, 2023
1 parent 47b1942 commit 1286c15
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/event_source/configure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/event_source/configure/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions lib/event_source/configure/mode.rb
Original file line number Diff line number Diff line change
@@ -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 normal?
@value == :normal
end

def web?
@value == :web
end

def self.normal
self.new(:normal)
end

def self.parse(mode_string)
return normal if mode_string.blank?
mode_sym = mode_string.to_sym
raise ::EventSource::Error::InvalidModeError, "\"#{mode_string}\" is an invalid mode. Must be empty, null, \"normal\", or \"web\"." if ![:web, :normal].include?(mode_sym)
self.new(mode_string.to_sym)
end
end
end
end
1 change: 1 addition & 0 deletions lib/event_source/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions lib/event_source/protocols/amqp/bunny_queue_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ def subscribe(subscriber_klass, bindings)

@channel_proxy.subject.prefetch(prefetch)

# Do not spawn consumers in the 'webserver' mode
if ::EventSource.config.mode.web?
logger.debug "In web mode, not booting subscription"
return
end

if options[:block]
spawn_thread(options) { add_consumer(subscriber_klass, options) }
else
Expand Down

0 comments on commit 1286c15

Please sign in to comment.