Module: Minion
Defined Under Namespace
Classes: Handler
Instance Method Summary collapse
- #amqp_url ⇒ Object
- #amqp_url=(url) ⇒ Object
- #check_all ⇒ Object
- #decode_json(string) ⇒ Object
- #enqueue(jobs, data = {}) ⇒ Object
- #error(&blk) ⇒ Object
- #job(queue, options = {}, &blk) ⇒ Object
- #log(msg) ⇒ Object
- #logger(&blk) ⇒ Object
- #run ⇒ Object
- #url=(url) ⇒ Object
Instance Method Details
#amqp_url ⇒ Object
102 103 104 |
# File 'lib/minion.rb', line 102 def amqp_url @@amqp_url ||= ENV["AMQP_URL"] || "amqp://guest:guest@localhost/" end |
#amqp_url=(url) ⇒ Object
106 107 108 |
# File 'lib/minion.rb', line 106 def amqp_url=(url) @@amqp_url = url end |
#check_all ⇒ Object
84 85 86 |
# File 'lib/minion.rb', line 84 def check_all @@handlers.each { |h| h.check } end |
#decode_json(string) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/minion.rb', line 76 def decode_json(string) if defined? ActiveSupport::JSON ActiveSupport::JSON.decode string else JSON.load string end end |
#enqueue(jobs, data = {}) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/minion.rb', line 14 def enqueue(jobs, data = {}) raise "cannot enqueue a nil job" if jobs.nil? raise "cannot enqueue an empty job" if jobs.empty? ## jobs can be one or more jobs if jobs.respond_to? :shift queue = jobs.shift data["next_job"] = jobs unless jobs.empty? else queue = jobs end encoded = JSON.dump(data) log "send: #{queue}:#{encoded}" bunny.queue(queue, :durable => true, :auto_delete => false).publish(encoded) end |
#error(&blk) ⇒ Object
36 37 38 |
# File 'lib/minion.rb', line 36 def error(&blk) @@error_handler = blk end |
#job(queue, options = {}, &blk) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/minion.rb', line 44 def job(queue, = {}, &blk) handler = Minion::Handler.new queue handler.when = [:when] if [:when] handler.unsub = lambda do log "unsubscribing to #{queue}" MQ.queue(queue, :durable => true, :auto_delete => false).unsubscribe end handler.sub = lambda do log "subscribing to #{queue}" MQ.queue(queue, :durable => true, :auto_delete => false).subscribe(:ack => true) do |h,m| return if AMQP.closing? begin log "recv: #{queue}:#{m}" args = decode_json(m) result = yield(args) next_job(args, result) rescue Object => e raise unless error_handler error_handler.call(e,queue,m,h) end h.ack check_all end end @@handlers ||= [] at_exit { Minion.run } if @@handlers.size == 0 @@handlers << handler end |
#log(msg) ⇒ Object
31 32 33 34 |
# File 'lib/minion.rb', line 31 def log(msg) @@logger ||= proc { |m| puts "#{Time.now} :minion: #{m}" } @@logger.call(msg) end |
#logger(&blk) ⇒ Object
40 41 42 |
# File 'lib/minion.rb', line 40 def logger(&blk) @@logger = blk end |
#run ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/minion.rb', line 88 def run log "Starting minion" Signal.trap('INT') { AMQP.stop{ EM.stop } } Signal.trap('TERM'){ AMQP.stop{ EM.stop } } EM.run do AMQP.start(amqp_config) do MQ.prefetch(1) check_all end end end |
#url=(url) ⇒ Object
10 11 12 |
# File 'lib/minion.rb', line 10 def url=(url) @@config_url = url end |