Module: Smith::AgentProcessObserver
- Includes:
- Logger
- Defined in:
- lib/smith/agent_process.rb
Class Method Summary collapse
-
.acknowledge_start(agent_process, &blk) ⇒ Object
FIXME: This doesn’t appear to be being called.
- .acknowledge_stop(agent_process) ⇒ Object
-
.kill(agent_process) ⇒ Object
This needs to use the PID class to verify if an agent is still running.
- .no_process_running(agent_process) ⇒ Object
-
.reap_agent(agent_process) ⇒ Object
If an agent is in an unknown state then this will check to see if the process is still alive and if it is kill it, otherwise log a message.
- .start(agent_process) ⇒ Object
-
.start_process(agent_process) ⇒ Object
Start an agent.
- .stop(agent_process) ⇒ Object
Methods included from Logger
Class Method Details
.acknowledge_start(agent_process, &blk) ⇒ Object
FIXME: This doesn’t appear to be being called.
205 206 207 |
# File 'lib/smith/agent_process.rb', line 205 def self.acknowledge_start(agent_process, &blk) logger.info { "Agent started: #{agent_process.name}, UUID: #{agent_process.uuid}, PID: #{agent_process.pid}" } end |
.acknowledge_stop(agent_process) ⇒ Object
227 228 229 230 |
# File 'lib/smith/agent_process.rb', line 227 def self.acknowledge_stop(agent_process) agent_process.delete logger.info { "Agent stopped: #{agent_process.name}, UUID: #{agent_process.uuid}, PID: #{agent_process.pid}" } end |
.kill(agent_process) ⇒ Object
This needs to use the PID class to verify if an agent is still running. FIXME
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/smith/agent_process.rb', line 234 def self.kill(agent_process) if agent_process.pid if agent_process.pid == 0 logger.info { "Agent's pid is 0. The agent probably didn't start correctly. Cleaning up." } agent_process.delete else logger.info { "Sending signal: TERM, #{agent_process.name}, UUID: #{agent_process.uuid}, PID: #{agent_process.pid}" } begin Process.kill('TERM', agent_process.pid) rescue logger.error { "Process does not exist. PID is stale: #{agent_process.pid}: #{agent_process.uuid}" } end end else logger.error { "Not sending kill signal, agent pid is not set: #{agent_process.uuid}" } end agent_process.delete end |
.no_process_running(agent_process) ⇒ Object
222 223 224 |
# File 'lib/smith/agent_process.rb', line 222 def self.no_process_running(agent_process) agent_process.delete end |
.reap_agent(agent_process) ⇒ Object
If an agent is in an unknown state then this will check to see if the process is still alive and if it is kill it, otherwise log a message. TODO this is not really a reaper but I haven’t quite worked out what I’m going to do with it so I’ll leave it as is
259 260 261 262 263 264 265 266 |
# File 'lib/smith/agent_process.rb', line 259 def self.reap_agent(agent_process) logger.info { "Reaping agent: #{agent_process.uuid}" } if Pathname.new('/proc').join(agent_process.pid.to_s).exist? logger.warn { "Agent is still alive: #{agent_process.uuid}" } else logger.warn { "Agent is already dead: #{agent_process.uuid}" } end end |
.start(agent_process) ⇒ Object
162 163 164 165 166 167 168 |
# File 'lib/smith/agent_process.rb', line 162 def self.start(agent_process) if agent_process.exists? agent_process.check else agent_process.delete end end |
.start_process(agent_process) ⇒ Object
Start an agent. This forks and execs the bootstrapper class which then becomes responsible for managing the agent process.
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/smith/agent_process.rb', line 172 def self.start_process(agent_process) pid = fork do # Detach from the controlling terminal unless Process.setsid raise 'Cannot detach from controlling terminal' end # Close all file descriptors apart from stdin, stdout, stderr ObjectSpace.each_object(IO) do |io| unless [STDIN, STDOUT, STDERR].include?(io) io.close unless io.closed? end end # Sort out the remaining file descriptors. Don't do anything with # stdout (and by extension stderr) as want the agency to manage it. STDIN.reopen("/dev/null") STDERR.reopen(STDOUT) bootstrapper = Pathname.new(__FILE__).dirname.join('bootstrap.rb'). binary = Smith.config.vm[agent_process.name.snake_case.to_sym] || Smith.config.vm.agent_default logger.debug { "Launching #{agent_process.name} with: #{binary}" } exec(binary, bootstrapper.to_s, agent_process.name, agent_process.uuid) end logger.info { "Detaching: #{agent_process.name}, UUID: #{agent_process.uuid}, PID: #{pid}" } # We don't want any zombies. Process.detach(pid) end |
.stop(agent_process) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/smith/agent_process.rb', line 209 def self.stop(agent_process) Messaging::Sender.new(agent_process.control_queue_def) do |sender| sender.consumer_count do |count| if count > 0 sender.publish(ACL::AgentCommand.new(:command => 'stop')) else logger.warn { "Agent is not listening. Setting state to dead: #{agent_process.name}, UUID: #{agent_process.uuid}, PID: #{agent_process.pid}" } agent_process.no_process_running end end end end |