Class: ComponentHost::Host

Inherits:
Object
  • Object
show all
Includes:
Dependency, Log::Dependency
Defined in:
lib/component_host/host.rb

Defined Under Namespace

Classes: Component

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.buildObject



9
10
11
12
13
14
# File 'lib/component_host/host.rb', line 9

def self.build
  instance = new
  Signal.configure instance
  instance.send = Actor::Messaging::Send
  instance
end

Instance Method Details

#abortObject

Raises:

  • (StopIteration)


115
116
117
# File 'lib/component_host/host.rb', line 115

def abort
  raise StopIteration
end

#componentsObject



111
112
113
# File 'lib/component_host/host.rb', line 111

def components
  @components ||= []
end

#log_observerObject



107
108
109
# File 'lib/component_host/host.rb', line 107

def log_observer
  @log_observer ||= SupervisorObservers::Log.new
end

#record_error(&block) ⇒ Object



28
29
30
# File 'lib/component_host/host.rb', line 28

def record_error(&block)
  record_errors_observer.record_error_proc = block
end

#record_errors_observerObject



103
104
105
# File 'lib/component_host/host.rb', line 103

def record_errors_observer
  @record_errors_observer ||= SupervisorObservers::RecordErrors.new
end

#register(initiator, name = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/component_host/host.rb', line 16

def register(initiator, name=nil)
  logger.trace(tag: :component_host) { "Registering component (Component Initiator: #{initiator}, Name: #{name || '(none)'})" }

  component = Component.new(initiator, name)

  components << component

  logger.debug(tag: :component_host) { "Registered component (Component Initiator: #{initiator}, Name: #{name || '(none)'})" }

  component
end

#registered?(&block) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
122
123
124
125
# File 'lib/component_host/host.rb', line 119

def registered?(&block)
  block ||= proc { true }

  components.any? do |component|
    block.(component.initiator, component.name)
  end
end

#start(&probe) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/component_host/host.rb', line 32

def start(&probe)
  started_components = []

  Actor::Supervisor.start do |supervisor|
    supervisor.add_observer record_errors_observer

    supervisor.add_observer log_observer

    signal.trap 'TSTP' do
      message = Actor::Messages::Suspend

      send.(message, supervisor.address)

      logger.info(tags: [:*, :signal]) { "Handled TSTP signal (Message Name: #{message.message_name}, SupervisorAddress: #{supervisor.address.id})" }
    end

    signal.trap 'CONT' do
      message = Actor::Messages::Resume

      send.(message, supervisor.address)

      logger.info(tags: [:*, :signal]) { "Handled CONT signal (Message Name: #{message.message_name}, SupervisorAddress: #{supervisor.address.id})" }
    end

    signal.trap 'INT' do
      message = Actor::Messages::Shutdown

      send.(message, supervisor.address)

      logger.info(tags: [:*, :signal]) { "Handled INT signal (Message Name: #{message.message_name}, SupervisorAddress: #{supervisor.address.id})" }
    end

    signal.trap 'TERM' do
      message = Actor::Messages::Shutdown

      send.(message, supervisor.address)

      logger.info(tags: [:*, :signal]) { "Handled TERM signal (Message Name: #{message.message_name}, SupervisorAddress: #{supervisor.address.id})" }
    end

    start_components do |component|
      started_components << component
    end

    probe.(supervisor) if probe
  end

  started_components
end

#start_components(&block) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/component_host/host.rb', line 82

def start_components(&block)
  components.each do |component|
    if ComponentHost::Defaults.startup_info?
      STDOUT.puts "  Component: #{component.initiator} (Name: #{component.name || '(none)'})"
    end

    logger.trace(tags: [:component_host, :start]) { "Starting component: #{component.initiator} (Name: #{component.name || '(none)'})" }

    component.start

    logger.info(tags: [:component_host, :start]) { "Started component: #{component.initiator} (Name: #{component.name || '(none)'})" }

    block.(component) if block
  end

rescue => error
  record_errors_observer.(error)
  logger.fatal(tags: [:*, :component_host, :start]) { "#{error.message} (Error: #{error.class})" }
  raise error
end