-
Notifications
You must be signed in to change notification settings - Fork 23
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
What happens if code inside after_commit callback fails? #12
Comments
OK, I think I've just found that the answer is yes, nothing will be rolled back:
https://guides.rubyonrails.org/active_record_callbacks.html#transaction-callbacks But I still wonder if it would be possible to rollback everything that happened in the transaction if something inside some kind of a |
No. You may want to take a look at Sagas pattern or some other mechanism of distributed transactions. There were a few gems that helps to build such workflows, like dirty_pipeline gem (however it is not finished) or others, but I can't recommend anything right now. |
Can you tell a bit more about your |
Thanks a lot for your quick answer! I'll be looking into those solutions!
Yeah, this is basically what
To make sure everything gets rolled back if any of those steps fail it looks like this now: def call
ActiveRecord::Base.transaction do
create_user!
some_important_stuff
end
end I use sidekiq to |
Sending emails and requesting remote APIs definitely should be outside of transaction. I would create some statuses for user (like But setting defaults and creating related objects should be inside transaction that create user (unless it is a result from these remote APIs, of course) So consider to split your |
very cool, I'll let you know how this one played out!
|
Hey there,
in this example:
If something bad inside
some_important_stuff
happens and throws an error, then anything that was persisted increate_user!
won't be rolled back, since when callingafter_commit
we already are not inside the transaction anymore, right?Thank you! :)
The text was updated successfully, but these errors were encountered: