Class: SqsBunny

Inherits:
Object
  • Object
show all
Defined in:
lib/sqs_bunny.rb

Overview

Main execution class, just call SqsBunny.run and pass in the path of your configuration file

Defined Under Namespace

Classes: NullLogger

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ SqsBunny

Returns a new instance of SqsBunny.



20
21
22
23
# File 'lib/sqs_bunny.rb', line 20

def initialize config
  @config = {'wait_time_seconds'=>10}.merge config
  @poll_count = 0
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



9
10
11
# File 'lib/sqs_bunny.rb', line 9

def logger
  @logger
end

Class Method Details

.run(config_file_path, logger = nil) ⇒ Object

Run this method to start the job, optionally pass in a configuration hash. Otherwise the job will look for a JSON configuration at config/setup.json



13
14
15
16
17
18
# File 'lib/sqs_bunny.rb', line 13

def self.run config_file_path, logger=nil
  raise "Configuration file could not be found at #{config_file_path}" unless File.exists? config_file_path
  s = self.new(JSON.parse(IO.read(config_file_path)))
  s.logger = logger
  s.go
end

Instance Method Details

#goObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sqs_bunny.rb', line 25

def go
  while true
    @poll_count += 1
    client = Aws::SQS::Client.new
    parent_url = @config['parent_q']['queue_url']
    log.debug "Polling for messages at #{parent_url}"
    resp = client.receive_message(queue_url:parent_url,wait_time_seconds:@config['wait_time_seconds'])
    if resp.respond_to? :messages
      resp.messages.each do |x|
        log.info x.body
        send_message client, x
        client.delete_message(queue_url:parent_url,receipt_handle:x.receipt_handle)
      end
    else 
      log.debug "No messages"
    end
    break unless keep_polling?
  end
end

#keep_polling?Boolean

This method can be overidden to provide your own polling stop semantics

Returns:

  • (Boolean)


53
54
55
# File 'lib/sqs_bunny.rb', line 53

def keep_polling?
  @config['poll_max'].nil? || @poll_count < @config['poll_max']
end

#send_message(client, message) ⇒ Object



45
46
47
48
49
50
# File 'lib/sqs_bunny.rb', line 45

def send_message client, message
  return unless @config['children']
  @config['children'].each do |child|
    client.send_message(queue_url:child['queue_url'],message_body:message.body)
  end
end