Class: Kindly::Queue

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

Instance Method Summary collapse

Constructor Details

#initializeQueue

Returns a new instance of Queue.



7
8
9
# File 'lib/kindly/queue.rb', line 7

def initialize
  @sqs = Aws::SQS::Client.new(region: 'us-west-2')
end

Instance Method Details

#add(job_name, job_id) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/kindly/queue.rb', line 11

def add(job_name, job_id)
  @sqs.send_message({
    queue_url: queue_url(job_name),
    message_body: "#{@job_name} has been requested.",
    message_attributes: {
      'JobId' => {
        string_value: job_id,
        data_type: "String",
      },
    },
  })
end

#pop(job_name) ⇒ Object



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

def pop(job_name)
  response = @sqs.receive_message({
    queue_url: queue_url(job_name),
    message_attribute_names: ['JobId'],
    max_number_of_messages: 1
  })
  raise too_many_messages(job_name) if response.messages.length > 1
  return nil if response.messages.length == 0

  message = response.messages[0]
  job_id = message.message_attributes['JobId'].string_value

  @sqs.delete_message({
    queue_url: queue_url(job_name),
    receipt_handle: message.receipt_handle
  })

  job_id
end