Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

3.6 KB
Raw Blame
from uuid import UUID

import structlog
from celery import shared_task

from posthog.exceptions_capture import capture_exception
from posthog.tasks.utils import CeleryQueue

logger = structlog.get_logger(__name__)


@shared_task(
    ignore_result=True,
    queue=CeleryQueue.LONG_RUNNING.value,
    max_retries=3,
    autoretry_for=(Exception,),
    retry_backoff=True,
    soft_time_limit=300,
    time_limit=330,
    name="products.canvas.backend.tasks.process_canvas_build",
)
def process_canvas_build(team_id: int, build_id: str) -> None:
    """Run one queued canvas build (idempotent — finished builds are a no-op)."""
    # Deferred import keeps the service off the Celery import path.
    from products.canvas.backend.build_service import run_canvas_build  # noqa: PLC0415

    run_canvas_build(team_id, build_id)


@shared_task(
    ignore_result=True,
    queue=CeleryQueue.DEFAULT.value,
    max_retries=5,
    autoretry_for=(Exception,),
    retry_backoff=True,
    soft_time_limit=60,
    time_limit=90,
    name="products.canvas.backend.tasks.cleanup_notebook_canvas_draft",
)
def cleanup_notebook_canvas_draft(team_id: int, canvas_id: str, version_id: str) -> None:
    from products.canvas.backend.notebook_integration import cleanup_discarded_notebook_canvas_draft  # noqa: PLC0415

    cleanup_discarded_notebook_canvas_draft(team_id=team_id, canvas_id=UUID(canvas_id), version_id=UUID(version_id))


@shared_task(
    ignore_result=True,
    queue=CeleryQueue.DEFAULT.value,
    max_retries=5,
    autoretry_for=(Exception,),
    retry_backoff=True,
    soft_time_limit=60,
    time_limit=90,
    name="products.canvas.backend.tasks.cleanup_canvas_source_uploads",
)
def cleanup_canvas_source_uploads(team_id: int, canvas_id: str, object_keys: list[str]) -> None:
    from products.canvas.backend.build_service import cleanup_unreferenced_source_uploads  # noqa: PLC0415

    cleanup_unreferenced_source_uploads(team_id, UUID(canvas_id), object_keys)


@shared_task(
    ignore_result=True,
    queue=CeleryQueue.DEFAULT.value,
    name="products.canvas.backend.tasks.sweep_canvas_builds",
)
def sweep_canvas_builds() -> None:
    """Recover builds stuck in flight (every 2 minutes)."""
    from products.canvas.backend.build_service import sweep_canvas_builds as run_sweep  # noqa: PLC0415

    try:
        counts = run_sweep()
        if any(counts.values()):
            logger.info("canvas_builds_swept", **counts)
    except Exception as error:
        logger.exception("canvas_build_sweep_failed", error=str(error))
        capture_exception(error, additional_properties={"task": "sweep_canvas_builds"})


@shared_task(
    ignore_result=True, queue=CeleryQueue.DEFAULT.value, name="products.canvas.backend.tasks.cleanup_canvas_builds"
)
def cleanup_canvas_builds() -> None:
    """Apply the canvas artifact retention policy (daily)."""
    from products.canvas.backend.build_service import cleanup_canvas_builds as run_cleanup  # noqa: PLC0415
    from products.canvas.backend.notebook_integration import requeue_discarded_notebook_canvas_drafts  # noqa: PLC0415

    try:
        requeue_discarded_notebook_canvas_drafts()
    except Exception as error:
        logger.exception("canvas_draft_requeue_failed", error=str(error))
        capture_exception(error, additional_properties={"task": "cleanup_canvas_builds"})
    try:
        pruned = run_cleanup()
        if pruned:
            logger.info("canvas_builds_pruned", count=pruned)
    except Exception as error:
        logger.exception("canvas_build_cleanup_failed", error=str(error))
        capture_exception(error, additional_properties={"task": "cleanup_canvas_builds"})