Class: Opener::Daemons::Pidfile
- Inherits:
-
Object
- Object
- Opener::Daemons::Pidfile
- Defined in:
- lib/opener/daemons/pidfile.rb
Overview
Class for writing and retrieving PID files as well as managing the associated process.
Instance Attribute Summary collapse
-
#path ⇒ String
readonly
The path to store the PID in.
Instance Method Summary collapse
-
#alive? ⇒ TrueClass|FalseClass
Returns ‘true` if the associated process is alive.
-
#initialize(path) ⇒ Pidfile
constructor
A new instance of Pidfile.
-
#read ⇒ Fixnum
Reads and returns the process ID.
-
#terminate ⇒ Object
Terminates the process by sending it the TERM signal and waits for it to shut down.
-
#unlink ⇒ Object
Removes the associated file, if it exists.
-
#write(id) ⇒ Object
Writes the given process ID to ‘@path`.
Constructor Details
#initialize(path) ⇒ Pidfile
Returns a new instance of Pidfile.
17 18 19 |
# File 'lib/opener/daemons/pidfile.rb', line 17 def initialize(path) @path = path end |
Instance Attribute Details
#path ⇒ String (readonly)
The path to store the PID in.
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/opener/daemons/pidfile.rb', line 11 class Pidfile attr_reader :path ## # @param [String] path # def initialize(path) @path = path end ## # Writes the given process ID to `@path`. # # @param [Fixnum] id # def write(id) File.open(path, 'w') do |handle| handle.write(id.to_s) end # Kill the process immediately if we couldn't write the PID rescue Errno::ENOENT, Errno::EPERM => error Process.kill('KILL', id) end ## # Reads and returns the process ID. # # @return [Fixnum] # def read return File.read(path).to_i end ## # Removes the associated file, if it exists. # def unlink File.unlink(path) if File.file?(path) end ## # Terminates the process by sending it the TERM signal and waits for it to # shut down. # def terminate id = read begin Process.kill('TERM', id) Process.wait(id) rescue Errno::ESRCH, Errno::ECHILD # Process terminated, yay. Any other error is re-raised. end end ## # Returns `true` if the associated process is alive. # # @return [TrueClass|FalseClass] # def alive? id = read begin Process.kill(0, id) return true rescue Errno::ESRCH, Errno::EPERM return false end end end |
Instance Method Details
#alive? ⇒ TrueClass|FalseClass
Returns ‘true` if the associated process is alive.
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/opener/daemons/pidfile.rb', line 72 def alive? id = read begin Process.kill(0, id) return true rescue Errno::ESRCH, Errno::EPERM return false end end |
#read ⇒ Fixnum
Reads and returns the process ID.
41 42 43 |
# File 'lib/opener/daemons/pidfile.rb', line 41 def read return File.read(path).to_i end |
#terminate ⇒ Object
Terminates the process by sending it the TERM signal and waits for it to shut down.
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/opener/daemons/pidfile.rb', line 56 def terminate id = read begin Process.kill('TERM', id) Process.wait(id) rescue Errno::ESRCH, Errno::ECHILD # Process terminated, yay. Any other error is re-raised. end end |
#unlink ⇒ Object
Removes the associated file, if it exists.
48 49 50 |
# File 'lib/opener/daemons/pidfile.rb', line 48 def unlink File.unlink(path) if File.file?(path) end |
#write(id) ⇒ Object
Writes the given process ID to ‘@path`.
26 27 28 29 30 31 32 33 34 |
# File 'lib/opener/daemons/pidfile.rb', line 26 def write(id) File.open(path, 'w') do |handle| handle.write(id.to_s) end # Kill the process immediately if we couldn't write the PID rescue Errno::ENOENT, Errno::EPERM => error Process.kill('KILL', id) end |