Class: Forklift::Base::Pid
- Inherits:
-
Object
- Object
- Forklift::Base::Pid
- Defined in:
- lib/forklift/base/pid.rb
Instance Method Summary collapse
- #delete! ⇒ Object
- #ensure_pid_dir ⇒ Object
- #forklift ⇒ Object
-
#initialize(forklift) ⇒ Pid
constructor
A new instance of Pid.
- #pid_dir ⇒ Object
- #pidfile ⇒ Object
- #recall ⇒ Object
- #safe_to_run? ⇒ Boolean
- #store! ⇒ Object
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_dir ⇒ Object
17 18 19 |
# File 'lib/forklift/base/pid.rb', line 17 def ensure_pid_dir `mkdir -p #{pid_dir}` end |
#forklift ⇒ Object
9 10 11 |
# File 'lib/forklift/base/pid.rb', line 9 def forklift @forklift end |
#pid_dir ⇒ Object
13 14 15 |
# File 'lib/forklift/base/pid.rb', line 13 def pid_dir "#{forklift.config[:project_root]}/pid" end |
#pidfile ⇒ Object
21 22 23 |
# File 'lib/forklift/base/pid.rb', line 21 def pidfile "#{pid_dir}/pidfile" end |
#recall ⇒ Object
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
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 |