fix: avoid repeated task and source scans on dashboard
Some checks failed
Mostovik Backend CI/CD / Tests and lint (pull_request) Successful in 3m16s
Mostovik Backend CI/CD / Build linux/amd64 release images (pull_request) Has been skipped
Mostovik Backend CI/CD / Deploy dev (pull_request) Has been skipped
Mostovik Backend CI/CD / Deploy and verify internal main (pull_request) Has been skipped
Mostovik Backend CI/CD / Tests and lint (push) Failing after 3s
Mostovik Backend CI/CD / Build linux/amd64 release images (push) Has been skipped
Mostovik Backend CI/CD / Deploy dev (push) Has been skipped
Mostovik Backend CI/CD / Deploy and verify internal main (push) Has been skipped
Some checks failed
Mostovik Backend CI/CD / Tests and lint (pull_request) Successful in 3m16s
Mostovik Backend CI/CD / Build linux/amd64 release images (pull_request) Has been skipped
Mostovik Backend CI/CD / Deploy dev (pull_request) Has been skipped
Mostovik Backend CI/CD / Deploy and verify internal main (pull_request) Has been skipped
Mostovik Backend CI/CD / Tests and lint (push) Failing after 3s
Mostovik Backend CI/CD / Build linux/amd64 release images (push) Has been skipped
Mostovik Backend CI/CD / Deploy dev (push) Has been skipped
Mostovik Backend CI/CD / Deploy and verify internal main (push) Has been skipped
This commit was merged in pull request #47.
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
from datetime import timedelta
|
||||
from unittest.mock import MagicMock
|
||||
from uuid import UUID
|
||||
|
||||
import pytest
|
||||
from apps.core.models import BackgroundJob, JobStatus
|
||||
@@ -145,3 +146,53 @@ def test_latest_task_does_not_reactivate_a_stale_job(status, time_field):
|
||||
**{time_field: timezone.now() - timedelta(minutes=15)}
|
||||
)
|
||||
assert SourceCardService.get_card(definition.slug)["active_tasks"] == []
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_latest_job_uses_created_time_then_uuid_and_keeps_older_active_jobs():
|
||||
definition = SourceCardService.get_definition("arbitration-cases")
|
||||
jobs = [
|
||||
BackgroundJob.objects.create(
|
||||
id=UUID(int=index),
|
||||
task_id=f"ordered-{index}",
|
||||
task_name=definition.task_names[0],
|
||||
status=JobStatus.SUCCESS,
|
||||
)
|
||||
for index in (1, 2, 3, 4)
|
||||
]
|
||||
now = timezone.now()
|
||||
BackgroundJob.objects.filter(pk__in=[job.pk for job in jobs]).update(
|
||||
created_at=now - timedelta(minutes=2)
|
||||
)
|
||||
# UUID 4 is older; UUID 2 wins the timestamp tie with UUID 1.
|
||||
BackgroundJob.objects.filter(pk__in=[jobs[0].pk, jobs[1].pk]).update(created_at=now)
|
||||
BackgroundJob.objects.filter(pk=jobs[2].pk).update(status=JobStatus.STARTED)
|
||||
active, refresh = SourceCardService._build_task_maps((definition,))
|
||||
assert [job["task_id"] for job in active[definition.slug]] == ["ordered-3"]
|
||||
assert [job["task_id"] for job in refresh[definition.slug]] == [
|
||||
"ordered-2",
|
||||
"ordered-3",
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_latest_job_is_chosen_before_filtering_source_card_metadata():
|
||||
definition = SourceCardService.get_definition("arbitration-cases")
|
||||
older = BackgroundJob.objects.create(
|
||||
task_id="old-card-job",
|
||||
task_name="custom.shared.task",
|
||||
status=JobStatus.SUCCESS,
|
||||
meta={"source_card": definition.slug},
|
||||
)
|
||||
newer = BackgroundJob.objects.create(
|
||||
task_id="new-other-card-job",
|
||||
task_name=older.task_name,
|
||||
status=JobStatus.SUCCESS,
|
||||
meta={"source_card": "bankruptcy-procedures"},
|
||||
)
|
||||
BackgroundJob.objects.filter(pk=older.pk).update(
|
||||
created_at=newer.created_at - timedelta(minutes=1)
|
||||
)
|
||||
active, refresh = SourceCardService._build_task_maps((definition,))
|
||||
assert active == {}
|
||||
assert refresh == {}
|
||||
|
||||
Reference in New Issue
Block a user