Class: Nanite::PidFile

Inherits:
Object show all
Defined in:
lib/nanite/pid_file.rb

Instance Method Summary collapse

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, options)
  @pid_dir = File.expand_path(options[:pid_dir] || options[:root] || Dir.pwd)
  @pid_file = File.join(@pid_dir, "nanite.#{identity}.pid")
end

Instance Method Details

#checkObject



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_dirObject



19
20
21
# File 'lib/nanite/pid_file.rb', line 19

def ensure_dir
  FileUtils.mkdir_p @pid_dir
end

#exists?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/nanite/pid_file.rb', line 37

def exists?
  File.exists? @pid_file
end

#read_pidObject



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

#removeObject



29
30
31
# File 'lib/nanite/pid_file.rb', line 29

def remove
  File.delete(@pid_file) if exists?
end

#to_sObject



41
42
43
# File 'lib/nanite/pid_file.rb', line 41

def to_s
  @pid_file
end

#writeObject



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