Skip to content

ActiveRecord Transaction on_commit / on_rollback callbacks - #26103

Closed
cristianbica wants to merge 1 commit into
rails:masterfrom
cristianbica:ar-transaction-callbacks
Closed

cristianbica wants to merge 1 commit into
rails:masterfrom
cristianbica:ar-transaction-callbacks

Conversation

@cristianbica

Copy link
Copy Markdown
Member

This adds on_commit / on_rollback callbacks to ActiveRecord's transactions. The way this works is:

  • transactions callbacks are stored on ActiveRecord::Transaction
  • rollback callbacks are called when the rollback has occurred (in nested transaction callbacks are called when the inner transaction rolls back)
  • commit callbacks are called after the actual commit is issued to the database (in nested transactions are called when the outer transaction commits)
  • commit callbacks are called immediately when there's no open transactions
  • rollback callbacks are ignored when there's no open transaction

Example:

MyModel.transaction do
  MyModel.connection.on_commit do
    # do somthing
  end
end # here it will call the on_commit block

The use case for this functionality is related to ActiveJob. One of the sources of errors when working with background jobs is that the job is executed before some data is committed to the database as illustrated here #26045 by DHH. While I initially wanted to implement the behavior as DHH suggested I've talked to @matthewd and he gave me a great idea: why don't we defer the job enqueueing until the transaction is committed. But we needed commit/rollback support on transaction so here it is!

@sgrif

sgrif commented Aug 11, 2016

Copy link
Copy Markdown
Contributor

Rather than adding methods to the connection adapter for this, what if we instead yield the transaction to the block?

MyModel.transaction do |t|
  t.on_commit do
  end
end

@cristianbica

Copy link
Copy Markdown
Member Author

@sgrif on the current use case (the ActiveJob thing) the callbacks would be added somewhere inside AJ. But inside AJ we can use ActiveRecord::Base.connection.current_transaction.on_commit.
So yes I think it's better what you suggested and I'll implement that for other cases where commit callbacks would be used. thanks

@kaspth

kaspth commented Aug 11, 2016

Copy link
Copy Markdown
Contributor

So this will essentially interpret e.g. after_create as after_create_commit for jobs?

@matthewd

Copy link
Copy Markdown
Member

@sgrif I don't think that works for this method's use case in general: while it's called on_commit, I expect it to generally be used as more of a whenever_everything_is_next_permanently_persisted -- "if we happen to be inside a transaction [for reasons outside our control], we need to defer this bit". If you have the relevant transaction block right there, you could just move the code down a few lines.

I'd like it delegated from the model instance (and class, I guess), so I don't think I feel strongly on whether it appears on connection, or goes straight from there to current_transaction.

@matthewd

Copy link
Copy Markdown
Member

@kaspth ... kinda? But that's a separate change, for which this is just a dependency.

@matthewd

Copy link
Copy Markdown
Member

Also, known future work, which we didn't want to get distracted on just yet: unifying this generic callback with the existing, narrower, model lifecycle hooks.

I imagine we'll keep both, because enqueuing a block for every model instance would be very inefficient. But their implementations could be more intertwined, instead of doing largely parallel things, separately.

@kaspth

kaspth commented Aug 11, 2016

Copy link
Copy Markdown
Contributor

@matthewd I'm just curious how:

defer the job enqueueing until the transaction is committed

would play out in broad strokes. I'm aware it'll be a separate change 😁

Otherwise :shipit:

@cristianbica
cristianbica force-pushed the ar-transaction-callbacks branch from da382f7 to 459df6f Compare August 11, 2016 22:54
@cristianbica

Copy link
Copy Markdown
Member Author

@matthewd @sgrif done.
PS: the passing of the transaction instance to the transaction block at least makes the tests more readable :)

@rafaelfranca

Copy link
Copy Markdown
Member

Is this intended to be public API? What is the difference between using this and using after_commit callbacks?

@cristianbica

Copy link
Copy Markdown
Member Author

@rafaelfranca As the ActiveRecord::ConnectionAdapters::Transaction class is nodoc'ed and the immediate usage (AJ thing) is internal I think currently is should't be public API. In the future I think we should make this a public API.
Difference is that this adds instance level / one time callbacks while the after_commitcallbacks are at class level and run for all transactions.

@rafaelfranca

Copy link
Copy Markdown
Member

Right, so we don't need a CHANGELOG entry. CHANGELOG in only for things user facing and this is not yet.

@cristianbica
cristianbica force-pushed the ar-transaction-callbacks branch from 459df6f to 4161854 Compare August 12, 2016 08:53
@cristianbica

Copy link
Copy Markdown
Member Author

@rafaelfranca right. Removed CHANGELOG changes. Thanks

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Since this is the same code as below in run_rollback_callbacks you could wrap commit_callbacks.clear and rollback_callbacks.clearinto a sharedclear_callbacks` method.

Also now the transaction block receives the current transaction (ActiveRecord::Transaction instance) as argument.

Example:

```
  MyModel.transaction do |t|
    t.on_commit do
      # ...
    end
    t.on_rollback do
      # ...
    end
  end
```
@cristianbica
cristianbica force-pushed the ar-transaction-callbacks branch from 4161854 to fdf0557 Compare August 12, 2016 16:11
@cristianbica

Copy link
Copy Markdown
Member Author

@eileencodes done

@rafaelfranca

Copy link
Copy Markdown
Member

r? @matthewd

@arthurnn arthurnn self-assigned this Sep 16, 2016
@arthurnn

arthurnn commented Nov 1, 2016

Copy link
Copy Markdown
Member

I am not sure I like adding more callbacks. I think the solution for #26045 would just make it raise in AJ if you are inside a callback. @dhh thoughts?

@dhh

dhh commented Nov 1, 2016

Copy link
Copy Markdown
Member

Yeah, I'm on the fence here too. I think this is too clever. I think it's a feature that after_create is part of the transaction. If we blur that line by making some executions within that block actually happen outside of the transaction, I think we end up making the whole thing conceptually more complicated.

I remain in favor of raising if you enqueue a job within the transaction (perhaps, if a use case arises, with the option to override this default behavior).

@matthewd

matthewd commented Nov 2, 2016

Copy link
Copy Markdown
Member

I certainly don't want to make after_create magically/unpredictably do some things outside the transaction... my aim is just to allow an API by which code anywhere can arrange, for itself, to do a thing after the transaction is completed. Inside an after_create is just a place that sometimes holds code that could benefit from that ability, as shown by the existence of after_create_commit.

I personally think it would be safe to default to having AJ automatically defer jobs enqueued inside transactions (most importantly, because they're already not guaranteed to run at any particular time after they're enqueued -- so there's no change to the contract, or even realistically plausible behaviour)... but that's something we can consider on the subsequent PR: this one only provides the low level API that would support it.


Apart from my hope to improve the default story for jobs involving in-txn records, this particular PR aims to address a more generic pattern of needing to use after_create or after_update to note the original/old values of some fields, for later inspection in a matching after_create_commit (the remember_to_* pattern).

@dhh

dhh commented Nov 8, 2016

Copy link
Copy Markdown
Member

Matthew, how would #perform_later know whether it's supposed to queue immediately or delay for a transaction-done hook? It also seems a bit bleeding for AJ to know about the internal timings of AR callbacks? Or maybe I'm misunderstanding.

Curious to see how we can use this to fix the remember_to_* pattern. That one really does annoy me and I wish we had a better story.

@cristianbica

Copy link
Copy Markdown
Member Author

Damn ... missed the notifications for those comments. Sadly I had other priorities in the last months so I couldn't work on this. I have to admit this solution is pretty clever :) but it might be too clever. Here's a rough implementation (I mean really rough) in AJ which uses this to defer job enqueuing: cristianbica/my-rails@2c2961f

What it does is:

  • AJ has a transactions_behaviour flag which can be : enqueue (current one), :defer, :raise
  • when enqueueing a job while in a transaction it will raise if transactions_behaviour is :raise
  • when enqueueing a job while in a transaction it will enqueue the job if transactions_behaviour is :enqueue
  • when enqueueing a job while in a transaction and transactions_behaviour is :defer:
    • it will store the job on a thread variable
    • it will register on_commit/on_rollback callbacks on all opened transactions
    • when all registered on_commit callbacks are called (all transactions are committed) it will enqueue the job (this covers nested transactions)
    • when an on_rollback is called (all transactions will be rolled-back) it will drop the job

@cristianbica

Copy link
Copy Markdown
Member Author

Anyway, as @matthewd said, this PR is about having a clean way to have commit/rollback callbacks on ActiveRecord transactions. The current use-cases I have for this PR are:

  • possible implementation of ActiveJob jobs enqueue deferring
  • possible try switching ActiveRecord implementation of after_commit / after_rollback to use the on_commit / on_rollback callbacks rather then the current one where transactions have references to AR records and calling some methods on them on commit/rollback

@cristianbica

Copy link
Copy Markdown
Member Author

ping @dhh @matthewd

@arthurnn arthurnn removed their assignment Mar 2, 2017
@cristianbica

Copy link
Copy Markdown
Member Author

Closing this and will take another stab at the initial issue.

@cristianbica

Copy link
Copy Markdown
Member Author

@byroot here is the PR related to enqueue jobs after transaction is committed. Also see #26045. And here’s a PoC cristianbica/my-rails@2c2961f

@byroot

byroot commented Oct 9, 2023

Copy link
Copy Markdown
Member

Thank you @cristianbica, we'll definitely revisit this for 7.2

@byroot

byroot commented Nov 21, 2023

Copy link
Copy Markdown
Member

So I started looking at this again. One extra difficulty now is that we need to handle multi-db, so we can't just register callbacks on a transaction, we'd need some other object overseeing all transactions from all DBs.

Doable, but trickier.

casperisfine pushed a commit to Shopify/rails that referenced this pull request Mar 26, 2024
Ref: rails#26103

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.

The one thing I haven't implemented yet, but probably isn't too hard is the
Active Job integration. But for that one we probably want to do it with some
sort of callback so that we don't introduce a hard dependency on Active Record
in Active Job.

Co-Authored-By: Cristian Bica <[email protected]>
casperisfine pushed a commit to Shopify/rails that referenced this pull request Mar 27, 2024
Ref: rails#26103

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.

The one thing I haven't implemented yet, but probably isn't too hard is the
Active Job integration. But for that one we probably want to do it with some
sort of callback so that we don't introduce a hard dependency on Active Record
in Active Job.

Co-Authored-By: Cristian Bica <[email protected]>
casperisfine pushed a commit to Shopify/rails that referenced this pull request Mar 28, 2024
Ref: rails#26103

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.

The one thing I haven't implemented yet, but probably isn't too hard is the
Active Job integration. But for that one we probably want to do it with some
sort of callback so that we don't introduce a hard dependency on Active Record
in Active Job.

Co-Authored-By: Cristian Bica <[email protected]>
casperisfine pushed a commit to Shopify/rails that referenced this pull request Mar 29, 2024
Ref: rails#26103

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.

The one thing I haven't implemented yet, but probably isn't too hard is the
Active Job integration. But for that one we probably want to do it with some
sort of callback so that we don't introduce a hard dependency on Active Record
in Active Job.

Co-Authored-By: Cristian Bica <[email protected]>
casperisfine pushed a commit to Shopify/rails that referenced this pull request Apr 3, 2024
Ref: rails#26103

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.

The one thing I haven't implemented yet, but probably isn't too hard is the
Active Job integration. But for that one we probably want to do it with some
sort of callback so that we don't introduce a hard dependency on Active Record
in Active Job.

Co-Authored-By: Cristian Bica <[email protected]>
casperisfine pushed a commit to Shopify/rails that referenced this pull request Apr 3, 2024
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 <[email protected]>
casperisfine pushed a commit to Shopify/rails that referenced this pull request Apr 3, 2024
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 <[email protected]>
casperisfine pushed a commit to Shopify/rails that referenced this pull request Apr 3, 2024
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 <[email protected]>
casperisfine pushed a commit to Shopify/rails that referenced this pull request Apr 3, 2024
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 <[email protected]>
casperisfine pushed a commit to Shopify/rails that referenced this pull request Apr 3, 2024
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 <[email protected]>
casperisfine pushed a commit to Shopify/rails that referenced this pull request Apr 3, 2024
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 <[email protected]>
casperisfine pushed a commit to Shopify/rails that referenced this pull request Apr 3, 2024
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 <[email protected]>
casperisfine pushed a commit to Shopify/rails that referenced this pull request Apr 3, 2024
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 <[email protected]>
casperisfine pushed a commit to Shopify/rails that referenced this pull request Apr 3, 2024
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 <[email protected]>
casperisfine pushed a commit to Shopify/rails that referenced this pull request Apr 3, 2024
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 <[email protected]>
fractaledmind pushed a commit to fractaledmind/rails that referenced this pull request May 13, 2024
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 <[email protected]>
xjunior pushed a commit to xjunior/rails that referenced this pull request Jun 9, 2024
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 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants