Skip to content
This repository was archived by the owner on Jun 2, 2021. It is now read-only.

Commit 7eb2257

Browse files
reidmitBrian Cunnie
andcommitted
🐞 Eliminate spurious UnknownError from CC workers
- Previously, the old records cleanup generates UnknownErrors when there are no records & it has been directed to preserve the most recent. Although these UnknownErrors are harmless, they pollute logs, and can cause consternation. - These UnknownErrors are not visible to the user via API calls; they appear only in the worker logs. - We now check to make sure that there is at least one record before attempting to preserve it. fixes: ```json { "error_code": "UnknownError", "description": "An unknown error occurred.", "code": 10001, "test_mode_info": { "description": "undefined method `id' for nil:NilClass", "error_code": "CF-id" } } ``` [finishes #172773653] Co-authored-by: Reid Mitchell <rmitchell@pivotal.io> Co-authored-by: Brian Cunnie <bcunnie@pivotal.io>
1 parent 9997811 commit 7eb2257

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

lib/database/old_record_cleanup.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ def delete
1616

1717
old_records = model.dataset.where(Sequel.lit('created_at < ?', cutoff_date))
1818
if keep_at_least_one_record
19-
last_id = model.order(:id).last.id
20-
old_records = old_records.where(Sequel.lit('id < ?', last_id))
19+
last_record = model.order(:id).last
20+
if last_record
21+
old_records = old_records.where(Sequel.lit('id < ?', last_record.id))
22+
end
2123
end
2224
logger.info("Cleaning up #{old_records.count} #{model.table_name} table rows")
2325

spec/unit/lib/database/old_record_cleanup_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@
2020
expect { stale_event2.reload }.to raise_error(Sequel::NoExistingObject)
2121
end
2222

23+
context "when there are no records at all but you're trying to keep at least one" do
24+
it "doesn't keep one because there aren't any to keep" do
25+
record_cleanup = Database::OldRecordCleanup.new(VCAP::CloudController::ServiceUsageEvent, 1, keep_at_least_one_record: true)
26+
27+
expect { record_cleanup.delete }.not_to raise_error
28+
expect(VCAP::CloudController::ServiceUsageEvent.count).to eq(0)
29+
end
30+
end
31+
2332
it 'only retrieves the current timestamp from the database once' do
2433
expect(VCAP::CloudController::Event.db).to receive(:fetch).with('SELECT CURRENT_TIMESTAMP as now').once.and_call_original
2534
record_cleanup = Database::OldRecordCleanup.new(VCAP::CloudController::Event, 1)

0 commit comments

Comments
 (0)