Class: Wildcloud::Agent::ComponentManager
- Inherits:
-
Object
- Object
- Wildcloud::Agent::ComponentManager
- Defined in:
- lib/wildcloud/agent/component_manager.rb
Instance Attribute Summary collapse
-
#components ⇒ Object
readonly
Returns the value of attribute components.
Instance Method Summary collapse
- #active_components ⇒ Object
- #auto_start ⇒ Object
- #handle_message(message) ⇒ Object
- #handle_start(message) ⇒ Object
-
#initialize(agent) ⇒ ComponentManager
constructor
A new instance of ComponentManager.
- #shutdown ⇒ Object
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
#components ⇒ Object (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_components ⇒ Object
78 79 80 |
# File 'lib/wildcloud/agent/component_manager.rb', line 78 def active_components @components end |
#auto_start ⇒ Object
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| = {'action' => 'start', 'persistent' => true} if component.kind_of?(Hash) ['component'] = component['component'] ['user'] = component['user'] ['directory'] = component['directory'] else ['component'] = component.to_s end () 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 () action = ['action'] handler = "handle_#{action}".to_sym if respond_to?(handler) send(handler, ) 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() component = ['component'] ['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!() @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 #{['directory']} && exec #{command}" if ['directory'] command = "su #{['user']} -c 'source /etc/profile && #{command}'" if ['user'] @settings[component][:pid] = EventMachine.system(command, started, stopped) end |
#shutdown ⇒ Object
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 |