Monday 7 September 2009

More fun with pid files

Another way of handling pid files is a more ruby like way is:
#!/usr/bin/env ruby

class PidFile
def initialize(filename, &code)
unless set_pid(filename)
raise RuntimeError.new("The process is already running")
end

code.call

File.delete(filename)
end

private

def set_pid(filename)
# Same code as before
end
end

And call it thus
PidFile.new("/var/run/app.pid") do
puts "What we want to do"
end

No comments:

Post a Comment