Class: Forklift::Base::Pid

Inherits:
Object
  • Object
show all
Defined in:
lib/forklift/base/pid.rb

Instance Method Summary collapse

Constructor Details

#initialize(forklift) ⇒ Pid

Returns a new instance of Pid.



5
6
7
# File 'lib/forklift/base/pid.rb', line 5

def initialize(forklift)
  @forklift = forklift
end

Instance Method Details

#delete!Object



36
37
38
39
# File 'lib/forklift/base/pid.rb', line 36

def delete!
  forklift.logger.debug "Removing pidfile @ #{pidfile}"
  FileUtils.rm(pidfile) rescue nil
end

#ensure_pid_dirObject



17
18
19
# File 'lib/forklift/base/pid.rb', line 17

def ensure_pid_dir
  `mkdir -p #{pid_dir}`
end

#forkliftObject



9
10
11
# File 'lib/forklift/base/pid.rb', line 9

def forklift
  @forklift
end

#pid_dirObject



13
14
15
# File 'lib/forklift/base/pid.rb', line 13

def pid_dir
  "#{forklift.config[:project_root]}/pid"
end

#pidfileObject



21
22
23
# File 'lib/forklift/base/pid.rb', line 21

def pidfile
  "#{pid_dir}/pidfile"
end

#recallObject



31
32
33
34
# File 'lib/forklift/base/pid.rb', line 31

def recall
  ensure_pid_dir
  IO.read(pidfile).to_i rescue nil
end

#safe_to_run?Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
# 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!Object



25
26
27
28
29
# 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