-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: prefer after_create instead of after-commit due to transactional guarentees #97
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wording comments. Lint rules should be written in imperative language to make it clear that they are (almost) never optional.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wording changes.
Co-authored-by: Cassidy Scheffer <[email protected]>
@AlexRiedler it would be worth calling out that there is not a built-in |
There is even a reference to it still in the current rails docs: https://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html |
@AlexRiedler I think I think the I saw this PR and tried to swap |
I think I'm wrong about the private method thing because the latest docs have it public: https://apidock.com/rails/v5.2.3/ActiveRecord/Transactions/ClassMethods/before_commit |
oh okay, lets just change it to after_create then (updating) |
A note here, devs cannot just switch after_commit to after_save and expect things to continue working exactly the same. In the case of triggering jobs after a model update, there is a high chance the worker gets instantiated before the transaction completes and uses a stale version of the model. This is the reason folks landed on after_commit in the first place. I'm afraid the cops doesn't make that clear and there's a good chance it'll result in more breaking behaviour. |
@desheikh good point; maybe there is a better way to word this hmmm...; or a way to link to something with good details around this cause it is nuance. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
Generally
after_commit
can be dangerous as it means its completely non-transactional.With the switch to using postgres backed job queue library (instead of redis); it now makes sense to queue the job within the commit in a
after_create
. Generally allafter_commit
has no guarantee of running and only things like logging really make sense in them (or anything that can be removed without consequence).Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).and description in grammatically correct, complete sentences.
bundle exec rake default
. It executes all tests and RuboCop for itself, and generates the documentation.