class MainLoop::ProcessHandler

Attributes

pid[R]

Public Class Methods

new(dispatcher, name, **kwargs, &block) click to toggle source
Calls superclass method MainLoop::Handler::new
# File lib/main_loop/process_handler.rb, line 8
def initialize(dispatcher, name, **kwargs, &block)
  super
  @handler_type = 'Process'
  @pid = nil
  dispatcher.add_handler(self)

  run(&block) if block_given?
end

Public Instance Methods

id() click to toggle source
# File lib/main_loop/process_handler.rb, line 17
def id
  @pid
end
kill() click to toggle source
# File lib/main_loop/process_handler.rb, line 51
def kill
  unless @pid
    logger.debug "Process[#{name}] alredy Killed. Skipped."
    return
  end

  @success = false
  logger.info "Process[#{name}] send kill: Pid:#{@pid}"
  ::Process.kill('KILL', @pid) rescue nil
end
reap(status) click to toggle source
# File lib/main_loop/process_handler.rb, line 21
def reap(status)
  logger.info "Process[#{name}] exited: Pid:#{@pid} Status: #{status.exitstatus.inspect} Termsig: #{status.termsig.inspect} Success: #{status.success?}"
  @pid = nil
  @finished = true
  @success = !!status.success?

  return if terminating?

  handle_retry
end
run(&block) click to toggle source
# File lib/main_loop/process_handler.rb, line 62
def run(&block)
  return if terminating?

  @block = block
  start_fork(&@block)
end
term() click to toggle source
# File lib/main_loop/process_handler.rb, line 32
def term
  unless @pid
    @terminating_at ||= Time.now
    logger.debug "Process[#{name}] alredy terminated. Skipped."
    return
  end

  if terminating?
    @success = false
    logger.info "Process[#{name}] send force terminate: KILL Pid:#{@pid}"
    ::Process.kill('KILL', @pid) rescue nil
  else
    @terminating_at ||= Time.now
    logger.info "Process[#{name}] send terminate: Pid:#{@pid}"
    @on_term&.call(@pid) rescue nil
    ::Process.kill('TERM', @pid) rescue nil
  end
end

Protected Instance Methods

start_fork() { || ... } click to toggle source
# File lib/main_loop/process_handler.rb, line 71
def start_fork
  @pid = Kernel.fork do
    yield
  end
  @finished = false
  logger.info "Process[#{name}] created: Pid:#{@pid}"
end