-
Notifications
You must be signed in to change notification settings - Fork 14
feat(jobs): schedule periodic stale-job check #1227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
14a449f
feat(jobs): schedule periodic stale-job reconcile + NATS consumer sna…
mihow f91bb66
fix(jobs): address review comments on PR #1227
mihow 081bdb9
fix(jobs): fall back to per-job manager when shared path fails
mihow 08036ca
refactor(jobs): rename beat task to jobs_health_check umbrella
mihow 6dc7e6e
refactor(jobs): fold snapshot task into umbrella, adopt IntegrityChec…
mihow 6726eee
fix(jobs): isolate sub-checks + pre-resolve loggers off event loop
mihow 99d7c57
Merge branch 'main' of github.com:RolnickLab/antenna into feat/period…
mihow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
ami/jobs/migrations/0020_schedule_job_monitoring_beat_tasks.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| from django.db import migrations | ||
|
|
||
|
|
||
| def create_periodic_tasks(apps, schema_editor): | ||
| from django_celery_beat.models import CrontabSchedule, PeriodicTask | ||
|
|
||
| stale_schedule, _ = CrontabSchedule.objects.get_or_create( | ||
| minute="*/15", | ||
| hour="*", | ||
| day_of_week="*", | ||
| day_of_month="*", | ||
| month_of_year="*", | ||
| ) | ||
| PeriodicTask.objects.get_or_create( | ||
| name="jobs.check_stale_jobs", | ||
| defaults={ | ||
| "task": "ami.jobs.tasks.check_stale_jobs_task", | ||
| "crontab": stale_schedule, | ||
| "description": "Reconcile jobs stuck in running states past FAILED_CUTOFF_HOURS", | ||
| }, | ||
| ) | ||
|
|
||
| stats_schedule, _ = CrontabSchedule.objects.get_or_create( | ||
| minute="*/5", | ||
| hour="*", | ||
| day_of_week="*", | ||
| day_of_month="*", | ||
| month_of_year="*", | ||
| ) | ||
| PeriodicTask.objects.get_or_create( | ||
| name="jobs.log_running_async_job_stats", | ||
| defaults={ | ||
| "task": "ami.jobs.tasks.log_running_async_job_stats", | ||
| "crontab": stats_schedule, | ||
| "description": "Log NATS consumer delivered/ack/pending stats for each running async_api job", | ||
| }, | ||
| ) | ||
|
|
||
|
|
||
| def delete_periodic_tasks(apps, schema_editor): | ||
| from django_celery_beat.models import PeriodicTask | ||
|
|
||
| PeriodicTask.objects.filter( | ||
| name__in=["jobs.check_stale_jobs", "jobs.log_running_async_job_stats"], | ||
| ).delete() | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("jobs", "0019_job_dispatch_mode"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.RunPython(create_periodic_tasks, delete_periodic_tasks), | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| from datetime import timedelta | ||
| from unittest.mock import AsyncMock, patch | ||
|
|
||
| from django.test import TestCase | ||
| from django.utils import timezone | ||
|
|
||
| from ami.jobs.models import Job, JobDispatchMode, JobState | ||
| from ami.jobs.tasks import check_stale_jobs_task, log_running_async_job_stats | ||
| from ami.main.models import Project | ||
|
|
||
|
|
||
| class CheckStaleJobsTaskTest(TestCase): | ||
| def setUp(self): | ||
| self.project = Project.objects.create(name="Beat schedule test project") | ||
|
|
||
| def _create_stale_job(self, status=JobState.STARTED, hours_ago=100): | ||
| job = Job.objects.create(project=self.project, name="stale", status=status) | ||
| Job.objects.filter(pk=job.pk).update(updated_at=timezone.now() - timedelta(hours=hours_ago)) | ||
| job.refresh_from_db() | ||
| return job | ||
|
|
||
| @patch("ami.jobs.tasks.cleanup_async_job_if_needed") | ||
| def test_returns_summary_counts(self, _mock_cleanup): | ||
| self._create_stale_job() | ||
| self._create_stale_job() | ||
| result = check_stale_jobs_task() | ||
| self.assertEqual(result, {"total": 2, "updated": 0, "revoked": 2}) | ||
|
|
||
| def test_no_stale_jobs_returns_zero_summary(self): | ||
| self._create_stale_job(hours_ago=1) # recent — not stale | ||
| self.assertEqual(check_stale_jobs_task(), {"total": 0, "updated": 0, "revoked": 0}) | ||
|
|
||
|
|
||
| class LogRunningAsyncJobStatsTest(TestCase): | ||
| def setUp(self): | ||
| self.project = Project.objects.create(name="Async snapshot test project") | ||
|
|
||
| def _create_async_job(self, status=JobState.STARTED): | ||
| job = Job.objects.create(project=self.project, name=f"async {status}", status=status) | ||
| Job.objects.filter(pk=job.pk).update(dispatch_mode=JobDispatchMode.ASYNC_API) | ||
| job.refresh_from_db() | ||
| return job | ||
|
|
||
| def test_no_running_jobs_short_circuits(self): | ||
| # A celery job with async dispatch but a final status should be skipped. | ||
| self._create_async_job(status=JobState.SUCCESS) | ||
| self.assertEqual(log_running_async_job_stats(), {"checked": 0}) | ||
|
|
||
| @patch("ami.jobs.tasks.TaskQueueManager") | ||
| def test_snapshots_each_running_async_job(self, mock_manager_cls): | ||
| job_a = self._create_async_job() | ||
| job_b = self._create_async_job() | ||
|
|
||
| instance = mock_manager_cls.return_value | ||
| instance.__aenter__ = AsyncMock(return_value=instance) | ||
| instance.__aexit__ = AsyncMock(return_value=False) | ||
| instance.log_consumer_stats_snapshot = AsyncMock() | ||
|
|
||
| result = log_running_async_job_stats() | ||
|
|
||
| self.assertEqual(result, {"checked": 2}) | ||
| snapshots = [call.args[0] for call in instance.log_consumer_stats_snapshot.await_args_list] | ||
| self.assertCountEqual(snapshots, [job_a.pk, job_b.pk]) | ||
|
|
||
| @patch("ami.jobs.tasks.TaskQueueManager") | ||
| def test_one_job_failure_does_not_block_others(self, mock_manager_cls): | ||
| job_ok = self._create_async_job() | ||
| job_broken = self._create_async_job() | ||
|
|
||
| instance = mock_manager_cls.return_value | ||
| instance.__aenter__ = AsyncMock(return_value=instance) | ||
| instance.__aexit__ = AsyncMock(return_value=False) | ||
|
|
||
| calls = [] | ||
|
|
||
| async def _snapshot(job_id): | ||
| calls.append(job_id) | ||
| if job_id == job_broken.pk: | ||
| raise RuntimeError("nats down for this one") | ||
|
|
||
| instance.log_consumer_stats_snapshot = AsyncMock(side_effect=_snapshot) | ||
|
|
||
| result = log_running_async_job_stats() | ||
| self.assertEqual(result, {"checked": 2}) | ||
| self.assertIn(job_ok.pk, calls) | ||
| self.assertIn(job_broken.pk, calls) | ||
|
|
||
| def test_non_async_jobs_skipped(self): | ||
| job = Job.objects.create(project=self.project, name="sync job", status=JobState.STARTED) | ||
| # default dispatch_mode should not be ASYNC_API | ||
| self.assertNotEqual(job.dispatch_mode, JobDispatchMode.ASYNC_API) | ||
| self.assertEqual(log_running_async_job_stats(), {"checked": 0}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.