Skip to content
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

Closed
wants to merge 5 commits into from

Conversation

AlexRiedler
Copy link
Contributor

@AlexRiedler AlexRiedler commented Nov 27, 2023

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 all after_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:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

@AlexRiedler AlexRiedler requested a review from a team as a code owner November 27, 2023 16:05
Copy link
Contributor

@cassiascheffer cassiascheffer left a 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.

lib/rubocop/cop/vendor/disallow_after_commit.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/vendor/disallow_after_commit.rb Outdated Show resolved Hide resolved
Copy link
Contributor Author

@AlexRiedler AlexRiedler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wording changes.

@hannahyeates
Copy link

@AlexRiedler it would be worth calling out that there is not a built-in before_commit callback: https://guides.rubyonrails.org/active_record_callbacks.html#available-callbacks. Can we recommend that people use after_save instead?

@AlexRiedler
Copy link
Contributor Author

@AlexRiedler
Copy link
Contributor Author

There is even a reference to it still in the current rails docs: https://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

Screenshot 2023-11-28 at 4 56 29 PM

@hannahyeates
Copy link

hannahyeates commented Nov 29, 2023

@AlexRiedler I think after_commit is a private method. It doesn't show up in the list of exposed callbacks:
Screenshot 2023-11-29 at 9 35 36 AM. My suspicion that before_commit is private comes from this GH issue: rails/rails#44458

I think the before_commit hook you referenced above is on the ActiveRecord::Transaction class: https://apidock.com/rails/v5.2.3/ActiveRecord/Transactions/ClassMethods/before_commit. The after_commit hook that would be disallowed here is on the ActiveRecord::Callbacks class: https://apidock.com/rails/v5.2.3/ActiveRecord/Callbacks

I saw this PR and tried to swap after_commit for before_commit and it didn't work because they aren't a 1:1 swap from the looks of it. Instead of recommending that we use the transaction level hook instead of the object level hook, can we recommend a safer model hook so that it is an easier fix to make? I'm not sure of the lift on doing this refactor, so if we really want to leverage before commit hooks, maybe we include some documentation for how to make this change?

@hannahyeates
Copy link

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

@AlexRiedler
Copy link
Contributor Author

oh okay, lets just change it to after_create then (updating)

CHANGELOG.md Outdated Show resolved Hide resolved
@AlexRiedler AlexRiedler changed the title feat: disallow after-commit feat: prefer after_create instead of after-commit due to transactional guarentees Nov 29, 2023
@desheikh
Copy link
Contributor

desheikh commented Dec 1, 2023

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.

@AlexRiedler
Copy link
Contributor Author

AlexRiedler commented Dec 1, 2023

@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.

@AlexRiedler AlexRiedler marked this pull request as draft January 23, 2024 05:09
Copy link

github-actions bot commented Mar 1, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

@github-actions github-actions bot added the Stale label Mar 1, 2024
@AlexRiedler AlexRiedler closed this Mar 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

4 participants