Module: KL::Agent
- Defined in:
- lib/kldockeragent/agent.rb
Constant Summary collapse
- PIDFile =
"#{KL.home}/kldockeragent.pid"
- @@notifier_engine =
KL::Notifier.new
- @@web_server =
KL::Server.new
- @@global_app_uuid =
KL.getApplicationId
Class Method Summary collapse
- .close ⇒ Object
- .getUUID ⇒ Object
- .getVersion ⇒ Object
- .is_linux ⇒ Object
- .is_windows ⇒ Object
- .notifier_engine ⇒ Object
- .pid ⇒ Object
-
.start(opts = {}) ⇒ Object
Start the agent.
-
.status ⇒ Object
Return agent’s PID if it is running, otherwise nil.
-
.stop(opts = {}) ⇒ Object
Stop the agent’s daemon.
- .web_server ⇒ Object
Class Method Details
.close ⇒ Object
136 137 138 139 |
# File 'lib/kldockeragent/agent.rb', line 136 def self.close KL.logger.info "[main] Stopping agent..." @@main_enabled = false end |
.getUUID ⇒ Object
141 142 143 |
# File 'lib/kldockeragent/agent.rb', line 141 def self.getUUID @@global_app_uuid end |
.getVersion ⇒ Object
145 146 147 |
# File 'lib/kldockeragent/agent.rb', line 145 def self.getVersion File.read(File.dirname(__FILE__) + '/../../VERSION').strip end |
.is_linux ⇒ Object
124 125 126 |
# File 'lib/kldockeragent/agent.rb', line 124 def self.is_linux (RbConfig::CONFIG['host_os'] =~ /linux/) end |
.is_windows ⇒ Object
120 121 122 |
# File 'lib/kldockeragent/agent.rb', line 120 def self.is_windows (RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/) end |
.notifier_engine ⇒ Object
128 129 130 |
# File 'lib/kldockeragent/agent.rb', line 128 def self.notifier_engine @@notifier_engine end |
.pid ⇒ Object
101 102 103 104 105 106 107 108 |
# File 'lib/kldockeragent/agent.rb', line 101 def self.pid begin pid = File.read(PIDFile).to_i return pid if Process.kill 0, pid rescue end nil end |
.start(opts = {}) ⇒ Object
Start the agent.
options: :daemon => true if running as a daemon, false if as a console application :port => port of web server will listen to :ssl => set true to enable HTTPS :certfile => certificate file path for HTTPS :keyfile => key file path for HTTPS
31 32 33 34 35 36 37 38 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 |
# File 'lib/kldockeragent/agent.rb', line 31 def self.start(opts={}) KL.logger.info "[main] Starting agent: #{KL::Agent.getUUID}" puts "Starting agent..." @@config = opts Process.daemon if opts[:daemon] and not opts[:mock] begin if not is_windows # trap signal ['INT', 'HUP'].each do |signal| trap(signal) { KL.logger.info "[main] Shutting down services..." web_server.stop notifier_engine.stop loop do break if notifier_engine.status == :stopped sleep 1 end self.close } end end File.open(PIDFile, 'w', 0644) { |f| f.write($$.to_s) } notifier_engine.start web_server.start @@main_enabled = true while @@main_enabled; end rescue Exception => e KL.logger.error "Starting the agent [Failed] #{e}\n#{e.backtrace.join("\n")}" raise e end end |
.status ⇒ Object
Return agent’s PID if it is running, otherwise nil.
112 113 114 115 116 117 118 |
# File 'lib/kldockeragent/agent.rb', line 112 def self.status if pid.nil? puts "Agent is not running." else puts "Agent is running with PID #{pid}" end end |
.stop(opts = {}) ⇒ Object
Stop the agent’s daemon.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/kldockeragent/agent.rb', line 71 def self.stop(opts={}) begin pid = File.read(PIDFile).to_i puts "Stopping agent with PID #{pid}..." Process.kill 'HUP', pid if not opts[:mock] begin sleep (KL::Notifier::SleepTime + 0.5) Process.kill 0, pid KL.logger.info "[main] Agent is still running." puts "Agent is still running." KL.logger.info "[main] Killing agent." puts "Killing agent." Process.kill 9, pid rescue KL.logger.info "[main] Agent has stopped." puts "Agent has stopped." File.delete(PIDFile) if File.exist?(PIDFile) end end rescue puts "Agent is not running." File.delete(PIDFile) if File.exist?(PIDFile) end end |
.web_server ⇒ Object
132 133 134 |
# File 'lib/kldockeragent/agent.rb', line 132 def self.web_server @@web_server end |