Skip to content

Commit 32614e0

Browse files
authored
Merge branch 'main' into remove_v1_code
2 parents 2987b62 + faab557 commit 32614e0

1 file changed

Lines changed: 13 additions & 8 deletions

File tree

‎vulnerabilities/pipelines/management/cleanup_pipeline_runs.py‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,25 @@ def cleanup_old_pipeline_runs(self):
4444
)
4545
for pipeline in progress.iter(pipelines):
4646
cutoff = timezone.now() - timedelta(days=retention_days)
47-
runs = pipeline.pipelineruns.filter(run_exitcode__isnull=False).order_by(
48-
"-created_date"
47+
keep_ids = list(
48+
pipeline.pipelineruns.filter(run_exitcode__isnull=False)
49+
.order_by("-created_date")[:minimum_number_of_runs_to_keep]
50+
.values_list("run_id", flat=True)
4951
)
50-
runs_to_keep = runs[:minimum_number_of_runs_to_keep]
51-
runs_to_delete = runs.filter(created_date__lt=cutoff).exclude(
52-
run_id__in=runs_to_keep.values("run_id")
52+
runs_to_delete = pipeline.pipelineruns.filter(created_date__lt=cutoff).exclude(
53+
run_id__in=keep_ids
5354
)
5455

5556
if runs_to_delete.exists():
5657
pipeline_count += 1
5758

58-
for run in runs_to_delete.iterator():
59-
run.delete()
60-
deleted_run_count += 1
59+
while runs_to_delete.exists():
60+
chunk_ids = list(runs_to_delete[:5000].values_list("run_id", flat=True))
61+
if not chunk_ids:
62+
break
63+
64+
count, _ = pipeline.pipelineruns.filter(run_id__in=chunk_ids).delete()
65+
deleted_run_count += count
6166

6267
self.log(
6368
f"Successfully removed {deleted_run_count:,d} runs from {pipeline_count} pipelines that were older than {retention_days} days."

0 commit comments

Comments
 (0)