breakers 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a99dbaa5b10dc377c242f0707ca01e138e5119b5
4
- data.tar.gz: f65d7cc872f4d76c2902362baab53c0a4825b73a
3
+ metadata.gz: 6eab277573c135bff307496a5ff60036e8e2d7e6
4
+ data.tar.gz: c1dfd133b1a41f3d9d6f10aede8cdce0265c43e2
5
5
  SHA512:
6
- metadata.gz: 7a6fcda690f8eb2757cc3a82e5240c7b5d3cd3bec48a1e4f9c9e3bb32f2b7ae5c08b8b12c2215750187aac5623860f7e6b2aa0548c0b64fd9736d17ed1643aa2
7
- data.tar.gz: 78e50c255536dcc2dfe3cb460197b4e1ac836cb7caa684341ac66e002b8f01927921eea3fb1011cc17f1ef6cc555b8126a26e047c35e25dcf45571fef96ceee9
6
+ metadata.gz: 869a21c2cd1296d8e98ae7f13dd80352f31af945491e8afc6f2d694c8d057873879c05933551035b2f071902b2a08b7164d2107b7a671507ce1831ec9dc9e098
7
+ data.tar.gz: 093725a48e5e68c0172cd5cfcfe9e1eb8ddff596501a26a055d27f322a1cba6db77c42296a55e7e309cbcc348414645fec630ea0d975e076c850f20d07047942
data/README.md CHANGED
@@ -154,6 +154,15 @@ service.end_forced_outage!
154
154
  Unlike with outages detected by the middleware, forced outages are not periodically tested to see if they have completed and must be
155
155
  manually ended with a call to `end_forced_outage!`.
156
156
 
157
+ ### Changing the Outage Response
158
+
159
+ By default, if you make a request against a service that is experiencing an outage a Breakers::OutageException will be raised. If you would
160
+ prefer to receive a response with a certain status code instead, you can change that with:
161
+
162
+ ```ruby
163
+ Breakers.outage_response = { type: :status_code, status_code: 503 }
164
+ ```
165
+
157
166
  ### Redis Data Structure
158
167
 
159
168
  Data is stored in Redis with the following structure:
@@ -1,5 +1,6 @@
1
1
  require 'breakers/client'
2
2
  require 'breakers/outage'
3
+ require 'breakers/outage_exception'
3
4
  require 'breakers/service'
4
5
  require 'breakers/uptime_middleware'
5
6
  require 'breakers/version'
@@ -40,4 +41,21 @@ module Breakers
40
41
  def self.redis_prefix
41
42
  @redis_prefix || ''
42
43
  end
44
+
45
+ # Configure the middleware's handling of outages. The default is to raise a Breakers::OutageException
46
+ # but you can also request that the response comes back with a configurable status code.
47
+ #
48
+ # @param [Hash] opts A hash of options
49
+ # @option opts [Symbol] :type Pass :exception to raise a Breakers::OutageException when an error occurs. Pass :status_code to respond.
50
+ # @option opts [Integer] :status_code If the type is :status_code, configure which code to return.
51
+ def self.outage_response=(opts)
52
+ @outage_response = { type: :exception }.merge(opts)
53
+ end
54
+
55
+ # Query for the outage response configuration
56
+ #
57
+ # @return [Hash] configuration for the outage response, as defined in outage_response=
58
+ def self.outage_response
59
+ @outage_response || { type: :exception }
60
+ end
43
61
  end
@@ -0,0 +1,17 @@
1
+ module Breakers
2
+ # The error that is raised when a request is made against a service that is
3
+ # experiencing an outage
4
+ class OutageException < StandardError
5
+ attr_reader :outage
6
+ attr_reader :service
7
+
8
+ def initialize(outage, service)
9
+ @outage = outage
10
+ @service = service
11
+ end
12
+
13
+ def message
14
+ "Outage detected on #{@service.name} beginning at #{@outage.start_time.to_i}"
15
+ end
16
+ end
17
+ end
@@ -31,12 +31,16 @@ module Breakers
31
31
  protected
32
32
 
33
33
  def outage_response(outage:, service:)
34
- Faraday::Response.new.tap do |response|
35
- response.finish(
36
- status: 503,
37
- body: "Outage detected on #{service.name} beginning at #{outage.start_time.to_i}",
38
- response_headers: {}
39
- )
34
+ if Breakers.outage_response[:type] == :status_code
35
+ Faraday::Response.new.tap do |response|
36
+ response.finish(
37
+ status: Breakers.outage_response[:status_code],
38
+ body: "Outage detected on #{service.name} beginning at #{outage.start_time.to_i}",
39
+ response_headers: {}
40
+ )
41
+ end
42
+ else
43
+ raise Breakers::OutageException.new(outage, service)
40
44
  end
41
45
  end
42
46
 
@@ -60,13 +64,15 @@ module Breakers
60
64
  end
61
65
  end
62
66
  rescue => e
63
- handle_error(
64
- service: service,
65
- request_env: request_env,
66
- response_env: nil,
67
- error: "#{e.class.name} - #{e.message}",
68
- current_outage: current_outage
69
- )
67
+ unless e.is_a?(Breakers::OutageException)
68
+ handle_error(
69
+ service: service,
70
+ request_env: request_env,
71
+ response_env: nil,
72
+ error: "#{e.class.name} - #{e.message}",
73
+ current_outage: current_outage
74
+ )
75
+ end
70
76
  raise
71
77
  end
72
78
 
@@ -1,3 +1,3 @@
1
1
  module Breakers
2
- VERSION = '0.1.1'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: breakers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aubrey Holland
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-10-18 00:00:00.000000000 Z
11
+ date: 2016-10-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -192,6 +192,7 @@ files:
192
192
  - lib/breakers.rb
193
193
  - lib/breakers/client.rb
194
194
  - lib/breakers/outage.rb
195
+ - lib/breakers/outage_exception.rb
195
196
  - lib/breakers/service.rb
196
197
  - lib/breakers/uptime_middleware.rb
197
198
  - lib/breakers/version.rb