Class: Aws::Rails::Middleware::ElasticBeanstalkSQSD

Inherits:
Object
  • Object
show all
Defined in:
lib/aws/rails/middleware/elastic_beanstalk_sqsd.rb,
lib/aws/rails/middleware/elastic_beanstalk_sqsd/configuration.rb

Overview

Middleware to handle requests from the SQS Daemon present on Elastic Beanstalk worker environments.

See Configuration for the available options and ElasticBeanstalkSQSD.configure for setting them in code.

Defined Under Namespace

Classes: Configuration, InvalidJobClassError

Constant Summary collapse

JOB_CLASS_NAME_PATTERN =

A job class name is an (optionally namespaced) constant path and nothing else. Names that cannot match this can never name a job class, so rejecting them keeps them out of the constant lookup.

/\A[A-Z]\w*(::[A-Z]\w*)*\z/.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ElasticBeanstalkSQSD

Returns a new instance of ElasticBeanstalkSQSD.



43
44
45
46
47
48
49
50
# File 'lib/aws/rails/middleware/elastic_beanstalk_sqsd.rb', line 43

def initialize(app)
  @app = app
  @logger = ::Rails.logger

  return unless ENV['AWS_PROCESS_BEANSTALK_WORKER_JOBS_ASYNC']

  @executor = init_executor
end

Class Method Details

.configConfiguration

Returns the current middleware configuration.

Returns:



38
39
40
# File 'lib/aws/rails/middleware/elastic_beanstalk_sqsd.rb', line 38

def config
  @config ||= Configuration.new
end

.configure {|config| ... } ⇒ Configuration

Yields the middleware configuration for customization.

Examples:

Restrict job dispatch to specific classes

Aws::Rails::Middleware::ElasticBeanstalkSQSD.configure do |config|
  config.job_class_allowlist = [SendReceiptJob, ProcessOrderJob]
end

Yield Parameters:

Returns:



32
33
34
35
# File 'lib/aws/rails/middleware/elastic_beanstalk_sqsd.rb', line 32

def configure
  yield(config) if block_given?
  config
end

Instance Method Details

#call(env) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/aws/rails/middleware/elastic_beanstalk_sqsd.rb', line 52

def call(env)
  request = ::ActionDispatch::Request.new(env)

  # Pass through unless user agent is the SQS Daemon
  return @app.call(env) unless from_sqs_daemon?(request)

  @logger.debug('aws-sdk-rails middleware detected call from Elastic Beanstalk SQS Daemon.')

  # Best-effort source check. On Docker-based EB platforms this is unreliable
  # because all traffic arrives via the docker bridge. Use job_class_allowlist
  # and network-level controls (security groups) as the primary safeguards.
  unless request.local? || sent_from_docker_host?(request)
    @logger.warn('SQSD request detected from untrusted address; returning 403 forbidden.')
    return forbidden_response
  end

  # Execute job or periodic task based on HTTP request context
  execute(request)
end

#shutdown(timeout = nil) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/aws/rails/middleware/elastic_beanstalk_sqsd.rb', line 72

def shutdown(timeout = nil)
  return unless @executor

  @logger.info("Shutting down SQS EBS background job executor. Timeout: #{timeout}")
  @executor.shutdown
  clean_shutdown = @executor.wait_for_termination(timeout)
  @logger.info("SQS EBS background executor shutdown complete. Clean: #{clean_shutdown}")
end