feat: update finance exchange and source refresh jobs
This commit is contained in:
@@ -61,6 +61,20 @@ class BackgroundJobModelTest(TestCase):
|
||||
self.assertEqual(job.result, result)
|
||||
self.assertIsNotNone(job.completed_at)
|
||||
|
||||
def test_complete_clears_previous_failure_details(self):
|
||||
job = BackgroundJob.objects.create(
|
||||
task_id=fake.uuid4(),
|
||||
task_name="test.task",
|
||||
error="stale error",
|
||||
traceback="stale traceback",
|
||||
)
|
||||
|
||||
job.complete(result={"processed": 1})
|
||||
|
||||
self.assertEqual(job.status, JobStatus.SUCCESS)
|
||||
self.assertEqual(job.error, "")
|
||||
self.assertEqual(job.traceback, "")
|
||||
|
||||
def test_fail(self):
|
||||
"""Тест завершения с ошибкой."""
|
||||
job = BackgroundJob.objects.create(
|
||||
@@ -288,7 +302,7 @@ class BackgroundJobServiceTest(TestCase):
|
||||
task_name="apps.other.tasks.task",
|
||||
meta={"source": "industrial_products"},
|
||||
)
|
||||
old_timestamp = timezone.now() - timedelta(hours=3)
|
||||
old_timestamp = timezone.now() - timedelta(hours=25)
|
||||
BackgroundJob.objects.filter(
|
||||
task_id__in=[stale_job.task_id, unrelated_job.task_id]
|
||||
).update(created_at=old_timestamp, updated_at=timezone.now())
|
||||
@@ -307,3 +321,37 @@ class BackgroundJobServiceTest(TestCase):
|
||||
self.assertIn("Stale background job", stale_job.error)
|
||||
self.assertEqual(fresh_job.status, JobStatus.PENDING)
|
||||
self.assertEqual(unrelated_job.status, JobStatus.PENDING)
|
||||
|
||||
def test_mark_stale_active_jobs_uses_updated_at_for_started_job(self):
|
||||
job = BackgroundJobService.create_job(
|
||||
task_id="job-heartbeat",
|
||||
task_name="apps.parsers.tasks.parse_industrial_products",
|
||||
meta={"source": "industrial_products"},
|
||||
)
|
||||
job.mark_started()
|
||||
old_timestamp = timezone.now() - timedelta(hours=5)
|
||||
BackgroundJob.objects.filter(pk=job.pk).update(
|
||||
started_at=old_timestamp,
|
||||
updated_at=timezone.now(),
|
||||
)
|
||||
|
||||
updated = BackgroundJobService.mark_stale_active_jobs_failed(
|
||||
max_age_minutes=240,
|
||||
task_names={"apps.parsers.tasks.parse_industrial_products"},
|
||||
meta_sources={"industrial_products"},
|
||||
)
|
||||
|
||||
self.assertEqual(updated, 0)
|
||||
job.refresh_from_db()
|
||||
self.assertEqual(job.status, JobStatus.STARTED)
|
||||
|
||||
BackgroundJob.objects.filter(pk=job.pk).update(updated_at=old_timestamp)
|
||||
updated = BackgroundJobService.mark_stale_active_jobs_failed(
|
||||
max_age_minutes=240,
|
||||
task_names={"apps.parsers.tasks.parse_industrial_products"},
|
||||
meta_sources={"industrial_products"},
|
||||
)
|
||||
|
||||
self.assertEqual(updated, 1)
|
||||
job.refresh_from_db()
|
||||
self.assertEqual(job.status, JobStatus.FAILURE)
|
||||
|
||||
Reference in New Issue
Block a user