fix(parsers): detect stale jobs by run age
This commit is contained in:
@@ -426,7 +426,10 @@ class ParserLoadLogServiceTest(TestCase):
|
||||
)
|
||||
old_timestamp = timezone.now() - timedelta(hours=3)
|
||||
ParserLoadLog.objects.filter(pk=log.pk).update(updated_at=old_timestamp)
|
||||
BackgroundJob.objects.filter(pk=job.pk).update(updated_at=old_timestamp)
|
||||
BackgroundJob.objects.filter(pk=job.pk).update(
|
||||
created_at=old_timestamp,
|
||||
updated_at=timezone.now(),
|
||||
)
|
||||
|
||||
updated = ParserLoadLogService.mark_stale_in_progress_failed(max_age_minutes=90)
|
||||
|
||||
|
||||
@@ -4,13 +4,14 @@ from datetime import timedelta
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from apps.core.models import BackgroundJob, JobStatus
|
||||
from apps.parsers.source_cards import (
|
||||
SourceCardDefinition,
|
||||
SourceCardService,
|
||||
SourceItemDefinition,
|
||||
)
|
||||
from django.http import Http404
|
||||
from django.test import SimpleTestCase
|
||||
from django.test import SimpleTestCase, TestCase, override_settings
|
||||
from django.utils import timezone
|
||||
from rest_framework.exceptions import ValidationError
|
||||
|
||||
@@ -291,3 +292,43 @@ class SourceCardServiceUnitTest(SimpleTestCase):
|
||||
),
|
||||
"idle",
|
||||
)
|
||||
|
||||
|
||||
@override_settings(PARSER_STALE_LOAD_MAX_AGE_MINUTES=90)
|
||||
class SourceCardServiceDatabaseTest(TestCase):
|
||||
def test_get_active_tasks_ignores_old_jobs_even_when_updated_recently(self):
|
||||
job = BackgroundJob.objects.create(
|
||||
task_id="old-source-task",
|
||||
task_name="apps.parsers.tasks.parse_industrial_products",
|
||||
status=JobStatus.STARTED,
|
||||
progress=10,
|
||||
meta={"source": "industrial_products"},
|
||||
)
|
||||
old_timestamp = timezone.now() - timedelta(hours=3)
|
||||
BackgroundJob.objects.filter(pk=job.pk).update(
|
||||
created_at=old_timestamp,
|
||||
started_at=old_timestamp,
|
||||
updated_at=timezone.now(),
|
||||
)
|
||||
|
||||
tasks = SourceCardService._get_active_tasks(
|
||||
SourceCardService.get_definition("manufacturers-and-products")
|
||||
)
|
||||
|
||||
self.assertEqual(tasks, [])
|
||||
|
||||
def test_get_active_tasks_keeps_recent_pending_jobs(self):
|
||||
BackgroundJob.objects.create(
|
||||
task_id="fresh-source-task",
|
||||
task_name="apps.parsers.tasks.parse_industrial_products",
|
||||
status=JobStatus.PENDING,
|
||||
progress=0,
|
||||
meta={"source": "industrial_products"},
|
||||
)
|
||||
|
||||
tasks = SourceCardService._get_active_tasks(
|
||||
SourceCardService.get_definition("manufacturers-and-products")
|
||||
)
|
||||
|
||||
self.assertEqual(len(tasks), 1)
|
||||
self.assertEqual(tasks[0]["task_id"], "fresh-source-task")
|
||||
|
||||
Reference in New Issue
Block a user