Skip to content

Instantly share code, notes, and snippets.

/logger.rb Secret

Created June 1, 2016 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/58a8c3eaf3cd790cb6aff3b5af54b2a0 to your computer and use it in GitHub Desktop.
Save anonymous/58a8c3eaf3cd790cb6aff3b5af54b2a0 to your computer and use it in GitHub Desktop.
module Wurker
# define a logger method as a module instance method
# so that it is available when we mixin this module
def logger
Wurker::Utility.logger
end # logger
# define the actual module logger instance method
# which uses or creates the @logger
def self.logger
@logger ||= begin
logger = ::Logger.new(STDOUT)
logger.level = Logger::WARN
logger
end
end # self.logger
end # Wurker
module Wurker
class Worker
def initialize(&block)
# any initial configuration for the base
# Worker class can go here
# use instance_eval to apply any custom configurations to the
# worker on initializeation
instance_eval(&block) if block_given?
end # initialize
# accept and store the block that this Worker will perform
# on every queue event
def handle(&block)
raise Wurker::TypeError, "Block must be given to #handle method" unless block_given?
@handle = block
end # handle
# start the worker by listening on the queue with a blocking #deq call
# the queue is passed to this method to decouple the worker and the queue
# as the queue itself is handled by the Wurker::Core dispatcher
def run(queue)
raise Wurker::TypeError, "Queue must be passed to Wurker::Worker#run" unless queue.is_a? Queue
logger.debug "[o] - Running with #{queue}"
while obj = queue.deq
logger.debug "[o] - #{self} got work item on queue #{queue}"
logger.debug "[o] - #{obj.ai}"
# call the registered handle block
instance_exec obj, &@handle
end # while obj = queue.deq
end # run
end # Worker
end # Wurker
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment