Class: SQS::Job::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/sqs/job/handler.rb

Overview

One Handler instance is created per SQS message received, and is responsible for processing it by instantiating a Message::Base subclass and calling its #invoke! method. The “main” method is #run!.

Messages have the following structure: ‘create’, params: { … } Where params is optional (for example, keepalive messages might not have params).

The message class is loaded by requiring ‘vm2/message/#type’ constantizing it in the normal way, and passing the params hash to #new.

Instance Method Summary collapse

Constructor Details

#initialize(sqs_message) ⇒ Handler

Returns a new instance of Handler.



17
18
19
# File 'lib/sqs/job/handler.rb', line 17

def initialize sqs_message
  @sqs_message = sqs_message
end

Instance Method Details

#run!Object

Run this handler



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/sqs/job/handler.rb', line 22

def run!
  require 'base64'
  
  fingerprint = @sqs_message.message_attributes['key_fingerprint'][:string_value]
  signature   = Base64.strict_decode64(@sqs_message.message_attributes['signature'][:string_value])
    
  raise SignatureInvalidException unless SQS::Job.signature_valid? @sqs_message.body, fingerprint, signature
    
  msg = JSON.parse(@sqs_message.body)
  raise MissingTypeException unless (type = msg['type'])
    
  klass = message_class type
  SQS::Job.logger.info "Received message #{klass}"
  
  message = klass.new(msg['params'] || {})
  raise InvalidMessageException, message unless message.valid?
  message.invoke!
end