module FastlyNsq
Constants
- ConnectionFailed
- LIFECYCLE_EVENTS
- NotConnectedError
- VERSION
Attributes
@return [String] NSQ Channel
Maximum number of times an NSQ message will be attempted. When set to nil
, the number of attempts will be unlimited. @return [Integer]
Maximum number of threads for FastlyNsq::PriorityThreadPool
@return [Integer]
Set Maximum requeue timeout in milliseconds @return [Integer]
@return [Proc] global preprocessor
Public Class Methods
Configuration for FastlyNsq
@example
FastlyNsq.configure do |config| config.channel = 'Z' config.logger = Logger.new end
@example
FastlyNsq.configure do |config| config.channel = 'fnsq' config.logger = Logger.new config.preprocessor = ->(_) { FastlyNsq.logger.info 'PREPROCESSESES' } lc.listen 'posts', ->(m) { puts "posts: #{m.body}" } lc.listen 'blogs', ->(m) { puts "blogs: #{m.body}" }, priority: 3 end
# File lib/fastly_nsq.rb, line 86 def configure yield self end
Map of lifecycle events @return [Hash]
# File lib/fastly_nsq.rb, line 40 def events @events ||= LIFECYCLE_EVENTS.each_with_object({}) { |e, a| a[e] = [] } end
Execute Procs assigned for the lifecycle event
@param event [Symbol] Lifecycle event to trigger
# File lib/fastly_nsq.rb, line 151 def fire_event(event) blocks = FastlyNsq.events.fetch(event) blocks.each do |block| begin block.call rescue => e logger.error "[#{event}] #{e.inspect}" end end end
Create a FastlyNsq::Listener
@param topic [String] NSQ topic on which to listen @param processor [Proc] processor that will be +call+ed per message @param options [Hash] additional options that are passed to FastlyNsq::Listener's constructor @return FastlyNsq::Listener
# File lib/fastly_nsq.rb, line 51 def listen(topic, processor, **options) FastlyNsq::Listener.new(topic: topic, processor: processor, **options) end
Return logger or set logger to default. @return [Logger]
# File lib/fastly_nsq.rb, line 58 def logger return @logger if @logger self.logger = Logger.new(STDERR) end
Set the logger and also set Nsq.logger @params logger [Logger]
# File lib/fastly_nsq.rb, line 67 def logger=(new_logger) @logger = Nsq.logger = new_logger end
Return an array of NSQ lookupd http addresses sourced from ENV @return [Array<String>] list of nsqlookupd http addresses
# File lib/fastly_nsq.rb, line 128 def lookupd_http_addresses ENV.fetch('NSQLOOKUPD_HTTP_ADDRESS').split(',').map(&:strip) end
Returns a new FastlyNsq::Manager
or the memoized instance +@manager+. @return [FastlyNsq::Manager]
# File lib/fastly_nsq.rb, line 94 def manager @manager ||= FastlyNsq::Manager.new end
Set a new manager and transfer listeners to the new manager. @param manager [FastlyNsq::Manager] @return [FastlyNsq::Manager]
# File lib/fastly_nsq.rb, line 102 def manager=(manager) @manager&.transfer(manager) @manager = manager end
Maximum number of threads for FastlyNsq::PriorityThreadPool
Default setting is 5 and can be set via ENV @return [Integer]
# File lib/fastly_nsq.rb, line 121 def max_processing_pool_threads @max_processing_pool_threads ||= ENV.fetch('MAX_PROCESSING_POOL_THREADS', 5).to_i end
Maximum requeue timeout in milliseconds. This setting controls the maximum value that will be sent from FastlyNsq::Message#requeue
This value should be less than or equal to the nsqd command line option max-req-timeout
. The default setting is 1 hour. @return [Integer] @see nsq.io/components/nsqd.html#command-line-options
# File lib/fastly_nsq.rb, line 114 def max_req_timeout @max_req_timeout ||= ENV.fetch('MAX_REQ_TIMEOUT', 60 * 60 * 1_000).to_i end
Register a block to run at a point in the lifecycle.
@example
FastlyNsq.configure do |config| config.on(:shutdown) do puts "Goodbye cruel world!" end end
@param event [Symbol] Event to hook into. One of :startup, :heartbeat or :shutdown. @yield Proc to execute when event is triggered.
# File lib/fastly_nsq.rb, line 142 def on(event, &block) event = event.to_sym raise ArgumentError, "Invalid event name: #{event}" unless LIFECYCLE_EVENTS.include?(event) events[event] << block end
Instance of FastlyNsq::NewRelic
@return [FastlyNsq::NewRelic]
# File lib/fastly_nsq.rb, line 165 def tracer @tracer ||= FastlyNsq::NewRelic.new end