summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZuul <zuul@review.opendev.org>2019-11-29 16:14:06 +0000
committerGerrit Code Review <review@openstack.org>2019-11-29 16:14:06 +0000
commit34c0ed87b68520e2094b5498b18974e5ea28bbc2 (patch)
treefc06ee137d8a6c86b51fe8d5de8e3f64f8ef098c
parent168b83b35da9b444a1f2c76554a3dff6fa694312 (diff)
parentfec03875e93b6c047c46bac5a503af8a32b7710f (diff)
downloadoslo-concurrency-34c0ed87b68520e2094b5498b18974e5ea28bbc2.tar.gz
Merge "Add lock_with_prefix convenience utility"
-rw-r--r--oslo_concurrency/lockutils.py23
-rw-r--r--oslo_concurrency/tests/unit/test_lockutils.py9
2 files changed, 32 insertions, 0 deletions
diff --git a/oslo_concurrency/lockutils.py b/oslo_concurrency/lockutils.py
index 7169ee4..ba0d0a5 100644
--- a/oslo_concurrency/lockutils.py
+++ b/oslo_concurrency/lockutils.py
@@ -281,6 +281,29 @@ def lock(name, lock_file_prefix=None, external=False, lock_path=None,
LOG.debug('Releasing lock "%(lock)s"', {'lock': name})
+def lock_with_prefix(lock_file_prefix):
+ """Partial object generator for the lock context manager.
+
+ Redefine lock in each project like so::
+
+ (in nova/utils.py)
+ from oslo_concurrency import lockutils
+
+ lock = lockutils.lock_with_prefix('nova-')
+
+
+ (in nova/foo.py)
+ from nova import utils
+
+ with utils.lock('mylock'):
+ ...
+
+ The lock_file_prefix argument is used to provide lock files on disk with a
+ meaningful prefix.
+ """
+ return functools.partial(lock, lock_file_prefix=lock_file_prefix)
+
+
def synchronized(name, lock_file_prefix=None, external=False, lock_path=None,
semaphores=None, delay=0.01, fair=False):
"""Synchronization decorator.
diff --git a/oslo_concurrency/tests/unit/test_lockutils.py b/oslo_concurrency/tests/unit/test_lockutils.py
index b124963..fee32bb 100644
--- a/oslo_concurrency/tests/unit/test_lockutils.py
+++ b/oslo_concurrency/tests/unit/test_lockutils.py
@@ -227,6 +227,15 @@ class LockTestCase(test_base.BaseTestCase):
self._do_test_lock_externally()
+ def test_lock_with_prefix(self):
+ # TODO(efried): Embetter this test
+ self.config(lock_path=tempfile.mkdtemp(), group='oslo_concurrency')
+ foo = lockutils.lock_with_prefix('mypfix-')
+
+ with foo('mylock', external=True):
+ # We can't check much
+ pass
+
def test_synchronized_with_prefix(self):
lock_name = 'mylock'
lock_pfix = 'mypfix-'