Skip to content

Commit

Permalink
Add InstanceStatusSnapshot to cleanup db task
Browse files Browse the repository at this point in the history
These are also piling up and should be deleted
  • Loading branch information
frankh committed Sep 5, 2017
1 parent d0edb89 commit 89e9290
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions cabot/cabotapp/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,36 @@ def clean_db(days_to_retain=7, batch_size=10000):
To loop over undeleted results, spawn new tasks to make sure db connection closed etc
"""
from .models import StatusCheckResult, ServiceStatusSnapshot
from .models import StatusCheckResult, ServiceStatusSnapshot, InstanceStatusSnapshot

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(
to_discard_service = ServiceStatusSnapshot.objects.order_by('time').filter(
time__lte=timezone.now() - timedelta(days=days_to_retain)
)
to_discard_instance = InstanceStatusSnapshot.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)
service_snapshot_ids = to_discard_service[:batch_size].values_list('id', flat=True)
instance_snapshot_ids = to_discard_instance[:batch_size].values_list('id', flat=True)

result_count = result_ids.count()
snapshot_count = snapshot_ids.count()
service_snapshot_count = service_snapshot_ids.count()
instance_snapshot_count = instance_snapshot_ids.count()

StatusCheckResult.objects.filter(id__in=result_ids).delete()
ServiceStatusSnapshot.objects.filter(id__in=snapshot_ids).delete()
ServiceStatusSnapshot.objects.filter(id__in=service_snapshot_ids).delete()
InstanceStatusSnapshot.objects.filter(id__in=instance_snapshot_ids).delete()

# If we reached the batch size on either we need to re-queue to continue cleaning up.
if result_count == batch_size or snapshot_count == batch_size:
if (
result_count == batch_size or
service_snapshot_count == batch_size or
instance_snapshot_count == batch_size
):
clean_db.apply_async(kwargs={
'days_to_retain': days_to_retain,
'batch_size': batch_size},
Expand Down

0 comments on commit 89e9290

Please sign in to comment.