diff options
Diffstat (limited to 'gitautodeploy')
-rw-r--r-- | gitautodeploy/gitautodeploy.py | 20 | ||||
-rw-r--r-- | gitautodeploy/lock.py | 35 |
2 files changed, 30 insertions, 25 deletions
diff --git a/gitautodeploy/gitautodeploy.py b/gitautodeploy/gitautodeploy.py index f24ec6e..a520a91 100644 --- a/gitautodeploy/gitautodeploy.py +++ b/gitautodeploy/gitautodeploy.py @@ -160,7 +160,13 @@ class GitAutoDeploy(object): logger.warning('No process is currently using port %s.' % self._config['port']) return False - os.kill(pid, signal.SIGKILL) + if hasattr(signal, SIGKILL): + os.kill(pid, signal.SIGKILL) + elif hasattr(signal, SIGHUP): + os.kill(pid, signal.SIGHUP) + else: + os.kill(pid, 1) + return True def create_pid_file(self): @@ -410,10 +416,14 @@ def main(): app = GitAutoDeploy() - signal.signal(signal.SIGHUP, app.signal_handler) - signal.signal(signal.SIGINT, app.signal_handler) - signal.signal(signal.SIGABRT, app.signal_handler) - signal.signal(signal.SIGPIPE, signal.SIG_IGN) + if hasattr(signal, SIGHUP): + signal.signal(signal.SIGHUP, app.signal_handler) + if hasattr(signal, SIGINT): + signal.signal(signal.SIGINT, app.signal_handler) + if hasattr(signal, SIGABRT): + signal.signal(signal.SIGABRT, app.signal_handler) + if hasattr(signal, SIGPIPE) and hasattr(signal, SIG_IGN): + signal.signal(signal.SIGPIPE, signal.SIG_IGN) config = get_config_defaults() diff --git a/gitautodeploy/lock.py b/gitautodeploy/lock.py index b0b408b..f894bb3 100644 --- a/gitautodeploy/lock.py +++ b/gitautodeploy/lock.py @@ -1,12 +1,15 @@ +from lockfile import LockFile + class Lock(): """Simple implementation of a mutex lock using the file systems. Works on *nix systems.""" path = None - _has_lock = False + lock = None def __init__(self, path): self.path = path + self.lock = LockFile(path) def obtain(self): import os @@ -14,39 +17,31 @@ class Lock(): logger = logging.getLogger() try: - os.open(self.path, os.O_CREAT | os.O_EXCL | os.O_WRONLY) - self._has_lock = True + self.lock.acquire(0) logger.debug("Successfully obtained lock: %s" % self.path) - except OSError: + except AlreadyLocked: return False - else: - return True + + return True def release(self): import os import logging logger = logging.getLogger() - if not self._has_lock: + if not self.has_lock(): raise Exception("Unable to release lock that is owned by another process") - try: - os.remove(self.path) - logger.debug("Successfully released lock: %s" % self.path) - finally: - self._has_lock = False + + self.lock.release() + logger.debug("Successfully released lock: %s" % self.path) def has_lock(self): - return self._has_lock + return self.lock.i_am_locking() def clear(self): import os import logging logger = logging.getLogger() - try: - os.remove(self.path) - except OSError: - pass - finally: - logger.debug("Successfully cleared lock: %s" % self.path) - self._has_lock = False
\ No newline at end of file + self.lock.break_lock() + logger.debug("Successfully cleared lock: %s" % self.path) |