Skip to content
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

676 create periodic task to check processing services #704

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from django.db import migrations
from django_celery_beat.models import PeriodicTask, CrontabSchedule


def create_periodic_task(apps, schema_editor):
crontab_schedule, _ = CrontabSchedule.objects.get_or_create(
minute="*/5", # Every 5 minutes
hour="*", # Every hour
day_of_week="*", # Every day
day_of_month="*", # Every day of month
month_of_year="*", # Every month
)

PeriodicTask.objects.get_or_create(
name="celery.check_processing_services_online",
task="ami.tasks.check_processing_services_online",
crontab=crontab_schedule,
)


def delete_periodic_task(apps, schema_editor):
# Delete the task if rolling back
PeriodicTask.objects.filter(name="celery.check_processing_services_online").delete()


class Migration(migrations.Migration):
dependencies = [
("main", "0044_merge_20250124_2333"),
]

operations = [
migrations.RunPython(create_periodic_task, delete_periodic_task),
]
21 changes: 21 additions & 0 deletions ami/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,24 @@ def save_model_instances(app_label: str, model_name: str, pks: list[int | str],
result = all(results)
if not result:
logger.error(f"Failed to save all {len(instance_pks)} instances of {app_label}.{model_name}")


@celery_app.task(soft_time_limit=10, time_limit=20)
def check_processing_services_online():
"""
Check the status of all processing services and update last checked.
"""
from ami.ml.models import ProcessingService

logger.info("Checking if processing services are online.")

services = ProcessingService.objects.all()

for service in services:
logger.info(f"Checking service {service}")
try:
status_response = service.get_status()
logger.info(status_response)
except Exception as e:
logger.error(f"Error checking service {service}: {e}")
continue
Loading