diff options
| author | Teddy Wing | 2018-02-06 17:12:09 +0100 |
|---|---|---|
| committer | Teddy Wing | 2018-02-08 11:29:31 +0100 |
| commit | ee1781d525bc04aff3f7f743be8297ee003ee9e0 (patch) | |
| tree | e11097666c6ad27adada3d7cd1df9ba96a19c396 | |
| parent | e07ef0eb9dac728ee5033b42c318692e2368b897 (diff) | |
| download | chouette-core-ee1781d525bc04aff3f7f743be8297ee003ee9e0.tar.bz2 | |
ComplianceCheckSet: Add `.abort_old`
Copy over the `Import.abort_old` method to this class. For now I haven't
bothered with moving it to a consolidated module. We'll see, maybe
that's a better approach. For now I'm going with a relaxed copy-paste
solution until we decide that the increased complexity of the module is
worth the deduplication.
We need this method to be able to abort stale imports. We'll be doing
this, similarly to imports, in a regular cron job.
Refs #4758
| -rw-r--r-- | app/models/compliance_check_set.rb | 12 | ||||
| -rw-r--r-- | spec/models/compliance_check_spec.rb | 32 |
2 files changed, 44 insertions, 0 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/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 |
