Class | MCollective::Runner |
In: |
lib/mcollective/runner.rb
|
Parent: | Object |
The main runner for the daemon, supports running in the foreground and the background, keeps detailed stats and provides hooks to access all this information
Daemonize the current process
# File lib/mcollective/runner.rb, line 32 32: def self.daemonize 33: fork do 34: Process.setsid 35: exit if fork 36: Dir.chdir('/tmp') 37: STDIN.reopen('/dev/null') 38: STDOUT.reopen('/dev/null', 'a') 39: STDERR.reopen('/dev/null', 'a') 40: 41: yield 42: end 43: end
# File lib/mcollective/runner.rb, line 6 6: def initialize(configfile) 7: @config = Config.instance 8: @config.loadconfig(configfile) unless @config.configured 9: 10: @stats = PluginManager["global_stats"] 11: 12: @security = PluginManager["security_plugin"] 13: @security.initiated_by = :node 14: 15: @connection = PluginManager["connector_plugin"] 16: @connection.connect 17: 18: @agents = Agents.new 19: 20: Signal.trap("USR1") do 21: Log.info("Reloading all agents after receiving USR1 signal") 22: @agents.loadagents 23: end 24: 25: Signal.trap("USR2") do 26: Log.info("Cycling logging level due to USR2 signal") 27: Log.cycle_level 28: end 29: end
Starts the main loop, before calling this you should initialize the MCollective::Config singleton.
# File lib/mcollective/runner.rb, line 46 46: def run 47: controltopics = Util.make_target("mcollective", :command) 48: Util.subscribe(controltopics) 49: 50: # Start the registration plugin if interval isn't 0 51: begin 52: PluginManager["registration_plugin"].run(@connection) unless @config.registerinterval == 0 53: rescue Exception => e 54: Log.error("Failed to start registration plugin: #{e}") 55: end 56: 57: loop do 58: begin 59: msg = receive 60: dest = msg[:msgtarget] 61: 62: sep = Regexp.escape(@config.topicsep) 63: prefix = Regexp.escape(@config.topicprefix) 64: regex = "#{prefix}(.+?)#{sep}(.+?)#{sep}command" 65: if dest.match(regex) 66: collective = $1 67: agent = $2 68: else 69: raise "Failed to handle message, could not figure out agent and collective from #{dest}" 70: end 71: 72: if agent == "mcollective" 73: Log.debug("Handling message for mcollectived controller") 74: 75: controlmsg(msg, collective) 76: else 77: Log.debug("Handling message for agent '#{agent}' on collective '#{collective}'") 78: 79: agentmsg(msg, agent, collective) 80: end 81: rescue Interrupt 82: Log.warn("Exiting after interrupt signal") 83: @connection.disconnect 84: exit! 85: 86: rescue NotTargettedAtUs => e 87: Log.debug("Message does not pass filters, ignoring") 88: 89: rescue Exception => e 90: Log.warn("Failed to handle message: #{e} - #{e.class}\n") 91: Log.warn(e.backtrace.join("\n\t")) 92: end 93: end 94: end