Class: Liveness::Dependency

Inherits:
Object
  • Object
show all
Defined in:
lib/liveness/dependency.rb

Overview

The required dependency for services

Since:

  • 0.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: nil, timeout: 5, &block) ⇒ Dependency

Returns a new instance of Dependency.

Since:

  • 0.1.0



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

#nameObject

Since:

  • 0.1.0



11
12
13
# File 'lib/liveness/dependency.rb', line 11

def name
  @name
end

#timeoutObject

Since:

  • 0.1.0



11
12
13
# File 'lib/liveness/dependency.rb', line 11

def timeout
  @timeout
end

Instance Method Details

#alive?Boolean

Check the dependency service alive

Returns:

  • (Boolean)

Since:

  • 0.1.0



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

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)

Since:

  • 0.1.0



51
52
53
# File 'lib/liveness/dependency.rb', line 51

def check!
  raise NotImplementedError
end

#connectObject

Connect with connector

Returns:

  • (Object)

Since:

  • 0.2.0



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

#statusHash

Return status

Returns:

  • (Hash)

Since:

  • 0.2.0



25
26
27
28
29
# File 'lib/liveness/dependency.rb', line 25

def status
  {
    status: alive? ? 'ok' : 'failed'
  }
end