created tasks for grade 4

This commit is contained in:
2026-11-19 19:09:19 +03:00
parent 52be9d282b
commit 27a3a6895f
25 changed files with 1492 additions and 651 deletions
+16 -1
View File
@@ -6,6 +6,7 @@ import json
import logging
import os
import signal
import time
from uuid import UUID
from app.core.background import (
@@ -19,6 +20,8 @@ from app.core.database import create_db_engine, create_session_factory
from app.core.jobs import JobSettings
from app.domain.attachment_jobs import publish_next_job, recover_expired_jobs
from app.storage.broker import JobBroker
from app.storage.jobs import queued_backlog
from app.core.telemetry import Telemetry
def crash_before_publish(attachment_id: UUID) -> None:
@@ -36,6 +39,13 @@ async def run_publisher(crash_attachment_id: UUID | None = None) -> None:
logging.basicConfig(level=settings.log_level)
stop = shutdown_event()
engine = create_db_engine(settings)
telemetry = Telemetry("messenger-publisher")
telemetry.observe_pool(engine.pool)
backlog = (0, 0.0)
labels = {"queue.name": jobs.broker_queue}
telemetry.gauge("messenger.queue.depth", lambda: backlog[0], attributes=labels)
telemetry.gauge("messenger.queue.oldest", lambda: backlog[1], unit="s", attributes=labels)
next_sample = 0.0
broker = None
try:
broker = JobBroker(jobs)
@@ -43,12 +53,16 @@ async def run_publisher(crash_attachment_id: UUID | None = None) -> None:
while not stop.is_set():
published = False
try:
await broker.ensure_ready()
async with factory() as session:
if telemetry.enabled and time.monotonic() >= next_sample:
backlog = await queued_backlog(session)
next_sample = time.monotonic() + 2
await broker.ensure_ready()
await recover_expired_jobs(session, jobs)
published = await publish_next_job(
session,
broker,
telemetry=telemetry,
attachment_id=crash_attachment_id,
before_publish=(
crash_before_publish
@@ -64,6 +78,7 @@ async def run_publisher(crash_attachment_id: UUID | None = None) -> None:
if broker is not None:
await broker.aclose()
await engine.dispose()
await asyncio.to_thread(telemetry.shutdown)
def main() -> None: