Skip to content

Commit 00aa374

Browse files
Yi ChenYi Chen
authored andcommitted
add an option to raise context exception
need an option to raise error back to the client
1 parent 52ba389 commit 00aa374

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

python_dynamodb_lock/python_dynamodb_lock.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ def acquire_lock(self,
348348
retry_timeout=None,
349349
additional_attributes=None,
350350
app_callback=None,
351+
raise_context_exception=False,
351352
):
352353
"""
353354
Acquires a distributed DynaomDBLock for the given key(s).
@@ -385,6 +386,7 @@ def acquire_lock(self,
385386
:param dict additional_attributes: Arbitrary application metadata to be stored with the lock
386387
:param Callable app_callback: Callback function that can be used to notify the app of lock entering
387388
the danger period, or an unexpected release
389+
:param bool raise_context_exception: Allow exception in the context to be raised
388390
:rtype: DynamoDBLock
389391
:return: A distributed lock instance
390392
"""
@@ -405,6 +407,7 @@ def acquire_lock(self,
405407
additional_attributes=additional_attributes,
406408
app_callback=app_callback,
407409
lock_client=self,
410+
raise_context_exception=raise_context_exception,
408411
)
409412

410413
start_time = time.monotonic()
@@ -838,6 +841,7 @@ def __init__(self,
838841
additional_attributes,
839842
app_callback,
840843
lock_client,
844+
raise_context_exception,
841845
):
842846
"""
843847
:param str partition_key: The primary lock identifier
@@ -851,6 +855,7 @@ def __init__(self,
851855
:param Callable app_callback: Callback function that can be used to notify the app of lock entering
852856
the danger period, or an unexpected release
853857
:param DynamoDBLockClient lock_client: The client that "owns" this lock
858+
:param bool raise_context_exception: Allow exception in the context to be raised
854859
"""
855860
BaseDynamoDBLock.__init__(self,
856861
partition_key,
@@ -863,6 +868,7 @@ def __init__(self,
863868
)
864869
self.app_callback = app_callback
865870
self.lock_client = lock_client
871+
self.raise_context_exception = raise_context_exception
866872
# additional properties
867873
self.last_updated_time = time.monotonic()
868874
self.thread_lock = threading.RLock()
@@ -883,7 +889,8 @@ def __exit__(self, exc_type, exc_value, traceback):
883889
"""
884890
logger.debug('Exiting: %s', self.unique_identifier)
885891
self.release(best_effort=True)
886-
return True
892+
if not self.raise_context_exception:
893+
return True
887894

888895

889896
def release(self, best_effort=True):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@
4444
test_suite='tests',
4545
tests_require=test_requirements,
4646
url='https://github.com/mohankishore/python_dynamodb_lock',
47-
version='0.9.1',
47+
version='0.9.2',
4848
zip_safe=False,
4949
)

0 commit comments

Comments
 (0)