class Smith::AgentBootstrap
Attributes
Public Class Methods
# File lib/smith/bootstrap.rb, line 19 def initialize(name, uuid) Dir.chdir('/') # FIXME # This doesn't do what I think it should. If an exception is # thrown in setup_control_queue, for example, it just kills # the agent without it actually raising the exception. Thread.abort_on_exception = true EventMachine.error_handler { |e| terminate!(e) } @agent_name = name @agent_uuid = uuid end
Public Instance Methods
# File lib/smith/bootstrap.rb, line 45 def load_agent path = agent_directories(@agent_name) logger.info { "Loading #{@agent_name} from: #{path}" } add_agent_load_path(path) load path @agent = class_from_name(@agent_name).new(@agent_uuid) end
Cleanly shutdown of the agent.
# File lib/smith/bootstrap.rb, line 78 def shutdown unlink_pid_file Smith.stop if Smith.running? end
# File lib/smith/bootstrap.rb, line 34 def signal_handlers logger.debug { "Installing default signal handlers" } %w{TERM INT QUIT}.each do |sig| @agent.install_signal_handler(sig) do |sig| logger.error { "Received signal #{sig}: #{agent.name}, UUID: #{agent.uuid}, PID: #{agent.pid}." } terminate! end end end
# File lib/smith/bootstrap.rb, line 54 def start! write_pid_file end
Exceptional shutdown of the agent. Note. Whenever this is called it almost certain that the reactor is not going to be running. So it must be restarted and then shutdown again See the note at the in main.
# File lib/smith/bootstrap.rb, line 62 def terminate!(exception=nil) handle_excecption(exception) if Smith.running? send_dead_message shutdown else Smith.start do send_dead_message shutdown end end end
Private Instance Methods
Add directories with library code to the load path. This assumes that the required files reside at the same level as the agent directory.
Always defaults to `lib` if a `lib_directories` list is not provided by the configuration.
$ROOT_PATH/agents
.../lib
Where $ROOT_PATH is dependent on the agent directory.
# File lib/smith/bootstrap.rb, line 135 def add_agent_load_path(path) Smith.agent_directories.each do |path| Smith.lib_directories.each do |lib_dir| lib_path = path.parent.join(lib_dir) if lib_path.exist? logger.info { "Adding #{lib_path} to load path." } $LOAD_PATH << lib_path else logger.info { "#{lib_dir} directory not found in: #{path.parent}." } end end end end
Format any excptions @param e [Exception] the exeption to format @return [String] formated exception
# File lib/smith/bootstrap.rb, line 107 def format_exception(e) "#{e.class.to_s}: #{e.inspect}\n\t".tap do |exception_string| exception_string << e.backtrace[0..-1].join("\n\t") if e.backtrace end end
Handle any exceptions. This will run the on_exception proc defined in the agent and log the exception.
@param e [Exception] the exeption to handle
# File lib/smith/bootstrap.rb, line 117 def handle_excecption(exception) if exception @agent.__send__(:__exception_handler, exception) if @agent logger.error { format_exception(exception) } end logger.error { "Terminating: #{@agent_name}, UUID: #{@agent_uuid}, PID: #{@pid.pid}." } end
# File lib/smith/bootstrap.rb, line 91 def send_dead_message logger.debug { "Sending dead message to agency: #{@agent_name} (#{@agent_uuid})" } Messaging::Sender.new(QueueDefinitions::Agent_lifecycle) do |sender| sender.publish(ACL::AgentDead.new(:uuid => @agent_uuid)) end end
# File lib/smith/bootstrap.rb, line 98 def unlink_pid_file if @pid && @pid.exist? logger.debug { "Cleaning up pid file: #{@pid.filename}" } end end
FIXME This really should be using Smith::Daemon
# File lib/smith/bootstrap.rb, line 86 def write_pid_file @pid = Daemons::PidFile.new(Daemons::Pid.dir(:normal, Dir::tmpdir, nil), ".smith-#{@agent_uuid}", true) @pid.pid = Process.pid end