videogen/apps/api/app/workers/tasks_insights.py
caelum0x d56639a594
Some checks failed
build-images / web (push) Has been cancelled
vision2026-ci / Python SDK resource tests (push) Has been cancelled
build-images / api (push) Has been cancelled
build-images / worker-gpu (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Has been cancelled
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Has been cancelled
codeql / Analyze (javascript-typescript) (push) Has been cancelled
codeql / Analyze (python) (push) Has been cancelled
perf / API hot-path micro-smoke (budgeted) (push) Has been cancelled
perf / Desktop engine crates (release build check) (push) Has been cancelled
vision2026-ci / API phase modules (ruff + pytest) (push) Has been cancelled
vision2026-ci / Alembic upgrade + drift check (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Has been cancelled
Menivor — open source release (MIT), free to use and fork
Paid, hosted product at menivor.com, now retired as a company project.
Fresh squashed history of the cleaned working tree (secret-scanned,
no .env, no customer data, no outreach logs). Built on
MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
2026-12-04 02:10:26 +03:00

61 lines
2.2 KiB
Python

"""Celery beat entrypoint for the remake-winner loop.
:func:`winner_loop_tick_task` advances every active loop once per tick
(``app.domains.insights.winner_loop_service.tick``): it queues newly rendered
variants for spaced auto-publishing, and once a project's check window (48h by
default) has passed since its last variant went live it pulls the real platform
metrics, ranks the variants, records the winner with a plain-language
explanation and — in ``auto`` mode, within the spend cap — remakes it.
Beat-safety contract (mirrors ``tasks_series``): the task NEVER raises out of
its body except for a transient ``ConnectionError`` that Celery auto-retries;
any other failure degrades to a logged zeroed summary. Every step is idempotent
(already-queued posts are skipped, settled projects are not re-measured, a
remade project returns its existing remake), so a duplicate tick is safe.
Wiring (celery_app.py): add ``"app.workers.tasks_insights"`` to ``include`` and
a beat entry ``{"task": "app.workers.tasks_insights.winner_loop_tick_task",
"schedule": 15 * 60.0}`` routed to the ``render`` queue (DB + HTTP only).
"""
from __future__ import annotations
from loguru import logger
from app.workers.celery_app import celery_app
WINNER_LOOP_TICK_SCHEDULE_S = 15 * 60.0
_EMPTY_SUMMARY: dict[str, int] = {"loops": 0, "advanced": 0, "winners": 0, "remade": 0, "errors": 0}
def _tick_impl() -> dict[str, int]:
from app.domains.insights.winner_loop_service import tick
return tick()
@celery_app.task(
bind=True,
autoretry_for=(ConnectionError,),
max_retries=3,
retry_backoff=True,
soft_time_limit=840,
time_limit=870,
)
def winner_loop_tick_task(self) -> dict[str, int]:
"""Advance every active remake-winner loop once. Never raises (beat-safe)."""
try:
summary = _tick_impl()
if summary.get("loops"):
logger.info(f"winner loop tick: {summary}")
return summary
except ConnectionError: # transient broker/DB — let Celery autoretry
raise
except Exception as exc: # noqa: BLE001 - a beat tick must never crash the worker
logger.error(f"winner_loop_tick_task failed: {exc}")
return dict(_EMPTY_SUMMARY)
__all__ = ["WINNER_LOOP_TICK_SCHEDULE_S", "winner_loop_tick_task"]