ragnarson / after_commit_queue Goto Github PK
View Code? Open in Web Editor NEWafter_commit_queue allows to run callbacks after transaction is committed
License: MIT License
after_commit_queue allows to run callbacks after transaction is committed
License: MIT License
If the transaction rolls back the hooks will stay around and will get submitted twice on retry.
after_commit_queue doesn't seem to work for nested transactions, as it stores its queue within the model object and ActiveRecord only runs the after_commit hook once for each uniq record. See https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb#L80-L89
Thus, if an individual record is updated twice within a single transaction, the run_after_commit runs only once. Compare this overly simplified example:
class MyModel ...
after_save do
run_after_commit { puts "run after commit called" }
end
end
MyModel.transaction do
model1 = MyModel.find(1)
model1.title = "1"
model1.save!
model2 = MyModel.find(1) # same "record"
model2.title = "2"
model2.save!
end
Output is only a single:
run after commit called
If we'd instead save the queue within the transaction object.
module AfterCommitQueue
# ...
def after_commit_queue
self.class.connection.current_transaction.instance_variable_get("@run_after_commit_queue") || self.class.connection.current_transaction.instance_variable_set("@run_after_commit_queue", [])
end
def clear_after_commit_queue
self.class.connection.current_transaction.instance_variable_set "@run_after_commit_queue", []
end
end
It would work as IMO expected. What do you think?
Hi,
I would like to suggest releasing a new version that is compatible with Rails 4.0. Bundling from GitHub ist ok, but it would be nice to get a „good“ version from rubygems.
Thanks!
When using gem like this
Gemfile
source :rubygems
gem 'after_commit_queue', :git => 'git://github.com/Ragnarson/after_commit_queue.git'
Generates
ArgumentError: invalid byte sequence in US-ASCII
An error occured while installing after_commit_queue (0.0.2), and Bundler cannot continue.
Make sure that `gem install after_commit_queue -v '0.0.2'` succeeds before bundling.
Extracted from #2
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.