Class: BirdGrinder::QueueProcessor
- Inherits:
-
Object
- Object
- BirdGrinder::QueueProcessor
- Defined in:
- lib/bird_grinder/queue_processor.rb
Overview
When running, the queue processor makes it possible to use a redis queue queue up and dispatch tweets and direct messages from external processes. This is useful since it makes it easy to have 1 outgoing source of tweets, triggered from any external application. Included in examples/bird_grinder_client.rb is a simple example client which uses redis to queue tweets and dms.
Instance Attribute Summary collapse
-
#tweeter ⇒ Object
Returns the value of attribute tweeter.
Class Method Summary collapse
-
.start ⇒ Object
Starts the queue processor with an initial check.
Instance Method Summary collapse
-
#check_queue ⇒ Object
Attempts to pop and process an item from the front of the queue.
-
#handle_action(action, args) ⇒ Object
Calls the correct method on the tweeter if present and in the whitelist.
-
#initialize ⇒ QueueProcessor
constructor
Initializes redis and our tweeter.
-
#process_action(res) ⇒ Object
Processes a given action action - calling handle action if present.
-
#schedule_check(time = nil) ⇒ Object
Check the queue.
Constructor Details
#initialize ⇒ QueueProcessor
Initializes redis and our tweeter.
24 25 26 27 |
# File 'lib/bird_grinder/queue_processor.rb', line 24 def initialize @tweeter = Tweeter.new(self) @redis = EM::P::Redis.connect end |
Instance Attribute Details
#tweeter ⇒ Object
Returns the value of attribute tweeter.
21 22 23 |
# File 'lib/bird_grinder/queue_processor.rb', line 21 def tweeter @tweeter end |
Class Method Details
.start ⇒ Object
Starts the queue processor with an initial check. raises an exception if the reactor isn’t running.
80 81 82 83 |
# File 'lib/bird_grinder/queue_processor.rb', line 80 def self.start raise "EventMachine must be running" unless EM.reactor_running? new.check_queue end |
Instance Method Details
#check_queue ⇒ Object
Attempts to pop and process an item from the front of the queue. Also, it will queue up the next check - if current item was empty, it will happen after a specified delay otherwise it will check now.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/bird_grinder/queue_processor.rb', line 32 def check_queue logger.debug "Checking Redis for outgoing messages" @redis.lpop(@@namespace) do |res| if res.blank? logger.debug "Empty queue, scheduling check in #{@@polling_delay} seconds" schedule_check(@@polling_delay) else logger.debug "Got item, processing and scheduling next check" begin process_action Yajl::Parser.parse(res) rescue Yajl::ParseError => e logger.error "Couldn't parse json: #{e.message}" end schedule_check end end end |
#handle_action(action, args) ⇒ Object
Calls the correct method on the tweeter if present and in the whitelist. logs and caught argument errors.
71 72 73 74 75 76 |
# File 'lib/bird_grinder/queue_processor.rb', line 71 def handle_action(action, args) args ||= [] @tweeter.send(action, *[*args]) if @@action_whitelist.include?(action) rescue ArgumentError logger.warn "Incorrect call for #{action} with arguuments #{args}" end |
#process_action(res) ⇒ Object
Processes a given action action - calling handle action if present.
63 64 65 66 67 |
# File 'lib/bird_grinder/queue_processor.rb', line 63 def process_action(res) if res.is_a?(Hash) && res["action"].present? handle_action(res["action"], res["arguments"]) end end |
#schedule_check(time = nil) ⇒ Object
Check the queue.
53 54 55 56 57 58 59 |
# File 'lib/bird_grinder/queue_processor.rb', line 53 def schedule_check(time = nil) if time == nil check_queue else EventMachine.add_timer(@@polling_delay) { check_queue } end end |