class Osbourne::Message
Attributes
Public Class Methods
# File lib/osbourne/message.rb, line 11 def initialize(message) @message = message end
Public Instance Methods
Deletes the message from SQS to prevent retrying against another worker. Osbourne
will automatically delete a message sent to a worker as long as the Osourbne::WorkerBase#process method returns `true`
# File lib/osbourne/message.rb, line 54 def delete message.delete Osbourne.logger.info "[Osbourne] [MSG ID: #{id}] Cleared" end
Osbourne
has built-in message deduplication, but it's still a good idea to do some verification in a worker
@return [String] The UUID of the recieved message
# File lib/osbourne/message.rb, line 35 def id message.message_id end
@return [Boolean] This will be `true` if the SNS message is also JSON
# File lib/osbourne/message.rb, line 18 def json? return false unless valid? sns_body.is_a?(Hash) end
If the message was broadcast via SNS, the body will be available here.
@return [Hash] If the message was JSON @return [String] If the message was not JSON @return [nil] If the message was not broadcast via SNS. @see raw_body
raw_body
for the raw body string
# File lib/osbourne/message.rb, line 46 def message_body sns_body end
@return [String] The raw string representation of the message
# File lib/osbourne/message.rb, line 71 def raw_body message.body end
Just because a message was recieved via SQS, doesn't mean it was originally broadcast via SNS @return [Boolean] Was the message broadcast via SNS?
# File lib/osbourne/message.rb, line 78 def sns? json_body.is_a?(Hash) && (%w[Message Type TopicArn MessageId] - json_body.keys).empty? end
The SNS topic that this message was broadcast to @return [String] if the message was broadcast via SNS, this will be the topic @return [nil] if the message was not broadcast via SNS
# File lib/osbourne/message.rb, line 63 def topic return nil unless sns? json_body["TopicArn"].split(":").last end
Does the message match the checksum? If not, the message has likely been mangled in transit @return [Boolean]
# File lib/osbourne/message.rb, line 27 def valid? @valid ||= message.md5_of_body == Digest::MD5.hexdigest(message.body) end
Private Instance Methods
# File lib/osbourne/message.rb, line 96 def json_body @json_body || safe_json(message.body) end
# File lib/osbourne/message.rb, line 84 def safe_json(content) JSON.parse(content) rescue JSON::ParserError false end
# File lib/osbourne/message.rb, line 90 def sns_body return unless sns? @sns_body ||= safe_json(json_body["Message"]) || json_body["Message"] end