Skip to content

The platform for continuously improving AI agents.

1.8 KB
Raw Blame
from celery import shared_task
from django.db.models import Q

from overbae.models import FinetuningJob
from overbae.services.dedicated import due as due_dedicated
from overbae.services.deployment import (
    due_deployments,
    ensure_baseline_deployment,
    ensure_training_deployment,
)
from overbae.services.model_activation import due_activations
from overbae.tasks.dedicated_deployment import advance_dedicated_deployment
from overbae.tasks.model_deployment import advance_model_activation, advance_model_deployment


@shared_task
def reconcile_deployments() -> int:
    # A commit followed by a lost Celery enqueue must still create the deployment.
    for job_id in (
        FinetuningJob.objects.filter(status="deploying")
        .filter(
            Q(deployed_model__isnull=True) | Q(deployed_model__status__in=("ready", "failed")),
        )
        .values_list("id", flat=True)
    ):
        ensure_training_deployment(str(job_id))
    for job_id in (
        FinetuningJob.objects.exclude(status__in=("failed", "cancelled"))
        .filter(
            eval_dataset__isnull=False,
            eval_set__isnull=False,
            progress__before_evals_started_at__isnull=False,
        )
        .values_list("id", flat=True)
    ):
        ensure_baseline_deployment(str(job_id))
    ids = list(due_deployments().values_list("id", flat=True))
    for deployment_id in ids:
        advance_model_deployment.delay(deployment_id=str(deployment_id))
    dedicated = list(due_dedicated().values_list("id", flat=True))
    for dedicated_id in dedicated:
        advance_dedicated_deployment.delay(deployment_id=str(dedicated_id))
    activations = list(due_activations().values_list("id", flat=True))
    for activation_id in activations:
        advance_model_activation.delay(activation_id=str(activation_id))
    return len(ids) + len(dedicated) + len(activations)