class Opener::Daemons::Daemon

The Daemon class communicates with an AWS SQS queue and delegates work to the mapper and worker classes.

@!attribute [r] component

@return [Class]

@!attribute [r] component_options

@return [Hash]

Attributes

component[R]
component_options[R]

Public Class Methods

new(component, options = {}) click to toggle source

@param [Class] component The component to run in the worker. @param [Hash] options Extra options to pass to the component.

Calls superclass method
# File lib/opener/daemons/daemon.rb, line 31
def initialize(component, options = {})
  @component         = component
  @component_options = options

  super() # keep parenthesis, parent method doesn't take arguments.
end

Public Instance Methods

before_start() click to toggle source

Called before the daemon is started.

# File lib/opener/daemons/daemon.rb, line 41
def before_start
  Core::Syslog.open(
    ENV['APP_NAME'],
    ::Syslog::LOG_CONS | ::Syslog::LOG_PID
  )

  Core::Syslog.info(
    'Starting daemon',
    :queue   => option(:queue_name),
    :threads => threads
  )

  GC::Profiler.enable

  Daemons.configure_rollbar

  NewRelic::Agent.manual_start if Daemons.newrelic?

  Aws.eager_autoload!(:services => %w{S3 SQS})
end
complete(message, output) click to toggle source

@param [AWS::SQS::ReceivedMessage] message @param [Mixed] output @param [Benchmark::Tms] timings

# File lib/opener/daemons/daemon.rb, line 90
def complete(message, output)
  log_msg = "Finished message #{message.message_id}"

  Core::Syslog.info(log_msg)

ensure
  Transaction.reset_current
end
create_mapper() click to toggle source

Overwrites the original method so that we can inject the component into the mapper.

@see [Oni::Daemon#create_mapper]

# File lib/opener/daemons/daemon.rb, line 68
def create_mapper
  unless option(:mapper)
    raise ArgumentError, 'No mapper has been set in the `:mapper` option'
  end

  return option(:mapper).new(component, component_options)
end
error(error) click to toggle source

Called when an error occurs.

@param [StandardError] error

# File lib/opener/daemons/daemon.rb, line 81
def error(error)
  report_exception(error)
end
report_exception(error) click to toggle source

Sends an error to Rollbar.

@param [StandardError] error

# File lib/opener/daemons/daemon.rb, line 104
def report_exception(error)
  if Daemons.rollbar?
    Rollbar.error(
      error,
      :active_threads   => Thread.list.count,
      :ruby_description => RUBY_DESCRIPTION,
      :parameters       => Transaction.current.parameters
    )
  else
    raise error
  end
ensure
  Transaction.reset_current
end