Module: RCS::Component

Defined in:
lib/rcs-common/component.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/rcs-common/component.rb', line 5

def self.included(base)
  base.__send__(:include, RCS::Tracer)
  base.__send__(:extend, RCS::Tracer)

  base.__send__(:extend, ClassMethods)

  base.__send__(:define_singleton_method, :component) do |value, name: nil|
    @_component = value.to_sym
    @_component_name = name || "RCS #{value.to_s.capitalize}"
  end
end

Instance Method Details

#componentObject



17
18
19
# File 'lib/rcs-common/component.rb', line 17

def component
  self.class.instance_variable_get('@_component')
end

#component_nameObject



21
22
23
# File 'lib/rcs-common/component.rb', line 21

def component_name
  self.class.instance_variable_get('@_component_name')
end

#component_versionObject



29
30
31
# File 'lib/rcs-common/component.rb', line 29

def component_version
  $version || full_component_version.split('-').first
end

#databaseObject



38
39
40
41
42
43
# File 'lib/rcs-common/component.rb', line 38

def database
  @_database ||= begin
    db_class =  RCS.const_get('Collector::DB') rescue RCS.const_get('DB::DB')
    db_class.instance
  end
end

#establish_database_connection(wait_until_connected: false) ⇒ Object



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

def establish_database_connection(wait_until_connected: false)
  loop do
    connected = database.respond_to?(:connect!) ? database.connect!(component) : database.connect

    if connected
      trace :info, "Database connection succeeded"
      break
    end

    if wait_until_connected
      trace :warn, "Database connection failed, retry..."
      sleep 1
    else
      trace :error, "Database connection failed"
      break
    end
  end
end

#full_component_versionObject



25
26
27
# File 'lib/rcs-common/component.rb', line 25

def full_component_version
  File.read(Dir.pwd + '/config/VERSION').strip
end

#run_with_rescueObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rcs-common/component.rb', line 64

def run_with_rescue
  $version = component_version
  trace_setup
  show_startup_message
  yield
  return 0
rescue Interrupt
  # call the kill handler if defined
  kill if self.respond_to? :kill
  trace :info, "User asked to exit. Bye bye!"
  exit(0)
rescue Exception => e
  trace :fatal, "FAILURE: " << e.message
  trace :fatal, "EXCEPTION: [#{e.class}] " << e.backtrace.join("\n")
  exit(1)
end

#show_startup_messageObject



33
34
35
36
# File 'lib/rcs-common/component.rb', line 33

def show_startup_message
  build = File.read(Dir.pwd + '/config/VERSION_BUILD')
  trace :fatal, "Starting the #{component_name} #{full_component_version} (#{build})..."
end