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

Cop idea: Mind Transactions #10

Open
ydakuka opened this issue Sep 16, 2023 · 0 comments
Open

Cop idea: Mind Transactions #10

ydakuka opened this issue Sep 16, 2023 · 0 comments

Comments

@ydakuka
Copy link

ydakuka commented Sep 16, 2023

Reference: https://github.com/toptal/active-job-style-guide#mind-transactions

Background processing of a scheduled job may happen sooner than you expect. Make sure to only schedule jobs when the transaction has been committed.

# bad - job may perform earlier than the transaction is committed
User.transaction do
  users_params.each do |user_params|
    user = User.create!(user_params)
    NotifyUserJob.perform_later(user)
  end
end

# good
users = User.transaction do
          users_params.map do |user_params|
            User.create!(user_params)
          end
        end
users.each { |user| NotifyUserJob.perform_later(user) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant