Class: Sanford::PIDFile
- Inherits:
-
Object
- Object
- Sanford::PIDFile
- Defined in:
- lib/sanford/pid_file.rb
Constant Summary collapse
- InvalidError =
Class.new(RuntimeError)
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#initialize(path) ⇒ PIDFile
constructor
A new instance of PIDFile.
- #pid ⇒ Object
- #remove ⇒ Object
- #to_s ⇒ Object
- #write ⇒ Object
Constructor Details
#initialize(path) ⇒ PIDFile
Returns a new instance of PIDFile.
8 9 10 |
# File 'lib/sanford/pid_file.rb', line 8 def initialize(path) @path = (path || '/dev/null').to_s end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
6 7 8 |
# File 'lib/sanford/pid_file.rb', line 6 def path @path end |
Instance Method Details
#pid ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/sanford/pid_file.rb', line 12 def pid pid = File.read(@path).strip pid && !pid.empty? ? pid.to_i : raise('no pid in file') rescue StandardError => exception error = InvalidError.new("A PID couldn't be read from #{@path.inspect}") error.set_backtrace(exception.backtrace) raise error end |
#remove ⇒ Object
30 31 32 |
# File 'lib/sanford/pid_file.rb', line 30 def remove FileUtils.rm_f(@path) end |
#to_s ⇒ Object
34 35 36 |
# File 'lib/sanford/pid_file.rb', line 34 def to_s @path end |
#write ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/sanford/pid_file.rb', line 21 def write FileUtils.mkdir_p(File.dirname(@path)) File.open(@path, 'w'){ |f| f.puts ::Process.pid } rescue StandardError => exception error = InvalidError.new("Can't write pid to file #{@path.inspect}") error.set_backtrace(exception.backtrace) raise error end |