Module: SimpleMessageQueue

Defined in:
lib/simple_message_queue.rb,
lib/simple_message_queue/errors.rb,
lib/simple_message_queue/version.rb,
lib/simple_message_queue/notification.rb,
lib/simple_message_queue/configuration.rb

Defined Under Namespace

Classes: Configuration, ConfigurationError, EnvironmentError, NotImplementedError, Notification

Constant Summary collapse

VERSION =
"0.0.9"

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.configurationObject

Class Methods only available to SimpleMessageQueue



10
11
12
# File 'lib/simple_message_queue.rb', line 10

def configuration
  @configuration
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

Yields:



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

def configure
  self.configuration ||= Configuration.new
  yield(configuration)

  if self.configuration.sns_notifications
    topics = ['send_message_failure']
    topics.each do |topic|
      SimpleMessageQueue::Notification::Topic.new(topic)
    end
  end

end

Instance Method Details

#countObject



46
47
48
# File 'lib/simple_message_queue.rb', line 46

def count
  queue.approximate_number_of_messages
end

#delete_queueObject



50
51
52
# File 'lib/simple_message_queue.rb', line 50

def delete_queue
  queue.delete
end

#exists?Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
# File 'lib/simple_message_queue.rb', line 54

def exists?
  # Although there is a queue.exists? method, that is only relevant if you already have the queue stored in a variable and then delete it
  # Trying to look it up by name will either return the queue object or throw an error, hence the rescue
  true if sqs.queues.named(queue_name)
rescue
  false
end

#loggerObject



75
76
77
78
79
80
81
# File 'lib/simple_message_queue.rb', line 75

def logger
  if SimpleMessageQueue.configuration.logger
    @logger = SimpleMessageQueue.configuration.logger
  else
    @logger ||= Logger.new(STDOUT)
  end
end

#process_message(message) ⇒ Object



94
95
96
# File 'lib/simple_message_queue.rb', line 94

def process_message(message)
  raise SimpleMessageQueue::NotImplementedError.new(name)
end

#queueObject



42
43
44
# File 'lib/simple_message_queue.rb', line 42

def queue
  @queue ||= sqs.queues.create(queue_name)
end

#queue_nameObject



33
34
35
36
37
38
39
40
# File 'lib/simple_message_queue.rb', line 33

def queue_name
  raise SimpleMessageQueue::EnvironmentError unless environment_defined?
  if @queue_name
    @queue_name + "_#{SimpleMessageQueue.configuration.environment}"
  else
    name.underscore.gsub('/', '_') + "_#{SimpleMessageQueue.configuration.environment}"
  end
end

#receiveObject



83
84
85
86
87
88
89
90
91
92
# File 'lib/simple_message_queue.rb', line 83

def receive
  @count = 0
  queue.poll(:idle_timeout => SimpleMessageQueue.configuration.idle_timeout, :wait_time_seconds => SimpleMessageQueue.configuration.wait_time_seconds) do |message|
    logger.info "Message received for #{queue_name}" if SimpleMessageQueue.configuration.debug
    logger.info "Message body: #{message.body}" if SimpleMessageQueue.configuration.debug
    @count += 1
    process_message(message)
  end
  @count
end

#send(message, options = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/simple_message_queue.rb', line 62

def send(message, options={})
  begin
    queue.send_message(message, options)
  rescue => error
    logger.error "There was an error when sending an item to #{queue_name} at #{DateTime.now}. Error: #{error.to_s}"

    if defined?(SimpleMessageQueue.configuration.sns_notifications) && SimpleMessageQueue.configuration.sns_notifications == true
      topic = SimpleMessageQueue::Notification::Topic.new('send_message_failure')
      topic.send("There was an error when sending an item to #{queue_name} at #{DateTime.now}. Error: #{error.to_s}", "SimpleMessageQueue: Send Message Failure")
    end
  end
end

#sqsObject



27
28
29
30
31
# File 'lib/simple_message_queue.rb', line 27

def sqs
  raise SimpleMessageQueue::ConfigurationError unless SimpleMessageQueue.configuration
  raise SimpleMessageQueue::EnvironmentError unless environment_defined?
  @@sqs ||= AWS::SQS.new(:access_key_id => SimpleMessageQueue.configuration.access_key_id, :secret_access_key => SimpleMessageQueue.configuration.secret_access_key)
end