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-'