Run rails code after an update to the database has commited, without after_commit

拈花ヽ惹草 提交于 2019-12-05 01:39:44

You can put in a transaction as well. Just like the example below:

transaction do
  thing = Thing.find(1)
  puts thing.foo # outputs "old value"
  thing.foo = "new value"
  thing.save
end
ThingProcessor.queue_job(thing.id)

Update: there is a gem which calls After Transaction, with this you may solve your problem. Here is the link: http://xtargets.com/2012/03/08/understanding-and-solving-race-conditions-with-ruby-rails-and-background-workers/

I had a similar issue, where by I needed to ensure that a transaction had commited before running a series of action. I ended up using this Gem:

https://github.com/Envek/after_commit_everywhere

It meant that I could do the following:

def finalize!
  Order.transaction do
    payment.charge!

    # ...

    # Ensure that we only send out items and perform after actions when the order has defintely be completed
    after_commit { OrderAfterFinalizerJob.perform_later(self) }
  end
end

What about wrapping a try around the transaction so that the job is queued up only upon success of the transaction?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!