Skip to content

Commit

Permalink
Add method is_locked(lock_name) to test whether lock is held.
Browse files Browse the repository at this point in the history
Fixes #647
  • Loading branch information
coleifer committed Jan 10, 2022
1 parent c568fdc commit b7aebe4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,11 @@ Huey object
with huey.lock_task('db-backup'):
do_db_backup()
.. py:method:: is_locked(lock_name)
:param str lock_name: Name of lock to check.
:returns: boolean value indicating whether lock is held or not.

.. py:method:: flush_locks(*names)
:param names: additional lock-names to flush.
Expand Down
6 changes: 6 additions & 0 deletions huey/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,9 @@ def flush(self):
def lock_task(self, lock_name):
return TaskLock(self, lock_name)

def is_locked(self, lock_name):
return TaskLock(self, lock_name).is_locked()

def flush_locks(self, *names):
flushed = set()
locks = self._locks
Expand Down Expand Up @@ -852,6 +855,9 @@ def __init__(self, huey, name):
self._key = '%s.lock.%s' % (self._huey.name, self._name)
self._huey._locks.add(self._key)

def is_locked(self):
return self._huey.storage.has_data_for_key(self._key)

def __call__(self, fn):
@wraps(fn)
def inner(*args, **kwargs):
Expand Down
7 changes: 7 additions & 0 deletions huey/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1283,10 +1283,17 @@ def task_a(n):

task_a(4)
with self.huey.lock_task('lock_x'):
self.assertFalse(self.huey.is_locked('lock_a'))
self.assertTrue(self.huey.is_locked('lock_x'))
self.assertEqual(self.execute_next(), 5)

# Ensure locks were cleared.
self.assertFalse(self.huey.is_locked('lock_a'))
self.assertFalse(self.huey.is_locked('lock_x'))

r = task_a(5)
with self.huey.lock_task('lock_a'):
self.assertTrue(self.huey.is_locked('lock_a'))
self.assertTrue(self.execute_next() is None)

exc = self.trap_exception(r)
Expand Down

0 comments on commit b7aebe4

Please sign in to comment.