changeset 4202:b26339343076

core: use a user specific directory for PID file: default location of pid file is now specific to logged user, this allow to run several instances of Libervia by different users on the same machine without PID conflicts.
author Goffi <goffi@goffi.org>
date Sun, 14 Jan 2024 17:48:02 +0100
parents 6a8cc6d668a4
children 4af030d4d3d8
files libervia/backend/core/constants.py libervia/backend/core/launcher.py
diffstat 2 files changed, 11 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/libervia/backend/core/constants.py	Thu Dec 14 09:19:32 2023 +0100
+++ b/libervia/backend/core/constants.py	Sun Jan 14 17:48:02 2024 +0100
@@ -21,12 +21,19 @@
     from os.path import expanduser, realpath
 except ImportError:
     BaseDirectory = None
-from os.path import dirname
+from os.path import dirname, join
 import tempfile
 from typing import Final
 
 from libervia import backend
 
+try:
+    from getpass import getuser
+except ImportError:
+    logged_user = "unkonwn"
+else:
+    logged_user = getuser()
+
 
 class Const(object):
 
@@ -458,7 +465,6 @@
         return cls.BOOL_TRUE if value else cls.BOOL_FALSE
 
 
-
 ## Configuration ##
 if (
     BaseDirectory
@@ -489,7 +495,7 @@
                 ).getAbsolutePath(),
                 Const.APP_NAME_FILE,
             ),
-            "pid_dir": tempfile.gettempdir(),
+            "pid_dir": join(tempfile.gettempdir(), Const.APP_NAME_FILE, logged_user),
             "log_dir": "%(local_dir)s",
         }
         Const.CONFIG_FILES = [
@@ -532,7 +538,7 @@
             "media_dir": "/usr/share/" + Const.APP_NAME_FILE + "/media",
             "local_dir": BaseDirectory.save_data_path(Const.APP_NAME_FILE),
             "downloads_dir": "~/Downloads/" + Const.APP_NAME_FILE,
-            "pid_dir": tempfile.gettempdir(),
+            "pid_dir": join(tempfile.gettempdir(), Const.APP_NAME_FILE, logged_user),
             "log_dir": "%(local_dir)s",
         }
 
--- a/libervia/backend/core/launcher.py	Thu Dec 14 09:19:32 2023 +0100
+++ b/libervia/backend/core/launcher.py	Sun Jan 14 17:48:02 2024 +0100
@@ -197,6 +197,7 @@
 
     def get_pid_file(self, config):
         pid_dir = Path(config.get("DEFAULT", "pid_dir")).expanduser()
+        pid_dir.mkdir(parents=True, exist_ok=True)
         return pid_dir / f"{self.APP_NAME_FILE}.pid"
 
     def wait_for_service(