Class: Trusty::IronIo::QueueProcessor

Inherits:
Object
  • Object
show all
Includes:
Errors::ExceptionHandlers
Defined in:
lib/trusty/iron_io/queue_processor.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Errors::ExceptionHandlers

#notify_exception, #try_with_data

Constructor Details

#initialize(queue_name) ⇒ QueueProcessor

Returns a new instance of QueueProcessor.



16
17
18
19
# File 'lib/trusty/iron_io/queue_processor.rb', line 16

def initialize(queue_name)
  @queue_name = queue_name
  @client     = IronMQ::Client.new(:token => ENV['IRON_TOKEN'], :project_id => ENV['IRON_PROJECT_ID'])
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



14
15
16
# File 'lib/trusty/iron_io/queue_processor.rb', line 14

def client
  @client
end

#queue_nameObject (readonly)

Returns the value of attribute queue_name.



14
15
16
# File 'lib/trusty/iron_io/queue_processor.rb', line 14

def queue_name
  @queue_name
end

Class Method Details

.run(queue_name, *args, &block) ⇒ Object

helper method forwards to “run” instance method



10
11
12
# File 'lib/trusty/iron_io/queue_processor.rb', line 10

def self.run(queue_name, *args, &block)
  new(queue_name).run(*args, &block)
end

Instance Method Details

#default_optionsObject



21
22
23
# File 'lib/trusty/iron_io/queue_processor.rb', line 21

def default_options
  { :timeout => 30, :break_if_nil => true }
end

#queueObject



25
26
27
# File 'lib/trusty/iron_io/queue_processor.rb', line 25

def queue
  @queue ||= client.queue(queue_name)
end

#run(options = {}, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/trusty/iron_io/queue_processor.rb', line 29

def run(options = {}, &block)
  options = default_options.merge(options)
  
  queue.poll(options) do |message|#, :break_if_nil => true do |message|
    
    # parse body for better data formatting
    begin
      body = JSON.parse message.body
    rescue JSON::ParserError => ex
      body = message.body
    end
    
    try_with_data :message_id => message.id, :body => body do
      block.call(message, queue)
    end
  end
end

#webhook_urlObject



47
48
49
50
51
52
53
54
55
# File 'lib/trusty/iron_io/queue_processor.rb', line 47

def webhook_url
  url_template = 'https://mq-aws-us-east-1.iron.io/1/projects/%{project_id}/queues/%{queue_name}/messages/webhook?oauth=%{token}'
  
  url_template % {
    project_id: ENV['IRON_TOKEN'],
    token: ENV['IRON_PROJECT_ID'],
    queue_name: queue_name
  }
end