Class: ActiveElasticJob::Rack::SqsMessageConsumer

Inherits:
Object
  • Object
show all
Defined in:
lib/active_elastic_job/rack/sqs_message_consumer.rb

Overview

This middleware intercepts requests which are sent by the SQS daemon running in Amazon Elastic Beanstalk worker environments. It does this by looking at the User-Agent header. Furthermore, it verifies the digest which is sent along with a legit SQS message, and passed as an HTTP header in the resulting request. The digest is based on Rails’ secrets.secret_key_base. Therefore, the application running in the web environment, which generates the digest, and the application running in the worker environment, which verifies the digest, have to use the same secrets.secret_key_base setting.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ SqsMessageConsumer

:nodoc:



16
17
18
# File 'lib/active_elastic_job/rack/sqs_message_consumer.rb', line 16

def initialize(app) #:nodoc:
  @app = app
end

Instance Method Details

#call(env) ⇒ Object

:nodoc:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/active_elastic_job/rack/sqs_message_consumer.rb', line 20

def call(env) #:nodoc:
  request = ActionDispatch::Request.new env
  if request.headers['User-Agent'] =~ /aws-sqsd/
    begin
      verify(request)
      job = JSON.load(request.body)
      ActiveJob::Base.execute(job)
    rescue ActiveElasticJob::MessageVerifier::InvalidDigest => e
      return ['403', {'Content-Type' => env['text/plain'] }, ["incorrect digest"]]
    rescue StandardError => e
      return ['500', {'Content-Type' => env['text/plain'] }, [e.message]]
    end
    return ['200', {'Content-Type' => 'application/json' }, [ '' ]]
  end
  @app.call(env)
end