Class: SimplePid
- Inherits:
-
Object
- Object
- SimplePid
- Defined in:
- lib/simple_pid.rb
Class Method Summary collapse
Instance Method Summary collapse
- #cleanup ⇒ Object (also: #zap)
- #ensure_stopped! ⇒ Object
- #exists? ⇒ Boolean
-
#initialize(path) ⇒ SimplePid
constructor
A new instance of SimplePid.
-
#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) ⇒ SimplePid
Returns a new instance of SimplePid.
7 8 9 |
# File 'lib/simple_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/simple_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/simple_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/simple_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 |
Instance Method Details
#cleanup ⇒ Object Also known as: zap
76 77 78 79 80 81 82 |
# File 'lib/simple_pid.rb', line 76 def cleanup begin File.delete(@path) rescue Errno::ENOENT File.delete("/tmp/#{Pathname.new(@path).basename}") end end |
#ensure_stopped! ⇒ Object
69 70 71 72 73 74 |
# File 'lib/simple_pid.rb', line 69 def ensure_stopped! if self.running? puts "Process already running with id #{self.pid}" exit 1 end end |
#exists? ⇒ Boolean
38 39 40 |
# File 'lib/simple_pid.rb', line 38 def exists? File.exists?(@path) end |
#pid ⇒ Object
Return the pid contained in the pidfile, or nil
63 64 65 66 67 |
# File 'lib/simple_pid.rb', line 63 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
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/simple_pid.rb', line 43 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
85 86 87 88 89 90 91 |
# File 'lib/simple_pid.rb', line 85 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 |