Class: Rack::Downtime

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/downtime.rb,
lib/rack/downtime/utils.rb,
lib/rack/downtime/version.rb,
lib/rack/downtime/strategy.rb

Overview

For the full documentation see README.md

Defined Under Namespace

Modules: Strategy, Utils

Constant Summary collapse

DOWNTIME_DISABLE =
"RACK_DOWNTIME_DISABLE".freeze
DOWNTIME_INSERT =
"RACK_DOWNTIME_INSERT".freeze
ENV_KEY =
"rack.downtime".freeze
DEFAULT_INSERT_AT =
"html body".freeze
CONTENT_TYPE =

Newer versions of Rack should have these

"Content-Type".freeze
CONTENT_LENGTH =
"Content-Length".freeze
VERSION =
"0.0.1".freeze

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Downtime

Create an instance of the Rack middleware to manage downtime notifications

Arguments

options (Hash)

downtime detection and insertion options; optional

Options

:strategy (Symbol|Hash)

Set the downtime detection strategy; defaults to Rack::Downtime::strategy. If a Hash its first value is passed as an argument to the strategy class.

:insert (String)

Path to an ERB template to insert when downtime is planned. Downtimes are given to the template as start_time and end_time.

:insert_at (String)

Where to insert the ERB template given by :insert. Can be given as a CSS selector or an XPath location.

Errors

ArgumentError

If the strategy is unknown



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rack/downtime.rb', line 51

def initialize(app, options = {})
  @app = app

  @strategy = options[:strategy] || self.class.strategy
  @strategy = load_strategy(@strategy) unless @strategy.respond_to?(:call)

  @insert = options[:insert]
  @insert = load_template(@insert) if @insert

  @insert_at = options[:insert_at] || DEFAULT_INSERT_AT
end

Class Attribute Details

.strategyObject

Set the default downtime strategy



29
30
31
# File 'lib/rack/downtime.rb', line 29

def strategy
  @strategy ||= :file
end

Instance Method Details

#call(env) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rack/downtime.rb', line 63

def call(env)
  return @app.call(env) if env[DOWNTIME_DISABLE] == "1"

  downtime = get_downtime(env)
  env[ENV_KEY] = downtime if downtime

  response = @app.call(env)
  return response unless downtime && insert_downtime?(env, response)

  old_body = response[2]
  new_body = insert_downtime(old_body, downtime)

  old_body.close if old_body.respond_to?(:close)
  response[1][CONTENT_LENGTH] = Rack::Utils.bytesize(new_body).to_s
  response[2] = [new_body]

  response
end