diff --git a/python_dynamodb_lock/python_dynamodb_lock.py b/python_dynamodb_lock/python_dynamodb_lock.py index 5dbeec9a..f91a5f2a 100644 --- a/python_dynamodb_lock/python_dynamodb_lock.py +++ b/python_dynamodb_lock/python_dynamodb_lock.py @@ -348,6 +348,7 @@ def acquire_lock(self, retry_timeout=None, additional_attributes=None, app_callback=None, + raise_context_exception=False, ): """ Acquires a distributed DynaomDBLock for the given key(s). @@ -385,6 +386,7 @@ def acquire_lock(self, :param dict additional_attributes: Arbitrary application metadata to be stored with the lock :param Callable app_callback: Callback function that can be used to notify the app of lock entering the danger period, or an unexpected release + :param bool raise_context_exception: Allow exception in the context to be raised :rtype: DynamoDBLock :return: A distributed lock instance """ @@ -405,6 +407,7 @@ def acquire_lock(self, additional_attributes=additional_attributes, app_callback=app_callback, lock_client=self, + raise_context_exception=raise_context_exception, ) start_time = time.monotonic() @@ -838,6 +841,7 @@ def __init__(self, additional_attributes, app_callback, lock_client, + raise_context_exception, ): """ :param str partition_key: The primary lock identifier @@ -851,6 +855,7 @@ def __init__(self, :param Callable app_callback: Callback function that can be used to notify the app of lock entering the danger period, or an unexpected release :param DynamoDBLockClient lock_client: The client that "owns" this lock + :param bool raise_context_exception: Allow exception in the context to be raised """ BaseDynamoDBLock.__init__(self, partition_key, @@ -863,6 +868,7 @@ def __init__(self, ) self.app_callback = app_callback self.lock_client = lock_client + self.raise_context_exception = raise_context_exception # additional properties self.last_updated_time = time.monotonic() self.thread_lock = threading.RLock() @@ -883,7 +889,8 @@ def __exit__(self, exc_type, exc_value, traceback): """ logger.debug('Exiting: %s', self.unique_identifier) self.release(best_effort=True) - return True + if not self.raise_context_exception: + return True def release(self, best_effort=True): diff --git a/setup.py b/setup.py index 26e59982..07da945f 100644 --- a/setup.py +++ b/setup.py @@ -44,6 +44,6 @@ test_suite='tests', tests_require=test_requirements, url='https://github.com/mohankishore/python_dynamodb_lock', - version='0.9.1', + version='0.9.2', zip_safe=False, )