diff options
| author | Teddy Wing | 2018-02-05 15:49:25 +0100 |
|---|---|---|
| committer | Teddy Wing | 2018-02-06 10:50:51 +0100 |
| commit | 4c3e7adbeccfcbc39de845880882893b5b734741 (patch) | |
| tree | 487e233f19604e744eca338f75f8ea1f1125c7a0 | |
| parent | f3d6710c7a19f835f17dff762dfe520fcffc6d63 (diff) | |
| download | chouette-core-4c3e7adbeccfcbc39de845880882893b5b734741.tar.bz2 | |
Import: Add `.abort_old` method
This will abort all imports that don't have a finished status and were
created more than four hours ago.
We want to add this functionality to the import cron to auto-abort stale
imports.
Refs #4963
| -rw-r--r-- | app/models/import.rb | 5 | ||||
| -rw-r--r-- | spec/models/import_spec.rb | 21 |
2 files changed, 26 insertions, 0 deletions
diff --git a/app/models/import.rb b/app/models/import.rb index 049a65f40..6330bd560 100644 --- a/app/models/import.rb +++ b/app/models/import.rb @@ -42,6 +42,11 @@ class Import < ActiveRecord::Base %w(successful failed warning aborted canceled) end + def self.abort_old + where('created_at < ?', 4.hours.ago) + .update_all(status: 'aborted') + end + def notify_parent parent.child_change update(notified_parent_at: DateTime.now) diff --git a/spec/models/import_spec.rb b/spec/models/import_spec.rb index ffb2360c2..8c1037a48 100644 --- a/spec/models/import_spec.rb +++ b/spec/models/import_spec.rb @@ -29,6 +29,27 @@ RSpec.describe Import, type: :model do ) end + describe ".abort_old" do + it "changes imports older than 4 hours to aborted" do + Timecop.freeze(Time.now) do + old_import = create( + :workbench_import, + status: 'pending', + created_at: 4.hours.ago - 1.minute + ) + current_import = create(:workbench_import, status: 'pending') + + Import.abort_old + + expect(current_import.reload.status).to eq('pending') + expect(old_import.reload.status).to eq('aborted') + end + end + + it "doesn't work on imports with a `finished_status`" do + end + end + describe "#destroy" do it "must destroy all child imports" do netex_import = create(:netex_import) |
