Class: Qurd::Message
- Inherits:
-
Object
- Object
- Qurd::Message
- Extended by:
- Qurd::Mixins::Configuration
- Defined in:
- lib/qurd/message.rb
Overview
Convert an SQS auto scaling message to a more usable object
Instance Attribute Summary collapse
- #aws_credentials ⇒ Aws::Credentials readonly
-
#context ⇒ Cabin::Context
readonly
Cabin::Channel logs messages as well as context.
-
#exceptions ⇒ Array<Exception>
readonly
Action exceptions.
-
#name ⇒ String
readonly
The Listener name.
-
#queue_url ⇒ String
readonly
The SQS url the message came from.
-
#region ⇒ Object
readonly
Returns the value of attribute region.
Class Method Summary collapse
-
.add_accessor(name) ⇒ Object
Add setter and getter instances methods.
Instance Method Summary collapse
-
#action ⇒ String
Convert the
message.Eventto an action. -
#body ⇒ Hashie::Mash
Convert the SQS message
bodyto a mash, keys includeType,MessageId,TopicArn,Subject,Message,Timestamp,SignatureVersion,Signature,SigningCertURL,UnsubscribeURL. -
#delete ⇒ Object
Delete an AWS SQS message.
-
#failed!(e = nil) ⇒ Object
Record an action failure.
-
#failed? ⇒ Boolean
Has processing the message failed.
-
#initialize(attrs) ⇒ Message
constructor
A new instance of Message.
- #inspect ⇒ Object
-
#instance(tries = nil) ⇒ Struct|nil
Memozied EC2 instance.
-
#instance_id ⇒ String
The SQS message’s
EC2InstanceId. -
#instance_name ⇒ Object
Memoize the instance’s
Nametag. -
#message ⇒ Hashie::Mash
Convert
body.Messageto a mash, keys includeStatusCode,Service,AutoScalingGroupName,Description,ActivityId,Event,DetailsAutoScalingGroupARNProgressTimeAccountIdRequestId,StatusMessageEndTimeEC2InstanceIdStartTimeCause. -
#message_id ⇒ String
The
body.MessageId. -
#receipt_handle ⇒ String
The SQS
receipt_handle, used to delete a message.
Methods included from Qurd::Mixins::Configuration
qurd_config, qurd_configuration, qurd_logger, qurd_logger!
Methods included from Qurd::Mixins::AwsClients
Constructor Details
#initialize(attrs) ⇒ Message
Returns a new instance of Message.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/qurd/message.rb', line 65 def initialize(attrs) @aws_credentials = attrs[:aws_credentials] @name = attrs[:name] @queue_url = attrs[:queue_url] @region = attrs[:region] @sqs_message = attrs[:message] @exceptions = [] @failed = false @context = qurd_config.get_context(name: @name, queue_name: (@queue_url[/[^\/]+$/] rescue nil), instance_id: instance_id, message_id: , action: action) qurd_logger.info "Received #{body.Subject} Cause #{.Cause} Event #{.Event}" end |
Instance Attribute Details
#aws_credentials ⇒ Aws::Credentials (readonly)
51 52 53 |
# File 'lib/qurd/message.rb', line 51 def aws_credentials @aws_credentials end |
#context ⇒ Cabin::Context (readonly)
Cabin::Channel logs messages as well as context. Context is retained until it is cleared.
51 52 53 54 55 56 |
# File 'lib/qurd/message.rb', line 51 attr_reader :aws_credentials, :context, :exceptions, :name, :queue_url, :region |
#exceptions ⇒ Array<Exception> (readonly)
Action exceptions
51 52 53 54 55 56 |
# File 'lib/qurd/message.rb', line 51 attr_reader :aws_credentials, :context, :exceptions, :name, :queue_url, :region |
#name ⇒ String (readonly)
The Listener name
51 52 53 54 55 56 |
# File 'lib/qurd/message.rb', line 51 attr_reader :aws_credentials, :context, :exceptions, :name, :queue_url, :region |
#queue_url ⇒ String (readonly)
The SQS url the message came from
51 52 53 54 55 56 |
# File 'lib/qurd/message.rb', line 51 attr_reader :aws_credentials, :context, :exceptions, :name, :queue_url, :region |
#region ⇒ Object (readonly)
Returns the value of attribute region.
51 52 53 54 55 56 |
# File 'lib/qurd/message.rb', line 51 attr_reader :aws_credentials, :context, :exceptions, :name, :queue_url, :region |
Class Method Details
.add_accessor(name) ⇒ Object
Add setter and getter instances methods. If the get or set method is already defined, an exception will be raised.
23 24 25 26 27 28 29 |
# File 'lib/qurd/message.rb', line 23 def self.add_accessor(name) if instance_methods.include?(name) || instance_methods.include?("#{name}=") qurd_logger.warn "Can not replace a method! (#{name})" end attr_accessor name end |
Instance Method Details
#action ⇒ String
Convert the message.Event to an action
163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/qurd/message.rb', line 163 def action case .Event when 'autoscaling:EC2_INSTANCE_LAUNCH' then 'launch' when 'autoscaling:EC2_INSTANCE_LAUNCH_ERROR' then 'launch_error' when 'autoscaling:EC2_INSTANCE_TERMINATE' then 'terminate' when 'autoscaling:EC2_INSTANCE_TERMINATE_ERROR' then 'terminate_error' when 'autoscaling:TEST_NOTIFICATION' then 'test' else qurd_logger.info "Ignoring #{.Event}" failed! end end |
#body ⇒ Hashie::Mash
Convert the SQS message body to a mash, keys include Type, MessageId, TopicArn, Subject, Message, Timestamp, SignatureVersion, Signature, SigningCertURL, UnsubscribeURL
87 88 89 90 91 |
# File 'lib/qurd/message.rb', line 87 def body @body ||= Hashie::Mash.new JSON.load(@sqs_message.body) rescue JSON::ParserError @body = Hashie::Mash.new {} end |
#delete ⇒ Object
Delete an AWS SQS message
177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/qurd/message.rb', line 177 def delete qurd_logger.debug('Preparing to delete message') if failed? && qurd_configuration.save_failures qurd_logger.error 'Message failed processing, not deleting' elsif qurd_configuration.dry_run qurd_logger.info 'Dry run' else end context.clear end |
#failed!(e = nil) ⇒ Object
Record an action failure
125 126 127 128 129 130 |
# File 'lib/qurd/message.rb', line 125 def failed!(e = nil) qurd_logger.debug 'Failed' @exceptions << e if e @failed = true nil end |
#failed? ⇒ Boolean
Has processing the message failed
134 135 136 |
# File 'lib/qurd/message.rb', line 134 def failed? @failed == true end |
#inspect ⇒ Object
190 191 192 193 194 195 196 197 198 |
# File 'lib/qurd/message.rb', line 190 def inspect format('#<Qurd::Message message_id:%s subject:%s cause:%s ' \ 'instance_id:%s instance:%s>', , body.Subject, .Cause, instance_id, instance) end |
#instance(tries = nil) ⇒ Struct|nil
Memozied EC2 instance. Caller must anticipate nil results, as instances may terminate before the message is received.
142 143 144 145 |
# File 'lib/qurd/message.rb', line 142 def instance(tries = nil) return @instance if @instance @instance = aws_instance(tries) end |
#instance_id ⇒ String
The SQS message’s EC2InstanceId
107 108 109 |
# File 'lib/qurd/message.rb', line 107 def instance_id @instance_id ||= .EC2InstanceId end |
#instance_name ⇒ Object
Memoize the instance’s Name tag
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/qurd/message.rb', line 148 def instance_name return @instance_name if @instance_name @instance_name = instance..find do |t| t.key == 'Name' end.value qurd_logger.debug("Found instance name '#{@instance_name}'") @instance_name rescue NoMethodError qurd_logger.debug('No instance found') @instance_name = nil end |
#message ⇒ Hashie::Mash
Convert body.Message to a mash, keys include StatusCode, Service, AutoScalingGroupName, Description, ActivityId, Event, Details AutoScalingGroupARN Progress Time AccountId RequestId, StatusMessage EndTime EC2InstanceId StartTime Cause
99 100 101 102 103 |
# File 'lib/qurd/message.rb', line 99 def @message ||= Hashie::Mash.new JSON.load(body.Message) rescue JSON::ParserError @message = Hashie::Mash.new {} end |
#message_id ⇒ String
The body.MessageId
113 114 115 |
# File 'lib/qurd/message.rb', line 113 def @message_id ||= body.MessageId end |
#receipt_handle ⇒ String
The SQS receipt_handle, used to delete a message
119 120 121 |
# File 'lib/qurd/message.rb', line 119 def receipt_handle @sqs_message.receipt_handle end |