From d0edb89030068d0d32dca077781bd880c57ac51d Mon Sep 17 00:00:00 2001 From: Frank Hamand Date: Tue, 5 Sep 2017 14:14:57 +0100 Subject: [PATCH] Sort status check results before cleaning Currently it could delete relevant status check results (e.g. the last one) --- cabot/cabotapp/tasks.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/cabot/cabotapp/tasks.py b/cabot/cabotapp/tasks.py index 95c1959..438942a 100644 --- a/cabot/cabotapp/tasks.py +++ b/cabot/cabotapp/tasks.py @@ -76,8 +76,12 @@ def clean_db(days_to_retain=7, batch_size=10000): """ from .models import StatusCheckResult, ServiceStatusSnapshot - to_discard_results = StatusCheckResult.objects.filter(time_complete__lte=timezone.now() - timedelta(days=days_to_retain)) - to_discard_snapshots = ServiceStatusSnapshot.objects.order_by('time').filter(time__lte=timezone.now() - timedelta(days=days_to_retain)) + to_discard_results = StatusCheckResult.objects.order_by('time_complete').filter( + time_complete__lte=timezone.now() - timedelta(days=days_to_retain) + ) + to_discard_snapshots = ServiceStatusSnapshot.objects.order_by('time').filter( + time__lte=timezone.now() - timedelta(days=days_to_retain) + ) result_ids = to_discard_results[:batch_size].values_list('id', flat=True) snapshot_ids = to_discard_snapshots[:batch_size].values_list('id', flat=True)