fix(parsers): detect stale jobs by run age
All checks were successful
CI/CD Pipeline / Quality Gate (push) Successful in 20s
CI/CD Pipeline / Build and Push Images (push) Successful in 6s
CI/CD Pipeline / Internal Notify (push) Successful in 1s
CI/CD Pipeline / Deploy Dev in Dokploy (push) Successful in 1s

This commit is contained in:
2026-04-28 21:56:01 +02:00
parent 0682398935
commit 5857f1a4d2
7 changed files with 90 additions and 37 deletions

View File

@@ -291,7 +291,7 @@ class BackgroundJobServiceTest(TestCase):
old_timestamp = timezone.now() - timedelta(hours=3)
BackgroundJob.objects.filter(
task_id__in=[stale_job.task_id, unrelated_job.task_id]
).update(updated_at=old_timestamp)
).update(created_at=old_timestamp, updated_at=timezone.now())
updated = BackgroundJobService.mark_stale_active_jobs_failed(
max_age_minutes=90,

View File

@@ -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)

View File

@@ -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")