-
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
Question regarding after_commit
behavior upon exception
#34
Comments
Thanks for raising this question (pun intended) In ActiveRecord model, all defined callbacks are belong to one instance, so can be grouped together. In this gem for every This behavior isn't documented or specifically decided to be that one, it just happened to be the current one. We can try to “group” callbacks e.g. by current transaction (so all callbacks from all files will be grouped into a single “record”) or maybe by I'm not sure which behavior is more desired. |
@Envek Thank you for prompt response! In my opinion, it would be better to group callbacks by the current transaction (if there is one like the very first code snippet above). - def committed!(*)
- @handlers[:after_commit]&.call
+ def committed!(should_run_callbacks: true)
+ if should_run_callbacks
+ @handlers[:after_commit]&.call
+ end
end What I suggest is to not running succeeding callbacks when active record tells you not to. |
Makes sense! Pull request are welcome! 😃 |
Another issue with current implementation is that ActiveRecord::Base.transaction do
AfterCommitEverywhere::after_commit { raise "should this raise and stop other callbacks? - 1" }
AfterCommitEverywhere::after_commit { ap "1" }
AfterCommitEverywhere::after_commit { raise "should this raise and stop other callbacks? - 2" }
AfterCommitEverywhere::after_commit { ap "2" }
AfterCommitEverywhere::after_commit { ap "3" }
end this would lead to
The entire |
if executed, you'd get the following
Is this behavior intended (every callback runs independently and doesn't affect the others)?
or should other following callbacks be cancelled and not executed when there is an exception just like rails's after_commit where it says:
and only result in
without numbers being printed?
I noticed that if an exception in one callback needs to be propagated to other callbacks, the following lines of code
after_commit_everywhere/lib/after_commit_everywhere/wrap.rb
Lines 26 to 28 in fbb1e36
should respect the
should_run_callbacks
keyword argument from Active Record.https://github.com/rails/rails/blob/44673c80bde08f17659a709279972827ffa0ae64/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb#L315-L317
cc. @njw1204
The text was updated successfully, but these errors were encountered: