Class: ElasticBeans::Rack::Exec

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_beans/rack/exec.rb

Overview

A middleware that can convert specially-crafted paths into commands and enqueue them for execution using ElasticBeans::Application#enqueue_command. Load it in your Rails application so the “scheduler” environment enqueues commands for execution.

Only accepts localhost requests from aws-sqsd.

Constant Summary collapse

FAILURE =
['500', {'Content-type' => 'text/plain'}, [""]]
SUCCESS =
['200', {'Content-type' => 'text/plain'}, [""]]
USER_AGENT_PREFIX =
'aws-sqsd'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, args = {}) ⇒ Exec

Returns a new instance of Exec.



18
19
20
21
22
# File 'lib/elastic_beans/rack/exec.rb', line 18

def initialize(app, args={})
  @app = app
  @application = args.fetch(:application)
  @logger = args[:logger] || Logger.new("/dev/null")
end

Instance Method Details

#call(env) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/elastic_beans/rack/exec.rb', line 24

def call(env)
  request = ActionDispatch::Request.new(env)
  if enabled? && cron_request?(request)
    command = command_from_request(request)
    logger.info("[elastic_beans] Executing command: `#{command}'")
    begin
      application.enqueue_command(command)
    rescue => e
      logger.info("[elastic_beans] Enqueue failed: #{e.class.name}: #{e.message}\n#{e.backtrace.join("; ")}")
      return FAILURE
    end

    return SUCCESS
  end

  logger.debug { "[elastic_beans] skipped exec request: #{request.path}" }
  app.call(env)
end