Sha256: e695061768ebf8afa419278c9b2517d61665b28dab5f4aa243f0b22c2f1e80e7

Contents?: true

Size: 1.8 KB

Versions: 4

Compression:

Stored size: 1.8 KB

Contents

require "sneakers/version"
require 'thread/pool'
require 'bunny'
require 'logger'
require 'serverengine'

module Sneakers
  module Handlers
  end
  module Concerns
  end
end

require 'sneakers/configuration'
require 'sneakers/support/production_formatter'
require 'sneakers/concerns/logging'
require 'sneakers/concerns/metrics'
require 'sneakers/handlers/oneshot'
require 'sneakers/worker'
require 'sneakers/publisher'

module Sneakers
  extend self

  CONFIG = Configuration.new

  def configure(opts={})
    # worker > userland > defaults
    CONFIG.merge!(opts)

    setup_general_logger!
    setup_worker_concerns!
    setup_general_publisher!
    @configured = true
  end

  def clear!
    CONFIG.clear
    @logger = nil
    @publisher = nil
    @configured = false
  end

  def daemonize!(loglevel=Logger::INFO)
    CONFIG[:log] = 'sneakers.log'
    CONFIG[:daemonize] = true
    setup_general_logger!
    logger.level = loglevel
  end

  def logger=(logger)
    @logger = logger
  end

  def logger
    @logger
  end

  def publish(msg, routing)
    @publisher.publish(msg, routing)
  end

  def configured?
    @configured
  end

  def server=(server)
    @server = server
  end

  def server?
    @server
  end

  def configure_server
    yield self if server?
  end

  private

  def setup_general_logger!
    if [:info, :debug, :error, :warn].all?{ |meth| CONFIG[:log].respond_to?(meth) }
      @logger = CONFIG[:log]
    else
      @logger = ServerEngine::DaemonLogger.new(CONFIG[:log])
      @logger.formatter = Sneakers::Support::ProductionFormatter
    end
  end

  def setup_worker_concerns!
    Worker.configure_logger(Sneakers::logger)
    Worker.configure_metrics(CONFIG[:metrics])
    CONFIG[:handler] ||= Sneakers::Handlers::Oneshot
  end

  def setup_general_publisher!
    @publisher = Sneakers::Publisher.new
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sneakers-2.2.1 lib/sneakers.rb
sneakers-2.2.0 lib/sneakers.rb
sneakers-2.1.0 lib/sneakers.rb
sneakers-1.1.2 lib/sneakers.rb