Class: Awful::SQS

Inherits:
Cli show all
Defined in:
lib/awful/sqs.rb

Instance Method Summary collapse

Methods inherited from Cli

#initialize, #ll, #version

Constructor Details

This class inherits a constructor from Awful::Cli

Instance Method Details

#create(name) ⇒ Object



49
50
51
# File 'lib/awful/sqs.rb', line 49

def create(name)
  sqs.create_queue(queue_name: name).queue_url.tap(&method(:puts))
end

#delete(name) ⇒ Object



54
55
56
57
58
# File 'lib/awful/sqs.rb', line 54

def delete(name)
  if yes? "Really delete queue #{name}", :yellow
    sqs.delete_queue(queue_url: queue_url(name))
  end
end

#dump(*names) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/awful/sqs.rb', line 61

def dump(*names)
  names.map do |name|
    sqs.get_queue_attributes(queue_url: queue_url(name), attribute_names: %w[All]).attributes
  end.tap do |queues|
    queues.each do |queue|
      puts YAML.dump(stringify_keys(queue))
    end
  end
end

#ls(*names) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/awful/sqs.rb', line 30

def ls(*names)
  if names.empty?
    queues = sqs.list_queues(queue_name_prefix: options[:prefix]).queue_urls
  else
    queues = names.map(&method(:queue_url))
  end
  attr = %w[QueueArn ApproximateNumberOfMessages ApproximateNumberOfMessagesNotVisible LastModifiedTimestamp]
  if options[:long]
    queues.map {|q| sqs.get_queue_attributes(queue_url: q, attribute_names: attr).attributes}.output do |list|
      print_table list.map { |q|
        [q['QueueArn'].split(':').last, q['ApproximateNumberOfMessages'], q['ApproximateNumberOfMessagesNotVisible'], Time.at(q['LastModifiedTimestamp'].to_i)]
      }
    end
  else
    queues.map { |q| q.split('/').last }.output(&method(:puts))
  end
end

#purge(name) ⇒ Object



99
100
101
# File 'lib/awful/sqs.rb', line 99

def purge(name)
  sqs.purge_queue(queue_url: queue_url(name))
end

#receive(name) ⇒ Object



92
93
94
95
96
# File 'lib/awful/sqs.rb', line 92

def receive(name)
  sqs.receive_message(queue_url: queue_url(name), max_number_of_messages: options[:number]).messages.tap do |messages|
    puts messages.map(&:body)
  end
end

#send(name, message) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/awful/sqs.rb', line 74

def send(name, message)
  args = only_keys_matching(options, %w[message_group_id message_deduplication_id]).merge(
    queue_url:    queue_url(name),
    message_body: message,
  )
  sqs.send_message(args)
end

#send_batch(name, *messages) ⇒ Object



83
84
85
86
87
88
# File 'lib/awful/sqs.rb', line 83

def send_batch(name, *messages)
  entries = messages.map.with_index do |m, i|
    {id: i.to_s, message_body: m}
  end
  sqs.send_message_batch(queue_url: queue_url(name), entries: entries)
end