module FastlyNsq

Constants

ConnectionFailed
LIFECYCLE_EVENTS
NotConnectedError
VERSION

Attributes

channel[RW]

@return [String] NSQ Channel

max_attempts[RW]

Maximum number of times an NSQ message will be attempted. When set to nil, the number of attempts will be unlimited. @return [Integer]

max_processing_pool_threads[W]

Maximum number of threads for FastlyNsq::PriorityThreadPool @return [Integer]

max_req_timeout[W]

Set Maximum requeue timeout in milliseconds @return [Integer]

preprocessor[RW]

@return [Proc] global preprocessor

Public Class Methods

configure() { |self| ... } click to toggle source

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
events() click to toggle source

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
fire_event(event) click to toggle source

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
listen(topic, processor, **options) click to toggle source

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
logger() click to toggle source

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
logger=(new_logger) click to toggle source

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
lookupd_http_addresses() click to toggle source

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
manager() click to toggle source

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
manager=(manager) click to toggle source

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
max_processing_pool_threads() click to toggle source

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
max_req_timeout() click to toggle source

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
on(event, &block) click to toggle source

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
tracer() click to toggle source

Instance of FastlyNsq::NewRelic

@return [FastlyNsq::NewRelic]

# File lib/fastly_nsq.rb, line 165
def tracer
  @tracer ||= FastlyNsq::NewRelic.new
end