Skip to content

Commit

Permalink
Clean unfinished audits (#210)
Browse files Browse the repository at this point in the history
* add audit parameters on audit __str__

* add task to clean unfinished audits

* add clean unfinished audits to populate-periodic-tasks
  • Loading branch information
fargito authored Jul 4, 2020
1 parent 78bda06 commit 0feb868
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
13 changes: 13 additions & 0 deletions backend/audits/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,19 @@ def clean_old_audit_statuses():
statuses.filter(status=AvailableStatuses.RUNNING.value).delete()


@shared_task
def clean_unfinished_audits():
"""some audits may never finish and end up stuck in a loop. we need to remove those"""
finished_statuses = (AvailableStatuses.ERROR.value, AvailableStatuses.SUCCESS.value)
for audit in Audit.objects.prefetch_related("audit_status_history"):
if (
audit.audit_status_history.order_by("created_at").last().status
not in finished_statuses
):
print(f"Deleting {audit.uuid}: {audit}")
audit.delete()


@shared_task
def get_wpt_audit_configurations(wpt_instance_url="https://webpagetest.org"):
"""gets all the available locations from WPT"""
Expand Down
4 changes: 2 additions & 2 deletions backend/audits/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def request_audit(request, project_uuid):
AuditStatusHistory.objects.create(
audit=audit,
status=AvailableStatuses.REQUESTED.value,
details="Audit created in database",
details="Audit manually requested",
)
created_audits.append(audit)

Expand All @@ -49,7 +49,7 @@ def request_audit(request, project_uuid):
AuditStatusHistory.objects.create(
audit=audit,
status=AvailableStatuses.REQUESTED.value,
details="Audit created in database",
details="Audit manually requested",
)
created_audits.append(audit)

Expand Down
8 changes: 8 additions & 0 deletions backend/core/management/commands/populate-periodic-tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ def handle(self, *args, **options):
schedule_2, _ = CrontabSchedule.objects.get_or_create(
minute="0", hour="2", day_of_week="*", day_of_month="*", month_of_year="*"
)
schedule_3, _ = CrontabSchedule.objects.get_or_create(
minute="0", hour="3", day_of_week="*", day_of_month="*", month_of_year="*"
)
schedule_7, _ = CrontabSchedule.objects.get_or_create(
minute="0", hour="7", day_of_week="*", day_of_month="*", month_of_year="*"
)
Expand Down Expand Up @@ -49,6 +52,11 @@ def handle(self, *args, **options):
name="clean old audits statuses",
task="audits.tasks.clean_old_audit_statuses",
)
PeriodicTask.objects.create(
crontab=schedule_3,
name="clean unfinished audits",
task="audits.tasks.clean_unfinished_audits",
)

schedule_seconds, _ = IntervalSchedule.objects.get_or_create(
every=1, period=IntervalSchedule.SECONDS
Expand Down

0 comments on commit 0feb868

Please sign in to comment.