New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New Django task with transaction atomic, return None
instead of the task UUID
#8976
Comments
None
instead of the task UUID
Where is it? |
oh no sorry... my fault |
I think that django if use the decorator instead the on_commit function? @transaction.atomic
def delay(.....)
@transaction.atomic
def apply_async(.....) |
Yes, that's expected behaviour, Your solution to use def create_user(request):
with transaction.atomic():
user = User.objects.create(first_name="bob", last_name="doe")
send_welcome_email.delay_on_commit(user.pk)
# later in the transaction, do something that cause the transaction to be rolledback
# e.g. set the email to a value that already exists, raising IntegrityError
user.email = "bob.doe@example.com"
user.save() # <- raises IntegrityError, and rollback the whole transaction When the whole transaction is rolled back, the whole operation failed, and user is NOT created in the DB, so we shouldn't send the welcome email. Since the transaction wasn't commited, the task never triggered (and no UUID was generated). Now if we replace the |
I confirm that In [9]: @app.task
...: def sum_number(n1, n2):
...: print(n1+n2)
...:
In [10]: task = sum_number.delay(1,2)
In [11]: task
Out[11]: <AsyncResult: 3a4138ec-35d7-4d46-808f-3f73a1dfee53>
In [12]: task = sum_number.delay_on_commit(1,2)
In [13]: task
In [14]: assert task is None
In [15]: task is None
Out[15]: True |
Ohh correctly...so if I need the task UUID just use mandatory the |
Correct, yes |
we can develop the way to wait for the end of the transaction and return the task UUID? |
That kind of defeat the whole purpose of the Which essentially calls this: The function is NOT executed at this point, it's basically added to a list of callbacks (line 732): self.run_on_commit.append((set(self.savepoint_ids), func, robust)) This list is read and executed in That may happen in a completely different context that where you trigger the task. |
Let me know if anything is unclear, it would be good to incorporate explanations to lift any doubts you may have in the Celery docs... |
So... it's a way for not waiting the finish of the transaction, correctly? In this |
Let me know if that helps: #8984 |
LGTM - Check out my comments in the PR. |
Sure!! 👍🏻 |
Checklist
main
branch of Celery.contribution guide
on reporting bugs.
for similar or identical bug reports.
for existing proposed fixes.
to find out if the bug was already fixed in the main branch.
in this issue (If there are none, check this box anyway).
Mandatory Debugging Information
celery -A proj report
in the issue.(if you are not able to do this, then at least specify the Celery
version affected).
main
branch of Celery.pip freeze
in the issue.to reproduce this bug.
Optional Debugging Information
and/or implementation.
result backend.
broker and/or result backend.
ETA/Countdown & rate limits disabled.
and/or upgrading Celery and its dependencies.
Related Issues and Possible Duplicates
Related Issues
Possible Duplicates
Environment & Settings
Celery version:
5.4.0
celery report
Output:Steps to Reproduce
Required Dependencies
Python Packages
pip freeze
Output:Other Dependencies
N/A
Minimally Reproducible Test Case
Expected Behavior
Return the correct task uuid like before the release 5.4.0
Actual Behavior
Issue⚠️
`AttributeError: 'NoneType' object has no attribute 'id'
Literally basic minimal task 😄 , but the
delay_on_commit
not return the task uuid forAsyncResult
, and in my case django raise excpetion (rightly) AttributeError -> maybe the task withdelay_on_commit
returnNone
?docs:
The text was updated successfully, but these errors were encountered: