Skip to content

Commit

Permalink
fix(api): cascade deletion and next scan at time (#5999)
Browse files Browse the repository at this point in the history
  • Loading branch information
vicferpoy authored Dec 3, 2024
1 parent bb34a93 commit 32d8da2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
4 changes: 4 additions & 0 deletions api/src/backend/tasks/beat.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from datetime import datetime, timedelta, timezone

from django_celery_beat.models import IntervalSchedule, PeriodicTask
from rest_framework_json_api.serializers import ValidationError
Expand Down Expand Up @@ -28,6 +29,9 @@ def schedule_provider_scan(provider_instance: Provider):
}
),
one_off=False,
defaults={
"start_time": datetime.now(timezone.utc) + timedelta(hours=24),
},
)
if not created:
raise ValidationError(
Expand Down
7 changes: 6 additions & 1 deletion api/src/backend/tasks/jobs/deletion.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.db import transaction

from api.db_utils import batch_delete
from api.models import Finding, Provider, Resource, Scan
from api.models import Finding, Provider, Resource, Scan, ScanSummary

logger = get_task_logger(__name__)

Expand All @@ -25,6 +25,11 @@ def delete_provider(pk: str):
deletion_summary = {}

with transaction.atomic():
# Delete Scan Summaries
scan_summaries_qs = ScanSummary.all_objects.filter(scan__provider=instance)
_, scans_summ_summary = batch_delete(scan_summaries_qs)
deletion_summary.update(scans_summ_summary)

# Delete Findings
findings_qs = Finding.all_objects.filter(scan__provider=instance)
_, findings_summary = batch_delete(findings_qs)
Expand Down
2 changes: 1 addition & 1 deletion api/src/backend/tasks/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def perform_scheduled_scan_task(self, tenant_id: str, provider_id: str):
name=f"scan-perform-scheduled-{provider_id}"
)
next_scan_date = datetime.combine(
datetime.now(timezone.utc), periodic_task_instance.date_changed.time()
datetime.now(timezone.utc), periodic_task_instance.start_time.time()
) + timedelta(hours=24)

scan_instance = Scan.objects.create(
Expand Down

0 comments on commit 32d8da2

Please sign in to comment.