class PidWatcher

Constants

DEFAULT_MESSAGE
DEFAULT_TIMEOUT
TIMEOUT_MESSAGE
TITLE
VERSION

Attributes

command[R]
message[R]
pid[R]
timeout[R]

Public Class Methods

new(pid, options) click to toggle source
# File lib/pid_watcher.rb, line 11
def initialize(pid, options)
  @pid     = pid
  @message = options[:message] || DEFAULT_MESSAGE
  @timeout = options[:timeout] || DEFAULT_TIMEOUT
  @command = options[:command]
end

Public Instance Methods

watch() click to toggle source
# File lib/pid_watcher.rb, line 18
def watch
  Process.daemon

  Timeout::timeout(timeout) do
    while pid_exists?
      sleep(1)
    end
    run_command unless command.nil?
    finish_notify
  end
rescue Timeout::Error
  timeout_notify
end

Private Instance Methods

finish_notify() click to toggle source
# File lib/pid_watcher.rb, line 45
def finish_notify
  prepared_msg = message % { :pid => pid }
  prepared_msg += " and '#{command}' was running in background" unless command.nil?

  TerminalNotifier.notify(
    prepared_msg,
    :title => TITLE,
    :subtitle => 'Finished'
  )
end
pid_exists?() click to toggle source
# File lib/pid_watcher.rb, line 40
def pid_exists?
  status = Process.kill(0, pid) rescue false
  !!status
end
run_command() click to toggle source
# File lib/pid_watcher.rb, line 34
def run_command
  Process.spawn(command)
rescue
  true
end
timeout_notify() click to toggle source
# File lib/pid_watcher.rb, line 56
def timeout_notify
  TerminalNotifier.notify(
    TIMEOUT_MESSAGE % { :pid => pid },
    :title    => TITLE,
    :subtitle => 'Timeout'
  )
end