Class: Wildcloud::Agent::ComponentManager

Inherits:
Object
  • Object
show all
Defined in:
lib/wildcloud/agent/component_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent) ⇒ ComponentManager

Returns a new instance of ComponentManager.



22
23
24
25
26
27
# File 'lib/wildcloud/agent/component_manager.rb', line 22

def initialize(agent)
  @agent = agent
  @components = []
  @settings = {}
  @shutdown = false
end

Instance Attribute Details

#componentsObject (readonly)

Returns the value of attribute components.



20
21
22
# File 'lib/wildcloud/agent/component_manager.rb', line 20

def components
  @components
end

Instance Method Details

#active_componentsObject



78
79
80
# File 'lib/wildcloud/agent/component_manager.rb', line 78

def active_components
  @components
end

#auto_startObject



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/wildcloud/agent/component_manager.rb', line 89

def auto_start
  (@agent.config['autostart'] ||= []).each do |component|
    options = {'action' => 'start', 'persistent' => true}
    if component.kind_of?(Hash)
      options['component'] = component['component']
      options['user'] = component['user']
      options['directory'] = component['directory']
    else
      options['component'] = component.to_s
    end
    handle_message(options)
  end
end

#handle_message(message) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/wildcloud/agent/component_manager.rb', line 29

def handle_message(message)
  action = message['action']
  handler = "handle_#{action}".to_sym
  if respond_to?(handler)
    send(handler, message)
  else
    @agent.logger.error('ComponentManager') { "Invalid action #{action}" }
  end
end

#handle_start(message) ⇒ Object



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
# File 'lib/wildcloud/agent/component_manager.rb', line 39

def handle_start(message)
  component = message['component']
  message['persistent'] ||= false
  @settings[component] = {}

  if @components.include?(component)
    @agent.logger.error('ComponentManager') { "Component #{component} is already running" }
    return
  end

  @agent.logger.info('ComponentManager') { "Component #{component} is being started" }

  started = proc do |process|
    @settings[component].merge!(message)
    @components << component
    @agent.logger.info('ComponentManager') { "Component #{component} started" }
  end

  stopped = proc do |out, status|
    @components.delete(component)
    @agent.logger.info('ComponentManager') { "Component #{component} stopped with status #{status.exitstatus}" }
    if !@shutdown && @settings[component]['persistent']
      @agent.logger.info('ComponentManager') { "Component #{component} will be restarted" }
      EventMachine.add_timer(5) do
        handle_start(@settings[component])
      end
    end
  end

  command = "wildcloud-#{component}"

  command = "cd #{message['directory']} && exec #{command}" if message['directory']

  command = "su #{message['user']} -c 'source /etc/profile && #{command}'" if message['user']

  @settings[component][:pid] = EventMachine.system(command, started, stopped)

end

#shutdownObject



82
83
84
85
86
87
# File 'lib/wildcloud/agent/component_manager.rb', line 82

def shutdown
  @shutdown = true
  @settings.each do |component, settings|
    Process.kill('TERM', settings[:pid])
  end
end