more or less decent locking

This commit is contained in:
Ward Wouts 2003-05-27 19:12:46 +00:00
parent d55a83664e
commit 8396d7f8cc
2 changed files with 16 additions and 9 deletions

View file

@ -1,4 +1,4 @@
# $Dwarf: TODO,v 1.15 2003/04/28 14:36:19 ward Exp $ # $Dwarf: TODO,v 1.16 2003/05/26 19:35:04 ward Exp $
# $Source$ # $Source$
[ ] check for multiple servers (ip adresses) for each name and pick [ ] check for multiple servers (ip adresses) for each name and pick
@ -20,4 +20,3 @@
exceptions. Use buffering for each body, before writing... exceptions. Use buffering for each body, before writing...
[ ] more regression tests [ ] more regression tests
[ ] update documentation [ ] update documentation
[ ] locking so you won't run 2 ripnews processes at the same time

View file

@ -1,6 +1,6 @@
#!/usr/local/bin/ruby -w #!/usr/local/bin/ruby -w
# $Dwarf: ripnews.rb,v 1.61 2003/05/24 18:33:46 ward Exp $ # $Dwarf: ripnews.rb,v 1.62 2003/05/26 19:35:04 ward Exp $
# $Source$ # $Source$
require 'date' require 'date'
@ -240,15 +240,23 @@ def lock
group = @config.keys[0] group = @config.keys[0]
if @config[group].has_key?("LOCKFILE") if @config[group].has_key?("LOCKFILE")
if FileTest.exists?(@config[group]["LOCKFILE"]) if FileTest.exists?(@config[group]["LOCKFILE"])
# Should also detect stale locks here lock = File.open(@config[group]["LOCKFILE"], "r")
line = lock.gets
lock.close
line.chomp!
psauxw = `ps -auxw`
if /^\S+\s+#{line}\s+/.match(psauxw)
print "Already running, exiting...\n" print "Already running, exiting...\n"
exit exit
else else
print "Stale lock found... removing...\n"
File.unlink(@config[group]["LOCKFILE"])
end
end
lock = File.new(@config[group]["LOCKFILE"], "w") lock = File.new(@config[group]["LOCKFILE"], "w")
lock.print "#{Process.pid}\n" lock.print "#{Process.pid}\n"
lock.close lock.close
end end
end
end end
def unlock def unlock