fix(parsers): close stale source jobs
Some checks failed
CI/CD Pipeline / Quality Gate (push) Failing after 2m4s
CI/CD Pipeline / Build and Push Images (push) Has been skipped
CI/CD Pipeline / Deploy Dev in Dokploy (push) Has been skipped
CI/CD Pipeline / Internal Notify (push) Successful in 1s

This commit is contained in:
2026-04-28 21:38:00 +02:00
parent 26f0669670
commit d96b76d32f
9 changed files with 262 additions and 18 deletions

View File

@@ -1,8 +1,11 @@
"""Тесты для BackgroundJob."""
from datetime import timedelta
from apps.core.models import BackgroundJob, JobStatus
from apps.core.services import BackgroundJobService
from django.test import TestCase
from django.utils import timezone
from faker import Faker
fake = Faker()
@@ -252,10 +255,6 @@ class BackgroundJobServiceTest(TestCase):
self.assertEqual([j.task_id for j in active_jobs], [job_user.task_id])
def test_cleanup_old_jobs(self):
from datetime import timedelta
from django.utils import timezone
old_job = BackgroundJobService.create_job(
task_id="job-old",
task_name="test.task",
@@ -272,3 +271,39 @@ class BackgroundJobServiceTest(TestCase):
deleted = BackgroundJobService.cleanup_old_jobs(days=30)
self.assertEqual(deleted, 1)
def test_mark_stale_active_jobs_failed_scopes_by_task_and_source(self):
stale_job = BackgroundJobService.create_job(
task_id="job-stale",
task_name="apps.parsers.tasks.parse_industrial_products",
meta={"source": "industrial_products"},
)
fresh_job = BackgroundJobService.create_job(
task_id="job-fresh",
task_name="apps.parsers.tasks.parse_industrial_products",
meta={"source": "industrial_products"},
)
unrelated_job = BackgroundJobService.create_job(
task_id="job-unrelated",
task_name="apps.other.tasks.task",
meta={"source": "industrial_products"},
)
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)
updated = BackgroundJobService.mark_stale_active_jobs_failed(
max_age_minutes=90,
task_names={"apps.parsers.tasks.parse_industrial_products"},
meta_sources={"industrial_products"},
)
stale_job.refresh_from_db()
fresh_job.refresh_from_db()
unrelated_job.refresh_from_db()
self.assertEqual(updated, 1)
self.assertEqual(stale_job.status, JobStatus.FAILURE)
self.assertIn("Stale background job", stale_job.error)
self.assertEqual(fresh_job.status, JobStatus.PENDING)
self.assertEqual(unrelated_job.status, JobStatus.PENDING)