diff options
| -rw-r--r-- | app/models/compliance_check_set.rb | 12 | ||||
| -rw-r--r-- | app/services/parent_import_notifier.rb | 15 | ||||
| -rw-r--r-- | app/services/parent_notifier.rb | 19 | ||||
| -rw-r--r-- | config/schedule.rb | 5 | ||||
| -rw-r--r-- | lib/tasks/compliance_check_sets.rb | 11 | ||||
| -rw-r--r-- | lib/tasks/imports.rake | 2 | ||||
| -rw-r--r-- | spec/models/compliance_check_spec.rb | 32 | ||||
| -rw-r--r-- | spec/services/parent_notifier_spec.rb (renamed from spec/services/parent_import_notifier_spec.rb) | 16 |
8 files changed, 88 insertions, 24 deletions
diff --git a/app/models/compliance_check_set.rb b/app/models/compliance_check_set.rb index f4c44d26d..3ea832048 100644 --- a/app/models/compliance_check_set.rb +++ b/app/models/compliance_check_set.rb @@ -19,6 +19,18 @@ class ComplianceCheckSet < ActiveRecord::Base where('created_at BETWEEN :begin AND :end', begin: period_range.begin, end: period_range.end) end + def self.finished_statuses + %w(successful failed warning aborted canceled) + end + + def self.abort_old + where( + 'created_at < ? AND status NOT IN (?)', + 4.hours.ago, + finished_statuses + ).update_all(status: 'aborted') + end + def notify_parent if parent # parent.child_change diff --git a/app/services/parent_import_notifier.rb b/app/services/parent_import_notifier.rb deleted file mode 100644 index 47e6755e4..000000000 --- a/app/services/parent_import_notifier.rb +++ /dev/null @@ -1,15 +0,0 @@ -class ParentImportNotifier - def self.notify_when_finished(imports = nil) - imports ||= imports_pending_notification - imports.each(&:notify_parent) - end - - def self.imports_pending_notification - Import - .where( - notified_parent_at: nil, - status: Import.finished_statuses - ) - .where.not(parent: nil) - end -end diff --git a/app/services/parent_notifier.rb b/app/services/parent_notifier.rb new file mode 100644 index 000000000..653c98aff --- /dev/null +++ b/app/services/parent_notifier.rb @@ -0,0 +1,19 @@ +class ParentNotifier + def initialize(klass) + @klass = klass + end + + def notify_when_finished(collection = nil) + collection ||= objects_pending_notification + collection.each(&:notify_parent) + end + + def objects_pending_notification + @klass + .where( + notified_parent_at: nil, + status: @klass.finished_statuses + ) + .where.not(parent: nil) + end +end diff --git a/config/schedule.rb b/config/schedule.rb index 08488c255..532707470 100644 --- a/config/schedule.rb +++ b/config/schedule.rb @@ -43,6 +43,11 @@ every 5.minutes do rake "import:notify_parent" end +every 5.minutes do + rake "compliance_check_sets:abort_old" + rake "compliance_check_sets:notify_parent" +end + every 1.minute do command "/bin/echo HeartBeat" end diff --git a/lib/tasks/compliance_check_sets.rb b/lib/tasks/compliance_check_sets.rb new file mode 100644 index 000000000..c53c7f9ed --- /dev/null +++ b/lib/tasks/compliance_check_sets.rb @@ -0,0 +1,11 @@ +namespace :compliance_check_sets do + desc "Notify parent check sets when children finish" + task notify_parent: :environment do + ParentNotifier.new(ComplianceCheckSet).notify_when_finished + end + + desc "Mark old unfinished check sets as 'aborted'" + task abort_old: :environment do + ComplianceCheckSet.abort_old + end +end diff --git a/lib/tasks/imports.rake b/lib/tasks/imports.rake index 6bc84acc8..55e017e7f 100644 --- a/lib/tasks/imports.rake +++ b/lib/tasks/imports.rake @@ -1,6 +1,6 @@ namespace :import do desc "Notify parent imports when children finish" task notify_parent: :environment do - ParentImportNotifier.notify_when_finished + ParentNotifier.new(Import).notify_when_finished end end diff --git a/spec/models/compliance_check_spec.rb b/spec/models/compliance_check_spec.rb index f83d78c29..ffa59245c 100644 --- a/spec/models/compliance_check_spec.rb +++ b/spec/models/compliance_check_spec.rb @@ -15,4 +15,36 @@ RSpec.describe ComplianceCheck, type: :model do it { should validate_presence_of :name } it { should validate_presence_of :code } it { should validate_presence_of :origin_code } + + describe ".abort_old" do + it "changes check sets older than 4 hours to aborted" do + Timecop.freeze(Time.now) do + old_check_set = create( + :compliance_check_set, + status: 'pending', + created_at: 4.hours.ago - 1.minute + ) + current_check_set = create(:compliance_check_set, status: 'pending') + + ComplianceCheckSet.abort_old + + expect(current_check_set.reload.status).to eq('pending') + expect(old_check_set.reload.status).to eq('aborted') + end + end + + it "doesn't work on check sets with a `finished_status`" do + Timecop.freeze(Time.now) do + check_set = create( + :compliance_check_set, + status: 'successful', + created_at: 4.hours.ago - 1.minute + ) + + ComplianceCheckSet.abort_old + + expect(check_set.reload.status).to eq('successful') + end + end + end end diff --git a/spec/services/parent_import_notifier_spec.rb b/spec/services/parent_notifier_spec.rb index 3ab505f88..ecf508fcd 100644 --- a/spec/services/parent_import_notifier_spec.rb +++ b/spec/services/parent_notifier_spec.rb @@ -1,4 +1,4 @@ -RSpec.describe ParentImportNotifier do +RSpec.describe ParentNotifier do let(:workbench_import) { create(:workbench_import) } describe ".notify_when_finished" do @@ -20,7 +20,7 @@ RSpec.describe ParentImportNotifier do expect(netex_import).to receive(:notify_parent) end - ParentImportNotifier.notify_when_finished(netex_imports) + ParentNotifier.new(Import).notify_when_finished(netex_imports) end it "doesn't call #notify_parent if its `notified_parent_at` is set" do @@ -33,11 +33,11 @@ RSpec.describe ParentImportNotifier do expect(netex_import).not_to receive(:notify_parent) - ParentImportNotifier.notify_when_finished + ParentNotifier.new(Import).notify_when_finished end end - describe ".imports_pending_notification" do + describe ".objects_pending_notification" do it "includes imports with a parent and `notified_parent_at` unset" do netex_import = create( :netex_import, @@ -47,7 +47,7 @@ RSpec.describe ParentImportNotifier do ) expect( - ParentImportNotifier.imports_pending_notification + ParentNotifier.new(Import).objects_pending_notification ).to eq([netex_import]) end @@ -55,7 +55,7 @@ RSpec.describe ParentImportNotifier do create(:import, parent: nil) expect( - ParentImportNotifier.imports_pending_notification + ParentNotifier.new(Import).objects_pending_notification ).to be_empty end @@ -70,7 +70,7 @@ RSpec.describe ParentImportNotifier do end expect( - ParentImportNotifier.imports_pending_notification + ParentNotifier.new(Import).objects_pending_notification ).to be_empty end @@ -83,7 +83,7 @@ RSpec.describe ParentImportNotifier do ) expect( - ParentImportNotifier.imports_pending_notification + ParentNotifier.new(Import).objects_pending_notification ).to be_empty end end |
