Class: RCS::HeartBeat::Base

Inherits:
Object
  • Object
show all
Extended by:
Tracer
Includes:
SystemStatusCodes, Tracer
Defined in:
lib/rcs-common/heartbeat.rb

Constant Summary

Constants included from Tracer

Tracer::TRACE_YAML_NAME

Constants included from SystemStatusCodes

SystemStatusCodes::ERROR, SystemStatusCodes::OK, SystemStatusCodes::WARN

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Tracer

thread_name, trace, trace_ensure_log_folders, trace_init, trace_named_put, trace_named_remove, trace_nested_pop, trace_nested_push, trace_setup

Constructor Details

#initialize(component_name, component_fullname: nil) ⇒ Base

Returns a new instance of Base.



35
36
37
38
39
40
41
# File 'lib/rcs-common/heartbeat.rb', line 35

def initialize(component_name, component_fullname: nil)
  raise("Undefined component version") unless version
  raise("Undefined component name") unless component_name

  @component_name     = component_name.to_s.downcase
  @component_fullname = component_fullname || "RCS::#{component_name.to_s.capitalize}"
end

Class Method Details

.component(component_name, component_fullname = nil) ⇒ Object

Declare the component To use as an helper in the subclasses



13
14
15
16
# File 'lib/rcs-common/heartbeat.rb', line 13

def self.component(component_name, component_fullname = nil)
  @component_name     = component_name
  @component_fullname = component_fullname
end

.performObject

This method is called from outside



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rcs-common/heartbeat.rb', line 19

def self.perform
  heartbeat = new(@component_name, component_fullname: @component_fullname)
  status, message = *heartbeat.perform
  if status
    heartbeat.update(status, message)
  else
    # trace(:warn, "heartbeat prevented")
  end
rescue Interrupt
  trace :fatal, "Heartbeat was interrupted because of a term signal"
rescue Exception => ex
  trace :fatal, "Cannot perform heartbeat: #{ex.message}"
  trace :fatal, ex.backtrace
end

Instance Method Details

#performObject

Override this method



66
67
68
# File 'lib/rcs-common/heartbeat.rb', line 66

def perform
  system_status_and_message
end

#update(status, message) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rcs-common/heartbeat.rb', line 43

def update(status, message)
  system_status, system_message = *system_status_and_message

  if status == OK and system_status != OK
    status, message = system_status, system_message
  end

  attributes = [@component_fullname, hostname, status, message, machine_stats, @component_name, version]

  if defined?(::Status)
    # Db
    ::Status.status_update(*attributes)
  else
    # Collector
    db_class = Object.const_get(@component_fullname)::DB
    attributes[1] = ''
    db_class.instance.update_status(*attributes)
  end
ensure
  reset_system_status_and_message
end