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.



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/heartcheck/checks/base.rb', line 44

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

#doc_urlString

An url to provide more info about the check.

It is important to provide details about the failure, for example:
- what is the impact
- how to fix it
- how to reproduce the failure

Returns:

  • (String)

    (default: nil)



39
40
41
# File 'lib/heartcheck/checks/base.rb', line 39

def doc_url
  @doc_url
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)


103
104
105
# File 'lib/heartcheck/checks/base.rb', line 103

def add_service(service)
  @services << service
end

#checkHash

run the check and return formated erros

Returns:

  • (Hash)


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

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

#informationsObject



144
145
146
147
148
# File 'lib/heartcheck/checks/base.rb', line 144

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

#inspectString

Returns a human-readable representation of the check

Returns:

  • (String)


132
133
134
# File 'lib/heartcheck/checks/base.rb', line 132

def inspect
  "#<#{self.class.name} name: #{name}, functional: #{functional?}, dev: #{dev?}>"
end

#on_error(&block) ⇒ void

This method returns an undefined value.

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



59
60
61
# File 'lib/heartcheck/checks/base.rb', line 59

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


91
92
93
# File 'lib/heartcheck/checks/base.rb', line 91

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>)


111
112
113
114
115
116
117
# File 'lib/heartcheck/checks/base.rb', line 111

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



76
77
78
# File 'lib/heartcheck/checks/base.rb', line 76

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

#uri_infoHash

Returns a structure comprised of host, port and schema (protocol) for the check

Returns:

  • (Hash)


140
141
142
# File 'lib/heartcheck/checks/base.rb', line 140

def uri_info
  { error: "#{self.class.name} #url_info not implemented." }
end