Class: Breakers::Outage
- Inherits:
-
Object
- Object
- Breakers::Outage
- Defined in:
- lib/breakers/outage.rb
Overview
A class defining an outage on a service
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#service ⇒ Object
readonly
Returns the value of attribute service.
Class Method Summary collapse
-
.create(service:, forced: false) ⇒ Breakers::Outage
Create a new outage on the given service.
-
.find_latest(service:) ⇒ Breakers::Outage
Return the most recent outage on the given service.
-
.in_range(service:, start_time:, end_time:) ⇒ Breakers::Outage
Return all of the outages on the given service that begin in the time range.
-
.outages_key(service:) ⇒ String
Get the key for storing the outage data in Redis for this service.
Instance Method Summary collapse
-
#end! ⇒ Object
Tell the outage to end, which will allow requests to begin flowing again.
-
#end_time ⇒ Time
Get the time at which the outage ended.
-
#ended? ⇒ Boolean
Check to see if the outage has ended.
-
#forced? ⇒ Boolean
Was the outage forced?.
-
#initialize(service:, body:) ⇒ Breakers::Outage
constructor
Create a new outage.
-
#last_test_time ⇒ Time
Get the time at which the outage last received a new request.
-
#ready_for_retest?(wait_seconds:) ⇒ Boolean
Check to see if the outage should be retested to make sure it’s still ongoing.
-
#start_time ⇒ Time
Get the time at which the outage started.
-
#update_last_test_time! ⇒ Object
Update the last test time to now.
Constructor Details
#initialize(service:, body:) ⇒ Breakers::Outage
Create a new outage
62 63 64 65 |
# File 'lib/breakers/outage.rb', line 62 def initialize(service:, body:) @body = MultiJson.load(body) @service = service end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
7 8 9 |
# File 'lib/breakers/outage.rb', line 7 def body @body end |
#service ⇒ Object (readonly)
Returns the value of attribute service.
6 7 8 |
# File 'lib/breakers/outage.rb', line 6 def service @service end |
Class Method Details
.create(service:, forced: false) ⇒ Breakers::Outage
Create a new outage on the given service
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/breakers/outage.rb', line 38 def self.create(service:, forced: false) data = MultiJson.dump(start_time: Time.now.utc.to_i, forced: forced) Breakers.client.redis_connection.zadd(outages_key(service: service), Time.now.utc.to_i, data) Breakers.client.logger&.error(msg: 'Breakers outage beginning', service: service.name, forced: forced) Breakers.client.plugins.each do |plugin| plugin.on_outage_begin(Outage.new(service: service, body: data)) if plugin.respond_to?(:on_outage_begin) end end |
.find_latest(service:) ⇒ Breakers::Outage
Return the most recent outage on the given service
13 14 15 16 |
# File 'lib/breakers/outage.rb', line 13 def self.find_latest(service:) data = Breakers.client.redis_connection.zrange(outages_key(service: service), -1, -1)[0] data && new(service: service, body: data) end |
.in_range(service:, start_time:, end_time:) ⇒ Breakers::Outage
Return all of the outages on the given service that begin in the time range
24 25 26 27 28 29 30 31 |
# File 'lib/breakers/outage.rb', line 24 def self.in_range(service:, start_time:, end_time:) data = Breakers.client.redis_connection.zrangebyscore( outages_key(service: service), start_time.to_i, end_time.to_i ) data.map { |item| new(service: service, body: item) } end |
.outages_key(service:) ⇒ String
Get the key for storing the outage data in Redis for this service
53 54 55 |
# File 'lib/breakers/outage.rb', line 53 def self.outages_key(service:) "#{Breakers.redis_prefix}#{service.name}-outages" end |
Instance Method Details
#end! ⇒ Object
Tell the outage to end, which will allow requests to begin flowing again
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/breakers/outage.rb', line 82 def end! new_body = @body.dup new_body['end_time'] = Time.now.utc.to_i replace_body(body: new_body) Breakers.client.logger&.info(msg: 'Breakers outage ending', service: @service.name, forced: forced?) Breakers.client.plugins.each do |plugin| plugin.on_outage_end(self) if plugin.respond_to?(:on_outage_begin) end end |
#end_time ⇒ Time
Get the time at which the outage ended
103 104 105 |
# File 'lib/breakers/outage.rb', line 103 def end_time @body['end_time'] && Time.at(@body['end_time']).utc end |
#ended? ⇒ Boolean
Check to see if the outage has ended
70 71 72 |
# File 'lib/breakers/outage.rb', line 70 def ended? @body.key?('end_time') end |
#forced? ⇒ Boolean
Was the outage forced?
77 78 79 |
# File 'lib/breakers/outage.rb', line 77 def forced? @body['forced'] end |
#last_test_time ⇒ Time
Get the time at which the outage last received a new request
110 111 112 |
# File 'lib/breakers/outage.rb', line 110 def last_test_time (@body['last_test_time'] && Time.at(@body['last_test_time']).utc) || start_time end |
#ready_for_retest?(wait_seconds:) ⇒ Boolean
Check to see if the outage should be retested to make sure it’s still ongoing
124 125 126 |
# File 'lib/breakers/outage.rb', line 124 def ready_for_retest?(wait_seconds:) (Time.now.utc - last_test_time) > wait_seconds end |
#start_time ⇒ Time
Get the time at which the outage started
96 97 98 |
# File 'lib/breakers/outage.rb', line 96 def start_time @body['start_time'] && Time.at(@body['start_time']).utc end |
#update_last_test_time! ⇒ Object
Update the last test time to now
115 116 117 118 119 |
# File 'lib/breakers/outage.rb', line 115 def update_last_test_time! new_body = @body.dup new_body['last_test_time'] = Time.now.utc.to_i replace_body(body: new_body) end |