From ced17ee7dd16622f1e5cf55f838301993f7ac3f7 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Sat, 11 May 2019 19:27:33 +0300 Subject: [PATCH] Feat: Store lock file in a shared directory Use `$UID` as a suffix to separate lock files for different users. --- docs-ru/news.rst | 8 ++++++++ docs/news.rst | 6 ++++++ m_librarian/web/utils.py | 19 ++++++++++++++----- tmp/.gitignore | 2 -- 4 files changed, 28 insertions(+), 7 deletions(-) delete mode 100644 tmp/.gitignore diff --git a/docs-ru/news.rst b/docs-ru/news.rst index d2e2c69..e490c1b 100644 --- a/docs-ru/news.rst +++ b/docs-ru/news.rst @@ -1,6 +1,14 @@ Новости ======= +Версия 0.1.6 (2019-05-??) +------------------------- + +* Поместить файл ``m_librarian.lock`` в общую директорию; + использовать ``$UID`` в качестве суффикса имени файла + чтобы иметь возможность запускать ``m_librarian`` + для разных пользователей. + Версия 0.1.5 (2019-02-01) ------------------------- diff --git a/docs/news.rst b/docs/news.rst index cb3e40b..ec811f0 100644 --- a/docs/news.rst +++ b/docs/news.rst @@ -1,6 +1,12 @@ News ==== +Version 0.1.6 (2019-05-??) +-------------------------- + +* Store lock file in a shared directory; use ``$UID`` as a suffix + to separate lock files for different users. + Version 0.1.5 (2019-02-01) -------------------------- diff --git a/m_librarian/web/utils.py b/m_librarian/web/utils.py index 12be81f..8804f3b 100644 --- a/m_librarian/web/utils.py +++ b/m_librarian/web/utils.py @@ -1,11 +1,20 @@ from fcntl import flock, LOCK_EX, LOCK_UN, LOCK_NB -from os import path, remove +from tempfile import gettempdir +import os import socket -lock_fname = path.join( - path.dirname(path.dirname(path.dirname(__file__))), - 'tmp', 'm_librarian.lock') +if os.access('/var/run/lock', os.W_OK): + lock_dir = '/var/run/lock' +else: + lock_dir = gettempdir() + +if hasattr(os, 'getuid'): + suffix = '-%d' % os.getuid() +else: + suffix = '' + +lock_fname = os.path.join(lock_dir, 'm_librarian%s.lock' % suffix) def get_lock(port): @@ -38,7 +47,7 @@ def close_lock(lock_file): lock_file = open(lock_fname, 'w') lock_file.write('') lock_file.close() - remove(lock_fname) + os.remove(lock_fname) def get_open_port(): diff --git a/tmp/.gitignore b/tmp/.gitignore deleted file mode 100644 index 120f485..0000000 --- a/tmp/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -* -!/.gitignore -- 2.39.5