Class: PidFile
- Inherits:
-
Object
- Object
- PidFile
- Defined in:
- lib/pidfile.rb
Constant Summary collapse
- VERSION =
'0.2.0'- DEFAULT_OPTIONS =
{ :pidfile => File.basename($0, File.extname($0)) + ".pid", :piddir => '/tmp', }
Instance Attribute Summary collapse
-
#piddir ⇒ Object
Returns the value of attribute piddir.
-
#pidfile ⇒ Object
Returns the value of attribute pidfile.
Class Method Summary collapse
-
.pidfile_exists?(path = nil) ⇒ Boolean
class method for determining the existence of pidfile.
-
.running?(path = nil) ⇒ Boolean
boolean stating whether the calling program is already running.
Instance Method Summary collapse
-
#alive? ⇒ Boolean
Boolean stating whether this process is alive and running.
-
#initialize(*args) ⇒ PidFile
constructor
A new instance of PidFile.
-
#locktime ⇒ Object
returns the modification time of the pidfile.
-
#pid ⇒ Object
Returns the PID, if any, of the instantiating process.
-
#pidfile_exists? ⇒ Boolean
does the pidfile exist?.
-
#pidpath ⇒ Object
Returns the fullpath to the file containing the process ID (PID).
-
#release ⇒ Object
unlock and remove the pidfile.
Constructor Details
#initialize(*args) ⇒ PidFile
Returns a new instance of PidFile.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/pidfile.rb', line 11 def initialize(*args) opts = {} case when args.length == 0 then when args.length == 1 && args[0].class == Hash then arg = args.shift if arg.class == Hash opts = arg end else raise ArgumentError, "new() expects hash or hashref as argument" end opts = DEFAULT_OPTIONS.merge opts @piddir = opts[:piddir] @pidfile = opts[:pidfile] @fh = nil create_pidfile at_exit { release } end |
Instance Attribute Details
#piddir ⇒ Object
Returns the value of attribute piddir.
2 3 4 |
# File 'lib/pidfile.rb', line 2 def piddir @piddir end |
#pidfile ⇒ Object
Returns the value of attribute pidfile.
2 3 4 |
# File 'lib/pidfile.rb', line 2 def pidfile @pidfile end |
Class Method Details
.pidfile_exists?(path = nil) ⇒ Boolean
class method for determining the existence of pidfile
80 81 82 83 84 |
# File 'lib/pidfile.rb', line 80 def self.pidfile_exists?(path=nil) path ||= File.join(DEFAULT_OPTIONS[:piddir], DEFAULT_OPTIONS[:pidfile]) File.exists?(path) end |
.running?(path = nil) ⇒ Boolean
boolean stating whether the calling program is already running
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/pidfile.rb', line 87 def self.running?(path=nil) path ||= File.join(DEFAULT_OPTIONS[:piddir], DEFAULT_OPTIONS[:pidfile]) if pidfile_exists?(path) pid = open(path, 'r').read.to_i else pid = nil end return false unless pid && (pid != Process.pid) process_exists?(pid) end |
Instance Method Details
#alive? ⇒ Boolean
Boolean stating whether this process is alive and running
54 55 56 57 58 |
# File 'lib/pidfile.rb', line 54 def alive? return false unless self.pid && (self.pid == Process.pid) self.class.process_exists?(self.pid) end |
#locktime ⇒ Object
returns the modification time of the pidfile
75 76 77 |
# File 'lib/pidfile.rb', line 75 def locktime File.mtime(self.pidpath) end |
#pid ⇒ Object
Returns the PID, if any, of the instantiating process
43 44 45 46 47 48 49 50 51 |
# File 'lib/pidfile.rb', line 43 def pid return @pid unless @pid.nil? if self.pidfile_exists? @pid = open(self.pidpath, 'r').read.to_i else @pid = nil end end |
#pidfile_exists? ⇒ Boolean
does the pidfile exist?
61 62 63 |
# File 'lib/pidfile.rb', line 61 def pidfile_exists? self.class.pidfile_exists?(pidpath) end |
#pidpath ⇒ Object
Returns the fullpath to the file containing the process ID (PID)
38 39 40 |
# File 'lib/pidfile.rb', line 38 def pidpath File.join(@piddir, @pidfile) end |
#release ⇒ Object
unlock and remove the pidfile. Sets pid to nil
66 67 68 69 70 71 72 |
# File 'lib/pidfile.rb', line 66 def release unless @fh.nil? @fh.flock(File::LOCK_UN) remove_pidfile end @pid = nil end |