aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2018-04-06 16:37:33 +0200
committerAlban Peignier2018-04-16 09:09:20 +0200
commitb05676bdda09c10a7130e0f86527a75e6535dad8 (patch)
tree928784b2203a5838cae56ddd7f10cd0422efcfc2
parent2346cdedc9f75cc3af36f856d2b732209af8caab (diff)
downloadchouette-core-b05676bdda09c10a7130e0f86527a75e6535dad8.tar.bz2
Route: Change `#calculate_costs!` to `after_commit` callback
Use an `after_commit` instead of an `after_save`. The `after_save` could cause intermittent problems due to Sidekiq starting before the record/transaction actually gets committed to the database. In those cases, the record wouldn't be found and cause an error. With an `after_commit` callback, the record is sure to be committed before Sidekiq takes over. Additionally, limit the callback to `:create` and `:update` because otherwise it will be active on `:destroy` by default also. This work isn't relevant on destroy, so don't do it then. Update the tests: * Update labels * Use `build` instead of `create` because we're saving the record at the end anyway so we don't need an object that starts out persisted * Use `truncation: true` for these tests because otherwise the `commit` callback doesn't get called due to transactional teardown Refs #6407
-rw-r--r--app/models/chouette/route.rb4
-rw-r--r--spec/models/chouette/route/route_base_spec.rb8
2 files changed, 7 insertions, 5 deletions
diff --git a/app/models/chouette/route.rb b/app/models/chouette/route.rb
index 14bfa47b6..9c722851f 100644
--- a/app/models/chouette/route.rb
+++ b/app/models/chouette/route.rb
@@ -77,7 +77,9 @@ module Chouette
validates_presence_of :published_name
validates_presence_of :line
validates :wayback, inclusion: { in: self.wayback.values }
- after_save :calculate_costs!, if: ->() { TomTom.enabled? }
+ after_commit :calculate_costs!,
+ on: [:create, :update],
+ if: ->() { TomTom.enabled? }
def duplicate opposite=false
overrides = {
diff --git a/spec/models/chouette/route/route_base_spec.rb b/spec/models/chouette/route/route_base_spec.rb
index 3d4a87791..43ff28c40 100644
--- a/spec/models/chouette/route/route_base_spec.rb
+++ b/spec/models/chouette/route/route_base_spec.rb
@@ -62,17 +62,17 @@ RSpec.describe Chouette::Route, :type => :model do
end
context "callbacks" do
- it "calls #calculate_costs! after_save when TomTom is enabled" do
+ it "calls #calculate_costs! after_commit when TomTom is enabled", truncation: true do
allow(TomTom).to receive(:enabled?).and_return(true)
- route = create(:route)
+ route = build(:route)
expect(route).to receive(:calculate_costs!)
route.save
end
- it "doesn't call #calculate_costs! after_save if TomTom is disabled" do
+ it "doesn't call #calculate_costs! after_commit if TomTom is disabled", truncation: true do
allow(TomTom).to receive(:enabled?).and_return(false)
- route = create(:route)
+ route = build(:route)
expect(route).not_to receive(:calculate_costs!)
route.save