Module: Smith::AgentProcessObserver

Includes:
Logger
Defined in:
lib/smith/agent_process.rb

Class Method Summary collapse

Methods included from Logger

included

Class Method Details

.acknowledge_start(agent_process, &blk) ⇒ Object



202
203
204
# File 'lib/smith/agent_process.rb', line 202

def self.acknowledge_start(agent_process, &blk)
  logger.info { "Agent started: #{agent_process.uuid}" }
end

.acknowledge_stop(agent_process) ⇒ Object



224
225
226
227
# File 'lib/smith/agent_process.rb', line 224

def self.acknowledge_stop(agent_process)
  agent_process.delete
  logger.info { "Agent stopped: #{agent_process.uuid}" }
end

.kill(agent_process) ⇒ Object

This needs to use the PID class to verify if an agent is still running. FIXME



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/smith/agent_process.rb', line 231

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 kill signal: #{agent_process.pid}: #{agent_process.uuid}" }
      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



219
220
221
# File 'lib/smith/agent_process.rb', line 219

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



255
256
257
258
259
260
261
262
# File 'lib/smith/agent_process.rb', line 255

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
# File 'lib/smith/agent_process.rb', line 172

def self.start_process(agent_process)
  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').expand_path

    binary = Smith.config.ruby[agent_process.name]
    logger.debug { "Launching #{agent_process.name} with: #{binary}" }
    exec(binary, bootstrapper.to_s, agent_process.name, agent_process.uuid)
  end

  # We don't want any zombies.
  Process.detach(agent_process.pid)
end

.stop(agent_process) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/smith/agent_process.rb', line 206

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.no_process_running
      end
    end
  end
end