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

Allow to register transaction callbacks outside of a record #51474

Merged
merged 1 commit into from Apr 3, 2024

Commits on Apr 3, 2024

  1. Allow to register transaction callbacks outside of a record

    Ref: rails#26103
    Ref: rails#51426
    
    A fairly common mistake with Rails is to enqueue a job from inside a
    transaction, and a record as argumemnt, which then lead to a RecordNotFound
    error when picked up by the queue.
    
    This is even one of the arguments advanced for job runners backed by the
    database such as `solid_queue`, `delayed_job` or `good_job`. But relying
    on this is undesirable iin my opinion as it makes the Active Job abstraction
    leaky, and if in the future you need to migrate to another backend or even
    just move the queue to a separate database, you may experience a lot of race
    conditions of the sort.
    
    But more generally, being able to defer work to after the current transaction
    has been a missing feature of Active Record. Right now the only way to do it
    is from a model callback, and this forces moving things in Active Record
    models that sometimes are better done elsewhere. Even as a self-proclaimed
    "service object skeptic", I often wanted this capability over the last decade,
    and I'm sure it got asked or desired by many more people.
    
    Also there's some 3rd party gems adding this capability using monkey patches.
    It's not a reason to upstream the capability, but it's a proof that there is
    demand for it.
    
    Implementation wise, this proof of concept shows that it's not really hard to
    implement, even with nested multi-db transactions support.
    
    Co-Authored-By: Cristian Bica <cristian.bica@gmail.com>
    byroot and cristianbica committed Apr 3, 2024
    Configuration menu
    Copy the full SHA
    c2df237 View commit details
    Browse the repository at this point in the history