Class: Heartcheck::Checks::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/heartcheck/checks/base.rb

Overview

Base check that contains the common functionality for chechs

Direct Known Subclasses

Firewall, Process, WatchFile

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCheck

Create a new instance and set the default values.



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/heartcheck/checks/base.rb', line 37

def initialize
  @dynamic_services = nil
  @error_proc       = nil
  @errors           = []
  @functional       = false
  @dev              = false
  @name             = self.class.name.split('::').last.downcase
  @services         = []
  @timeout          = 0
  @validate_proc    = nil
end

Instance Attribute Details

#devBoolean Also known as: dev?

When it is true the check will work just for ‘/dev` route.

you can use it to create some check is not essential/functional for you app.
For example you can execute some performance check.

Returns:

  • (Boolean)

    (default: false)



19
20
21
# File 'lib/heartcheck/checks/base.rb', line 19

def dev
  @dev
end

#functionalBoolean Also known as: functional?

When it is true the check will work just for ‘/functional` route.

It should be used to add warning messages that need to verify,
but doesn't break your application.
For example when your async process has more than 3 jobs.

Returns:

  • (Boolean)

    (default: false)



11
12
13
# File 'lib/heartcheck/checks/base.rb', line 11

def functional
  @functional
end

#nameString

The name for identify the check in the result page

Returns:

  • (String)


25
26
27
# File 'lib/heartcheck/checks/base.rb', line 25

def name
  @name
end

#timeoutInteger

Time that the check has to execute.

If it is 0 the timeout will be disable

Returns:

  • (Integer)

    (default: 0)



31
32
33
# File 'lib/heartcheck/checks/base.rb', line 31

def timeout
  @timeout
end

Instance Method Details

#add_service(service) ⇒ void

This method returns an undefined value.

It is used to add a service that will use when check run.

Examples:

add_service(name: 'service name', connection: MyConnection)

Parameters:

  • service (Hash)


96
97
98
# File 'lib/heartcheck/checks/base.rb', line 96

def add_service(service)
  @services << service
end

#checkHash

run the check and return formated erros

Returns:

  • (Hash)


115
116
117
118
119
120
# File 'lib/heartcheck/checks/base.rb', line 115

def check
  validation
  { name => { 'status' => (@errors.empty? ? 'ok' : 'error') } }.tap do |response|
    response[name]['message'] = error_message unless @errors.empty?
  end
end

#informationsObject



122
123
124
125
126
# File 'lib/heartcheck/checks/base.rb', line 122

def informations
  info
rescue => e
  { 'error' => e.message }
end

#on_error(&block) ⇒ void

This method returns an undefined value.

Seter to add a proc to execute when some check fail.



52
53
54
# File 'lib/heartcheck/checks/base.rb', line 52

def on_error(&block)
  @error_proc = block if block_given?
end

#register_dynamic_services(&block) ⇒ void

This method returns an undefined value.

It is used to register dynamic services thats need to be evaluate when the checker is running.

This proc need to return an Array.

Examples:

register_dynamic_services do
  [
    { name: 'service name', connection: MyConnection }
  ]
end


84
85
86
# File 'lib/heartcheck/checks/base.rb', line 84

def register_dynamic_services(&block)
  @dynamic_services = block if block_given?
end

#servicesArray<Hash>

It is used to add a service that will use when check run.

Returns:

  • (Array<Hash>)


104
105
106
107
108
109
110
# File 'lib/heartcheck/checks/base.rb', line 104

def services
  if @dynamic_services
    @services + @dynamic_services.call
  else
    @services
  end
end

#to_validate {|services, errors| ... } ⇒ void

This method returns an undefined value.

Seter to add a proc to execute to check.

Examples:

c.to_validate do |services, errors|
  services.each do |service|
    errors << "erro message" unless if service[:connection].connected?
  end
end

Yield Parameters:

  • services (Array<Hash>)

    services to check

  • errors (Array<String>)

    to add error message when check fails



69
70
71
# File 'lib/heartcheck/checks/base.rb', line 69

def to_validate(&block)
  @validate_proc = block if block_given?
end