Class: VCAP::PidFile
- Inherits:
-
Object
- Object
- VCAP::PidFile
- Defined in:
- lib/vcap/common.rb
Overview
Helper class to atomically create/update pidfiles and ensure that only one instance of a given process is running at all times.
NB: Ruby doesn’t have real destructors so if you want to be polite and clean up after yourself be sure to call unlink() before your process exits.
usage:
begin
pidfile = VCAP::PidFile.new('/tmp/foo')
rescue => e
puts "Error creating pidfile: %s" % (e)
exit(1)
end
Defined Under Namespace
Classes: ProcessRunningError
Instance Method Summary collapse
-
#initialize(pid_file, create_parents = true) ⇒ PidFile
constructor
A new instance of PidFile.
- #to_s ⇒ Object
-
#unlink ⇒ Object
Removes the created pidfile.
- #unlink_at_exit ⇒ Object
-
#unlink_on_signals(*sigs) ⇒ Object
Removes the created pidfile upon receipt of the supplied signals.
Constructor Details
#initialize(pid_file, create_parents = true) ⇒ PidFile
Returns a new instance of PidFile.
142 143 144 145 146 |
# File 'lib/vcap/common.rb', line 142 def initialize(pid_file, create_parents=true) @pid_file = pid_file @dirty = true write(create_parents) end |
Instance Method Details
#to_s ⇒ Object
176 177 178 |
# File 'lib/vcap/common.rb', line 176 def to_s() @pid_file end |
#unlink ⇒ Object
Removes the created pidfile
149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/vcap/common.rb', line 149 def unlink() return unless @dirty # Swallowing exception here is fine. Removing the pid files is a courtesy. begin File.unlink(@pid_file) @dirty = false rescue end self end |
#unlink_at_exit ⇒ Object
171 172 173 174 |
# File 'lib/vcap/common.rb', line 171 def unlink_at_exit() at_exit { unlink() } self end |
#unlink_on_signals(*sigs) ⇒ Object
Removes the created pidfile upon receipt of the supplied signals
162 163 164 165 166 167 168 169 |
# File 'lib/vcap/common.rb', line 162 def unlink_on_signals(*sigs) return unless @dirty sigs.each do |s| Signal.trap(s) { unlink() } end self end |