class Forklift::Base::Pid

Public Class Methods

new(forklift) click to toggle source
# File lib/forklift/base/pid.rb, line 5
def initialize(forklift)
  @forklift = forklift
end

Public Instance Methods

delete!() click to toggle source
# File lib/forklift/base/pid.rb, line 36
def delete!
  forklift.logger.debug "Removing pidfile @ #{pidfile}"
  FileUtils.rm(pidfile) rescue nil
end
ensure_pid_dir() click to toggle source
# File lib/forklift/base/pid.rb, line 17
def ensure_pid_dir
  `mkdir -p #{pid_dir}`
end
forklift() click to toggle source
# File lib/forklift/base/pid.rb, line 9
def forklift
  @forklift
end
pid_dir() click to toggle source
# File lib/forklift/base/pid.rb, line 13
def pid_dir
  "#{forklift.config[:project_root]}/pid"
end
pidfile() click to toggle source
# File lib/forklift/base/pid.rb, line 21
def pidfile
  "#{pid_dir}/pidfile"
end
recall() click to toggle source
# File lib/forklift/base/pid.rb, line 31
def recall
  ensure_pid_dir
  IO.read(pidfile).to_i rescue nil
end
safe_to_run?() click to toggle source
# File lib/forklift/base/pid.rb, line 41
def safe_to_run?
  return if recall.nil?
  count = `ps -p #{recall} | wc -l`.to_i
  if count >= 2
    forklift.logger.fatal "This application is already running (pidfile) #{recall}. Exiting now"
    exit(1)
  else
    forklift.logger.log "Clearing old pidfile from previous process #{recall}"
    delete!
  end
end
store!() click to toggle source
# File lib/forklift/base/pid.rb, line 25
def store!
  forklift.logger.debug "Creating pidfile @ #{pidfile}"
  ensure_pid_dir
  File.open(pidfile, 'w') {|f| f << Process.pid}
end