Class: Nanite::PidFile
Instance Method Summary collapse
- #check ⇒ Object
- #ensure_dir ⇒ Object
- #exists? ⇒ Boolean
-
#initialize(identity, options) ⇒ PidFile
constructor
A new instance of PidFile.
- #read_pid ⇒ Object
- #remove ⇒ Object
- #to_s ⇒ Object
- #write ⇒ Object
Constructor Details
#initialize(identity, options) ⇒ PidFile
Returns a new instance of PidFile.
3 4 5 6 |
# File 'lib/nanite/pid_file.rb', line 3 def initialize(identity, ) @pid_dir = File.([:pid_dir] || [:root] || Dir.pwd) @pid_file = File.join(@pid_dir, "nanite.#{identity}.pid") end |
Instance Method Details
#check ⇒ Object
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/nanite/pid_file.rb', line 8 def check if pid = read_pid if process_running? pid raise "#{@pid_file} already exists (pid: #{pid})" else Log.info "removing stale pid file: #{@pid_file}" remove end end end |
#ensure_dir ⇒ Object
19 20 21 |
# File 'lib/nanite/pid_file.rb', line 19 def ensure_dir FileUtils.mkdir_p @pid_dir end |
#exists? ⇒ Boolean
37 38 39 |
# File 'lib/nanite/pid_file.rb', line 37 def exists? File.exists? @pid_file end |
#read_pid ⇒ Object
33 34 35 |
# File 'lib/nanite/pid_file.rb', line 33 def read_pid open(@pid_file,'r') {|f| f.read.to_i } if exists? end |
#remove ⇒ Object
29 30 31 |
# File 'lib/nanite/pid_file.rb', line 29 def remove File.delete(@pid_file) if exists? end |
#to_s ⇒ Object
41 42 43 |
# File 'lib/nanite/pid_file.rb', line 41 def to_s @pid_file end |
#write ⇒ Object
23 24 25 26 27 |
# File 'lib/nanite/pid_file.rb', line 23 def write ensure_dir open(@pid_file,'w') {|f| f.write(Process.pid) } File.chmod(0644, @pid_file) end |