Module: Chore::Queues::SQS

Defined in:
lib/chore/queues/sqs.rb,
lib/chore/queues/sqs/consumer.rb,
lib/chore/queues/sqs/publisher.rb

Defined Under Namespace

Classes: Consumer, Publisher

Class Method Summary collapse

Class Method Details

.create_queues!(halt_on_existing = false) ⇒ Array<String>

Helper method to create queues based on the currently known list as provided by your configured Chore::Jobs This is meant to be invoked from a rake task, and not directly. These queues will be created with the default settings, which may not be ideal. This is meant only as a convenience helper for testing, and not as a way to create production quality queues in SQS

Parameters:

  • halt_on_existing (TrueClass, FalseClass) (defaults to: false)

    Raise an exception if the queue already exists

Returns:

  • (Array<String>)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/chore/queues/sqs.rb', line 16

def self.create_queues!(halt_on_existing=false)
  raise 'You must have atleast one Chore Job configured and loaded before attempting to create queues' unless Chore.prefixed_queue_names.length > 0

  if halt_on_existing
    existing = self.existing_queues
    if existing.size > 0
      raise <<-ERROR.gsub(/^\s+/, '')
      We found queues that already exist! Verify your queue names or prefix are setup correctly.

      The following queue names were found:
      #{existing.join("\n")}
      ERROR
    end
  end

  Chore.prefixed_queue_names.each do |queue_name|
    Chore.logger.info "Chore Creating Queue: #{queue_name}"
    begin
      sqs_client.create_queue(queue_name: queue_name)
    rescue Aws::SQS::Errors::QueueAlreadyExists
      Chore.logger.info "exists with different config"
    end
  end

  Chore.prefixed_queue_names
end

.delete_queues!Array<String>

Helper method to delete all known queues based on the list as provided by your configured Chore::Jobs This is meant to be invoked from a rake task, and not directly.

Returns:

  • (Array<String>)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/chore/queues/sqs.rb', line 48

def self.delete_queues!
  raise 'You must have atleast one Chore Job configured and loaded before attempting to create queues' unless Chore.prefixed_queue_names.length > 0

  Chore.prefixed_queue_names.each do |queue_name|
    begin
      Chore.logger.info "Chore Deleting Queue: #{queue_name}"
      url = sqs_client.get_queue_url(queue_name: queue_name).queue_url
      sqs_client.delete_queue(queue_url: url)
    rescue => e
      # This could fail for a few reasons - log out why it failed, then continue on
      Chore.logger.error "Deleting Queue: #{queue_name} failed because #{e}"
    end
  end

  Chore.prefixed_queue_names
end

.existing_queuesArray<String>

Collect a list of queues that already exist

Returns:

  • (Array<String>)


68
69
70
71
72
73
74
75
76
77
78
# File 'lib/chore/queues/sqs.rb', line 68

def self.existing_queues
  Chore.prefixed_queue_names.select do |queue_name|
    # If the NonExistentQueue exception is raised we do not care about that queue name.
    begin
      sqs_client.get_queue_url(queue_name: queue_name)
      true
    rescue Aws::SQS::Errors::NonExistentQueue
      false
    end
  end
end

.sqs_clientObject



4
5
6
# File 'lib/chore/queues/sqs.rb', line 4

def self.sqs_client
  Aws::SQS::Client.new(logger: Chore.logger, log_level: Chore.log_level_to_sym)
end