lib/sfpagent/agent.rb in sfpagent-0.3.6 vs lib/sfpagent/agent.rb in sfpagent-0.3.7

- old
+ new

@@ -11,11 +11,11 @@ module Sfp module Agent NetHelper = Object.new.extend(Sfp::Helper::Net) - CacheDir = (Process.euid == 0 ? '/var/sfpagent' : File.expand_path('~/.sfpagent')) + CacheDir = (Process.euid == 0 ? '/var/sfpagent' : File.expand_path(Dir.home + '/.sfpagent')) Dir.mkdir(CacheDir, 0700) if not File.exist?(CacheDir) DefaultPort = 1314 PIDFile = "#{CacheDir}/sfpagent.pid" @@ -67,12 +67,12 @@ # :ssl => set true to enable HTTPS # :certfile => certificate file path for HTTPS # :keyfile => key file path for HTTPS # def self.start(opts={}) - Sfp::Agent.logger.info "Starting SFP Agent daemons..." - puts "Starting SFP Agent daemons..." + Sfp::Agent.logger.info "Starting agent..." + puts "Starting agent..." @@config = opts Process.daemon if opts[:daemon] and not opts[:mock] @@ -131,53 +131,62 @@ server.start if not opts[:mock] rescue Exception => e Sfp::Agent.logger.error "Starting the agent [Failed] #{e}\n#{e.backtrace.join("\n")}" + raise e end end # Stop the agent's daemon. # def self.stop(opts={}) begin pid = File.read(PIDFile).to_i - puts "Stopping SFP Agent with PID #{pid}..." + puts "Stopping agent with PID #{pid}..." Process.kill 'HUP', pid if not opts[:mock] begin sleep (Sfp::BSig::SleepTime + 0.25) + Process.kill 0, pid - Sfp::Agent.logger.info "SFP Agent daemon is still running." - puts "SFP Agent daemon is still running." - return false + Sfp::Agent.logger.info "Agent is still running." + puts "Agent is still running." + + Sfp::Agent.logger.info "Killing agent." + puts "Killing agent." + Process.kill 9, pid rescue - Sfp::Agent.logger.info "SFP Agent daemon has stopped." - puts "SFP Agent daemon has stopped." + Sfp::Agent.logger.info "Agent has stopped." + puts "Agent has stopped." File.delete(PIDFile) if File.exist?(PIDFile) end end rescue - puts "SFP Agent is not running." + puts "Agent is not running." File.delete(PIDFile) if File.exist?(PIDFile) end - - true end - # Print the status of the agent. - # - def self.status + def self.pid begin pid = File.read(PIDFile).to_i - Process.kill 0, pid - puts "SFP Agent is running with PID #{pid}" + return pid if Process.kill 0, pid rescue - puts "SFP Agent is not running." - File.delete(PIDFile) if File.exist?(PIDFile) + end + nil + end + + # Return agent's PID if it is running, otherwise nil. + # + def self.status + if pid.nil? + puts "Agent is not running." + else + puts "Agent is running with PID #{pid}" end end def self.get_cache_model(name) if File.exist?(CacheModelFile)