Class: MicroQ::SqsClient

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ SqsClient

Returns a new instance of SqsClient.



5
6
7
8
# File 'lib/micro_q/sqs_client.rb', line 5

def initialize(name)
  @name = "#{MicroQ.config.env}_#{name}"
  @url  = client.create_queue(:queue_name => @name)[:queue_url]
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



3
4
5
# File 'lib/micro_q/sqs_client.rb', line 3

def url
  @url
end

Instance Method Details

#messagesObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/micro_q/sqs_client.rb', line 10

def messages
  response = client.receive_message(
    :queue_url => url,
    :wait_time_seconds => 10,
    :max_number_of_messages => 10,
    :visibility_timeout => 5 * 60
  )

  ((response && response[:messages]) || []).collect do |message|
    JSON.parse(message[:body]).merge(
      'sqs_id' => message[:message_id],
      'sqs_handle' => message[:receipt_handle],
      'sqs_queue' => url
    )
  end
end

#messages_create(message) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/micro_q/sqs_client.rb', line 27

def messages_create(message)
  attrs = {
    :queue_url => url,
    :message_body => message.to_json
  }

  attrs[:delay_seconds] = (message['run_at'].to_i - Time.now.to_i) if message.key?('run_at')

  client.send_message(attrs)[:message_id]
end

#messages_delete(message) ⇒ Object



38
39
40
41
42
43
# File 'lib/micro_q/sqs_client.rb', line 38

def messages_delete(message)
  client.delete_message(
    :queue_url => message['sqs_queue'],
    :receipt_handle => message['sqs_handle']
  )
end