The post_process() method is skipped if `db_transaction = False`
See original GitHub issueIf you set db_transaction = False
on your service and implement a post_process()
method, the post process method does not execute. While examining the code, it makes sense why because the on transaction commit hook is being used. However, the behavior is a bit obtuse especially if you are not familiar with the internals of the library.
The solution I propose is if db_transaction = False
and a post_process()
is implemented, then automatically execute the post_process()
in sequence after the process()
. This would be useful for services where transaction safety is not needed (like an API call, etc.) and would make the feature in parity when db_transaction = True
.
I do believe it’s as easy as implementing a finally
statement:
@contextmanager
def _process_context(self):
"""
Returns the context for :meth:`process`
:return:
"""
if self.db_transaction:
with transaction.atomic(using=self.using):
if self.has_on_commit_action:
transaction.on_commit(self.post_process)
yield
else:
try:
yield
finally:
if self.has_on_commit_action:
self.post_process()
Note: I would likely rename has_on_commit_action
to has_post_process_action
since it would not be transaction related.
Alternatively, we could throw an exception if you define a post_process when transaction is false however I think making the feature act in parity is better.
Please let me know your thoughts and I’d be happy to make a PR.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Merged in #52, thanks!
I don’t think
post_process
should run if the main part of the process fails. I see the intent ofpost_process
being for things like notifications/syncing/etc which shouldn’t fire if things have failed and the database hasn’t updated.But I do think
post_process
should run regardless of the transaction setting as long as an exception doesn’t make it all the way to the library level.