[projects/git-slug: 84/170] Remove lock file when slug_watch exits or crashes

glen glen at pld-linux.org
Mon Sep 21 21:45:52 CEST 2015


commit 632fff97f75d16266e3fd7ca27d2fd32649d7b6a
Author: Kacper Kornet <draenog at pld-linux.org>
Date:   Mon Mar 12 07:44:21 2012 +0000

    Remove lock file when slug_watch exits or crashes

 slug_watch | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)
---
diff --git a/slug_watch b/slug_watch
index 0eaf82b..ffbec8e 100755
--- a/slug_watch
+++ b/slug_watch
@@ -7,6 +7,8 @@ import os
 import pwd
 import pyinotify
 
+from contextlib import contextmanager
+
 from git_slug.gitconst import EMPTYSHA1, REFREPO, REFFILE
 from git_slug.serverconst import WATCHDIR
 from git_slug.refsdata import RemoteRefsData
@@ -22,6 +24,21 @@ WATCHDIR=WATCHDIR
 wm = pyinotify.WatchManager()  # Watch Manager
 mask = pyinotify.IN_CLOSE_WRITE # watched events
 
+ at contextmanager
+def lock(path=LOCKFILE):
+    f = open(path, 'a')
+    try:
+        fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
+    except IOError:
+        raise SystemExit('Already running: file {} locked'.format(path))
+    else:
+        try:
+            yield
+        finally:
+            f.close()
+            os.remove(path)
+
+
 def convertstream(stream):
     for line in stream:
         (sha1, ref, repo) = line.decode('utf-8').split()
@@ -80,9 +97,5 @@ parser.add_argument('-u', '--user')
 options = parser.parse_args()
 
 os.chdir(pwd.getpwuid(os.getuid()).pw_dir)
-with open(LOCKFILE, "a") as f:
-    try: 
-        fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
-        run()
-    except IOError:
-        pass
+with lock(LOCKFILE):
+    run()
================================================================

---- gitweb:

http://git.pld-linux.org/gitweb.cgi/projects/git-slug.git/commitdiff/4ed64f73960519a2f4fd04c42950b2c96ae795c5



More information about the pld-cvs-commit mailing list