Class: Pid
- Inherits:
-
Object
- Object
- Pid
- Defined in:
- lib/pid.rb
Class Method Summary collapse
- .cleanup(path) ⇒ Object
- .cleanup!(path) ⇒ Object
- .drop(path) ⇒ Object
- .running?(pid_or_filepath) ⇒ Boolean
Instance Method Summary collapse
- #cleanup ⇒ Object (also: #zap)
- #ensure_stopped! ⇒ Object
- #exists? ⇒ Boolean
-
#initialize(path) ⇒ Pid
constructor
A new instance of Pid.
-
#pid ⇒ Object
Return the pid contained in the pidfile, or nil.
-
#running? ⇒ Boolean
Returns true if the process is running.
- #write! ⇒ Object
Constructor Details
#initialize(path) ⇒ Pid
Returns a new instance of Pid.
7 8 9 |
# File 'lib/pid.rb', line 7 def initialize(path) @path = path.to_absolute_path end |
Class Method Details
.cleanup(path) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/pid.rb', line 23 def self.cleanup(path) p = self.new(path) if p.running? return false else p.cleanup return true end end |
.cleanup!(path) ⇒ Object
33 34 35 36 |
# File 'lib/pid.rb', line 33 def self.cleanup!(path) p = self.new(path) p.cleanup if p.exists? end |
.drop(path) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/pid.rb', line 11 def self.drop(path) p = self.new(path) if p.exists? unless p.running? p.cleanup p.write! end else p.write! end end |
.running?(pid_or_filepath) ⇒ Boolean
38 39 40 41 |
# File 'lib/pid.rb', line 38 def self.running?(pid_or_filepath) object = self.new(pid_or_filepath) object.running? end |
Instance Method Details
#cleanup ⇒ Object Also known as: zap
81 82 83 84 85 86 87 |
# File 'lib/pid.rb', line 81 def cleanup begin File.delete(@path) rescue Errno::ENOENT File.delete("/tmp/#{Pathname.new(@path).basename}") end end |
#ensure_stopped! ⇒ Object
74 75 76 77 78 79 |
# File 'lib/pid.rb', line 74 def ensure_stopped! if self.running? puts "Process already running with id #{self.pid}" exit 1 end end |
#exists? ⇒ Boolean
43 44 45 |
# File 'lib/pid.rb', line 43 def exists? File.exists?(@path) end |
#pid ⇒ Object
Return the pid contained in the pidfile, or nil
68 69 70 71 72 |
# File 'lib/pid.rb', line 68 def pid return nil unless self.exists? File.open( @path ) { |f| return f.gets.to_i } end |
#running? ⇒ Boolean
Returns true if the process is running
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/pid.rb', line 48 def running? return false unless self.exists? # Check if process is in existence # The simplest way to do this is to send signal '0' # (which is a single system call) that doesn't actually # send a signal begin Process.kill(0, self.pid) return true rescue Errno::ESRCH return false rescue ::Exception # for example on EPERM (process exists but does not belong to us) return true #rescue Errno::EPERM # return false end end |
#write! ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/pid.rb', line 90 def write! begin File.open(@path, "w") { |f| f.puts Process.pid } rescue Errno::ENOENT, Errno::EACCES File.open("/tmp/#{Pathname.new(@path).basename}", "w") { |f| f.puts Process.pid } end end |