From ff65bbde146f792897f0274094097733e67020dc Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Sat, 2 May 2020 12:16:17 +0000 Subject: [PATCH] Implement native lock in hallmonitor --- server/hallmonitor.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/server/hallmonitor.py b/server/hallmonitor.py index 2c134f1..1f1f5a3 100644 --- a/server/hallmonitor.py +++ b/server/hallmonitor.py @@ -7,6 +7,11 @@ import configparser import handler import time import glob +import ftncl +import signal +import errno + +from contextlib import contextmanager processes = {} @@ -57,5 +62,33 @@ def run(): finally: time.sleep(30) +@contextmanager +def timeout(seconds): + def timeout_handler(signum, frame): + pass + + original_handler = signal.signal(signal.SIGALRM, timeout_handler) + + try: + signal.alarm(seconds) + yield + + finally: + signal.alarm(0) + signal.signal(signal.SIGALRM, original_handler) + +def create_lock(): + with timeout(1): + fileobj = open('/tmp/panosteal/monitor.lock', 'w+') + try: + fcntl.flock(fileobj.fileno(), fcntl.LOCK_EX) + except IOError as e: + if e.errno != errno.EINTR: + raise e + exit("Already running - exiting") + return fileobj + exit("An error has occurred - exiting") + if __name__ == "__main__": + create_lock() run()