diff options
author | Eric Fried <openstack@fried.cc> | 2019-09-17 16:18:12 -0500 |
---|---|---|
committer | Eric Fried <openstack@fried.cc> | 2019-09-17 16:23:01 -0500 |
commit | fec03875e93b6c047c46bac5a503af8a32b7710f (patch) | |
tree | 2b9245329055ab5e0969e8b28b581adfd24ce4ae /oslo_concurrency | |
parent | fe86f5e4ae8aee42d685dcedb574b95a5c5ce85a (diff) | |
download | oslo-concurrency-fec03875e93b6c047c46bac5a503af8a32b7710f.tar.gz |
Add lock_with_prefix convenience utility
There's a convenience wrapper around the lockutils.synchronized
decorator that lets oslo.concurrency consumers set up a prefix to use
for all lock files (presumably to obviate collisions with other
consumers when using jejune lock names, like 'lock'). This commit adds
an equivalent wrapper around lockutils.lock, the context manager
counterpart to the lockutils.synchronized decorator.
Note that the unit test added for lock_with_prefix is pretty bare; but
it follows the precedent set by the existing tests. Future commits
should make all these tests more thorough/robust.
Change-Id: I4e723ee3be1e57c543684390b607c84388c6e930
Diffstat (limited to 'oslo_concurrency')
-rw-r--r-- | oslo_concurrency/lockutils.py | 23 | ||||
-rw-r--r-- | oslo_concurrency/tests/unit/test_lockutils.py | 9 |
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-' |