-
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
Needs extra caution when using within models #13
Comments
Using this gem inside models is weird, because models have their own I thought that it is obvious: use built-in However, as this gem uses builtin ActiveRecord's mechanisms (building fake records and registering in AR's after_commit callbacks queue), there shouldn't be any difference between them. Different behavior is a bug, I believe. Please tell me more about your setup:
If you're able to provide executable Gist (like one for reproduction of bugs in ActiveRecord itself) it would be very helpful and appreciated. |
You are absolutely right about after_commit in models. It happened because we were so used to placing after_commit's in services (thank you for the gem!). Actually, the gem itself doesn't even participate in this "bug." Here's a simple gist which shows how it happened (and how it doesn't have to do anything with after_commit_everywhere itself). |
I played with your gist replacing
Thank you for reporting this, it is interesting. |
Oh, no, I was wrong, forget the nonsense that I wrote. Actually there is a problem in following code: class Post < ActiveRecord::Base
def self.bulk_ops
find_each do
after_commit { raise "Some doesn't expect that to happen in future tests, but they should" }
end
end
end This is the same as: class Post < ActiveRecord::Base
after_commit do
raise "Some doesn't expect that to happen in SecondTest, but they should"
end
end So by calling class-level That's why following test cases are failing. As they actually should. |
Added a note to the README |
@Envek Incredible, thank you! |
We were looking at some strange bug where one test was weirdly affecting another - jobs enqueued in one test were seemingly trying to run in another.
As it turned out - there was a class method on the model, used for some bulk updates, which were enqueuing async jobs with after_commit {...} block.
Dev intended to run after_commit only inside of that bulk update method, but it actually was defined for the whole model, because the AR model has its own after_commit method.
Extracting that bulk method out from the AR model into a separate service solved the issue.
Probably makes sense to put some warning in the Readme about usage within AR models?
The text was updated successfully, but these errors were encountered: