Class: Gongren::Rails::Worker
- Inherits:
-
Worker
- Object
- Worker
- Gongren::Rails::Worker
- Defined in:
- lib/gongren/rails/worker.rb
Instance Method Summary collapse
-
#active_record_model(ar) ⇒ Object
Given a two element Array of [“ClassName”, ID], returns an ActiveRecord instance.
- #start ⇒ Object
Instance Method Details
#active_record_model(ar) ⇒ Object
Given a two element Array of [“ClassName”, ID], returns an ActiveRecord instance.
65 66 67 |
# File 'lib/gongren/rails/worker.rb', line 65 def active_record_model(ar) ar.first.constantize.find(ar.last) end |
#start ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/gongren/rails/worker.rb', line 4 def start raise ArgumentError, "#start must be called with a block" unless block_given? logger.info { "Gongren::RailsWorker #{worker_id} ready to work" } AMQP.start do MQ.queue(control_queue_name, ).bind(control_exchange_name, ) do |header, data| = Marshal.load(data) logger.info { .inspect } if [:selector].to_s.strip.empty? then logger.error { "Received control request without :selector key: ignoring" } else begin send([:selector], ) rescue Exception => e log_failure(header, , e) end end end MQ.queue(queue_name, ).bind(exchange_name, ).subscribe(:ack => true) do |header, data| = Marshal.load(data) class << ; include Unit; end # Dynamically add our #ack method .gongren_header = header logger.info { .inspect } receiver = case keys = [:receiver].keys when [:active_record] active_record_model([:receiver][:active_record]) when [:class] [:receiver][:class].constantize else raise UnknownReceiverKeys, "Unable to map from #{keys} to a receiver instance: aborting" end .args.collect! do |arg| if arg.kind_of?(Hash) && arg[:active_record] then active_record_model(arg[:active_record]) else arg end end logger.debug { "Mapped message is:\n#{.inspect}" } begin receiver.send([:selector], *[:args]) # Automatically ack messages, but do it only once logger.debug { "Block ack'd? #{.acked?}" } unless .acked? logger.debug { "Ack'ing for the block" } .ack end rescue Exception => e log_failure(header, , e) end end end end |