aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Weth <git@jonathanweth.de>2020-07-30 16:09:10 +0200
committerJonathan Weth <git@jonathanweth.de>2020-07-30 16:09:10 +0200
commit4747c039084167143b9c567c9b4a69efef28c1f1 (patch)
treee0620daa73bed49d1bc82fbce7894f52da39ba55
parent367a88ffa591219f6e7028a6a544e521531dee8f (diff)
downloadRWA.Support.SessionService-4747c039084167143b9c567c9b4a69efef28c1f1.tar.gz
RWA.Support.SessionService-4747c039084167143b9c567c9b4a69efef28c1f1.tar.bz2
RWA.Support.SessionService-4747c039084167143b9c567c9b4a69efef28c1f1.zip
Stop service if lock file was removed
-rw-r--r--lock.py25
-rwxr-xr-xservice.py24
2 files changed, 41 insertions, 8 deletions
diff --git a/lock.py b/lock.py
new file mode 100644
index 0000000..8be0651
--- /dev/null
+++ b/lock.py
@@ -0,0 +1,25 @@
+import os
+import tempfile
+
+lock_dir_path = os.path.join(tempfile.gettempdir(), "rwa-session-service")
+lock_file_path = os.path.join(lock_dir_path, "rwa-session-service.lock")
+
+
+def lock():
+ """Create lock file."""
+ os.makedirs(lock_dir_path, exist_ok=True)
+ with open(lock_file_path, "w") as f:
+ f.write("lock")
+
+
+def is_locked():
+ """Check if the lock file exists."""
+ return os.path.exists(lock_file_path)
+
+
+def unlock():
+ """Remove the lock file."""
+ try:
+ os.remove(lock_file_path)
+ except FileNotFoundError:
+ pass
diff --git a/service.py b/service.py
index 339008e..daa35e5 100755
--- a/service.py
+++ b/service.py
@@ -14,6 +14,7 @@ import dbus.mainloop.glib
import dbus.service
from gi.repository import GLib
+from lock import is_locked, lock, unlock
from session import Session
from trigger import TriggerServerThread
@@ -41,6 +42,9 @@ class RWAService(dbus.service.Object):
self.bus = dbus.SessionBus()
name = dbus.service.BusName("org.ArcticaProject.RWA", bus=self.bus)
+ self.check_lock_thread = Thread(target=self._check_lock)
+ self.check_lock_thread.start()
+
self.trigger_service = TriggerServerThread(self._trigger)
self.trigger_service.start()
@@ -219,6 +223,15 @@ class RWAService(dbus.service.Object):
self.trigger_service.shutdown()
self.loop.quit()
+ def _check_lock(self):
+ """Check if lock file exists."""
+ while True:
+ if not is_locked():
+ logging.error("The lock file was removed, so stop this service.")
+ self._stop_all()
+ break
+ time.sleep(1)
+
def str2bool(v: Union[str, bool, int]) -> bool:
"""Return true or false if the given string can be interpreted as a boolean otherwise raise an exception."""
@@ -233,18 +246,13 @@ def str2bool(v: Union[str, bool, int]) -> bool:
if __name__ == "__main__":
- lock_dir_path = os.path.join(tempfile.gettempdir(), "rwa-session-service")
- os.makedirs(lock_dir_path, exist_ok=True)
- lock_file_path = os.path.join(lock_dir_path, "rwa-session-service.lock")
-
# Check for lock file
- if os.path.exists(lock_file_path):
+ if is_locked():
logging.error("The service is already running.")
exit(1)
# Create lock file
- with open(lock_file_path, "w") as f:
- f.write("lock")
+ lock()
parser = argparse.ArgumentParser(description="D-Bus Session Service for RWA")
parser.add_argument(
@@ -289,4 +297,4 @@ if __name__ == "__main__":
loop.run()
logging.info("Remove lock file ...")
- os.remove(lock_file_path)
+ unlock()