Class: Liveness::Dependency
- Inherits:
-
Object
- Object
- Liveness::Dependency
- Defined in:
- lib/liveness/dependency.rb
Overview
The required dependency for services
Direct Known Subclasses
Liveness::Dependencies::MySQL, Liveness::Dependencies::PostgreSQL, Liveness::Dependencies::Redis
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#alive? ⇒ Boolean
Check the dependency service alive.
-
#check! ⇒ Boolean
Check the dependency service alive.
-
#connect ⇒ Object
Connect with connector.
-
#initialize(name: nil, timeout: 5, &block) ⇒ Dependency
constructor
A new instance of Dependency.
-
#status ⇒ Hash
Return status.
Constructor Details
#initialize(name: nil, timeout: 5, &block) ⇒ Dependency
Returns a new instance of Dependency.
14 15 16 17 18 |
# File 'lib/liveness/dependency.rb', line 14 def initialize(name: nil, timeout: 5, &block) @name = name @timeout = timeout @connector = block end |
Instance Attribute Details
#name ⇒ Object
11 12 13 |
# File 'lib/liveness/dependency.rb', line 11 def name @name end |
#timeout ⇒ Object
11 12 13 |
# File 'lib/liveness/dependency.rb', line 11 def timeout @timeout end |
Instance Method Details
#alive? ⇒ Boolean
Check the dependency service alive
36 37 38 39 40 41 42 43 44 |
# File 'lib/liveness/dependency.rb', line 36 def alive? Timeout.timeout(@timeout) do connect || check! rescue StandardError false end rescue Timeout::Error false end |
#check! ⇒ Boolean
Check the dependency service alive
51 52 53 |
# File 'lib/liveness/dependency.rb', line 51 def check! raise NotImplementedError end |
#connect ⇒ Object
Connect with connector
60 61 62 63 64 |
# File 'lib/liveness/dependency.rb', line 60 def connect return unless @connector.respond_to?(:call) instance_exec(self, &@connector) end |
#status ⇒ Hash
Return status
25 26 27 28 29 |
# File 'lib/liveness/dependency.rb', line 25 def status { status: alive? ? 'ok' : 'failed' } end |