Class: Breakers::Service
- Inherits:
-
Object
- Object
- Breakers::Service
- Defined in:
- lib/breakers/service.rb
Overview
A service defines a backend system that your application relies upon. This class allows you to configure the outage detection for a service as well as to define which requests belong to it.
Constant Summary collapse
- DEFAULT_OPTS =
{ seconds_before_retry: 60, error_threshold: 50, data_retention_seconds: 60 * 60 * 24 * 30 }.freeze
Instance Method Summary collapse
-
#add_error ⇒ Object
Indicate that an error has occurred and potentially create an outage.
-
#add_success ⇒ Object
Indicate that a successful response has occurred.
-
#begin_forced_outage! ⇒ Object
Force an outage to begin on the service.
-
#end_forced_outage! ⇒ Object
End a forced outage on the service.
-
#errors_in_range(start_time:, end_time:, sample_minutes: 60) ⇒ Array[Hash]
Return data about the failed request counts in the time range.
-
#handles_request?(request_env:) ⇒ Boolean
Given a Faraday::Env, return true if this service handles the request, via its matcher.
-
#initialize(opts) ⇒ Service
constructor
Create a new service.
-
#latest_outage ⇒ Object
Return the most recent outage on the service.
-
#name ⇒ String
Get the name of the service.
-
#outages_in_range(start_time:, end_time:) ⇒ Array[Outage]
Return a list of all outages in the given time range.
-
#seconds_before_retry ⇒ Integer
Get the seconds before retry parameter.
-
#successes_in_range(start_time:, end_time:, sample_minutes: 60) ⇒ Array[Hash]
Return data about the successful request counts in the time range.
Constructor Details
#initialize(opts) ⇒ Service
Create a new service
20 21 22 |
# File 'lib/breakers/service.rb', line 20 def initialize(opts) @configuration = DEFAULT_OPTS.merge(opts) end |
Instance Method Details
#add_error ⇒ Object
Indicate that an error has occurred and potentially create an outage
47 48 49 50 |
# File 'lib/breakers/service.rb', line 47 def add_error increment_key(key: errors_key) maybe_create_outage end |
#add_success ⇒ Object
Indicate that a successful response has occurred
53 54 55 |
# File 'lib/breakers/service.rb', line 53 def add_success increment_key(key: successes_key) end |
#begin_forced_outage! ⇒ Object
Force an outage to begin on the service. Forced outages are not periodically retested.
58 59 60 |
# File 'lib/breakers/service.rb', line 58 def begin_forced_outage! Outage.create(service: self, forced: true) end |
#end_forced_outage! ⇒ Object
End a forced outage on the service.
63 64 65 66 67 68 |
# File 'lib/breakers/service.rb', line 63 def end_forced_outage! latest = Outage.find_latest(service: self) if latest.forced? latest.end! end end |
#errors_in_range(start_time:, end_time:, sample_minutes: 60) ⇒ Array[Hash]
Return data about the failed request counts in the time range
104 105 106 |
# File 'lib/breakers/service.rb', line 104 def errors_in_range(start_time:, end_time:, sample_minutes: 60) values_in_range(start_time: start_time, end_time: end_time, type: :errors, sample_minutes: sample_minutes) end |
#handles_request?(request_env:) ⇒ Boolean
Given a Faraday::Env, return true if this service handles the request, via its matcher
35 36 37 |
# File 'lib/breakers/service.rb', line 35 def handles_request?(request_env:) @configuration[:request_matcher].call(request_env) end |
#latest_outage ⇒ Object
Return the most recent outage on the service
71 72 73 |
# File 'lib/breakers/service.rb', line 71 def latest_outage Outage.find_latest(service: self) end |
#name ⇒ String
Get the name of the service
27 28 29 |
# File 'lib/breakers/service.rb', line 27 def name @configuration[:name] end |
#outages_in_range(start_time:, end_time:) ⇒ Array[Outage]
Return a list of all outages in the given time range
80 81 82 83 84 85 86 |
# File 'lib/breakers/service.rb', line 80 def outages_in_range(start_time:, end_time:) Outage.in_range( service: self, start_time: start_time, end_time: end_time ) end |
#seconds_before_retry ⇒ Integer
Get the seconds before retry parameter
42 43 44 |
# File 'lib/breakers/service.rb', line 42 def seconds_before_retry @configuration[:seconds_before_retry] end |
#successes_in_range(start_time:, end_time:, sample_minutes: 60) ⇒ Array[Hash]
Return data about the successful request counts in the time range
94 95 96 |
# File 'lib/breakers/service.rb', line 94 def successes_in_range(start_time:, end_time:, sample_minutes: 60) values_in_range(start_time: start_time, end_time: end_time, type: :successes, sample_minutes: sample_minutes) end |