Files
webmail-client/backend/workers/worker.py
T
lukasandClaude Opus 4.8 e0a45c5a46 sync-v2 step 1: worker roles + interactive/bulk queue split
- config: WORKER_ROLE (sync|jobs|all), INTERACTIVE_CONCURRENCY, BULK_CONCURRENCY.
- queue: named queues (interactive/bulk) + enqueue_job() router; sync_bootstrap
  goes to bulk, all other jobs to interactive.
- workers.run: single entrypoint; role=jobs runs both arq consumers (per-queue
  timeout/concurrency), role=sync runs the supervisor, all=both (dev default).
- docker-compose: worker-sync + worker-jobs services (shared image, nofile
  ulimit); backend/Dockerfile; Makefile worker/worker-sync/worker-jobs targets.
- tests repointed to app.queue.get_arq_pool; supervisor behavior unchanged.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-18 19:53:31 +02:00

65 lines
2.1 KiB
Python

"""Worker job functions and the sync-worker lifecycle (refactor §1, §6).
The process is launched via ``workers.run`` (``WORKER_ROLE`` decides which arq
workers run). This module only defines the job callables and the two queue
function-sets; the runner wires them onto the ``interactive`` / ``bulk`` queues.
"""
from __future__ import annotations
import logging
import uuid
from typing import Any
from app.outbox.draft_sync import push_draft_to_imap_job, remove_imap_draft_job
from app.outbox.jobs import send_job
from app.sync.actions import (
delete_thread_job,
move_job,
move_thread_to_folder_job,
push_flags_job,
)
from app.sync.engine import bootstrap_and_backfill, refresh_account_job
logger = logging.getLogger(__name__)
async def sync_bootstrap_account(ctx: dict[str, Any], account_id: str) -> int:
"""Bootstrap + backfill an account (enqueued on account creation)."""
logger.info("sync.bootstrap_account start account=%s", account_id)
count = await bootstrap_and_backfill(uuid.UUID(account_id))
logger.info("sync.bootstrap_account done account=%s messages=%d", account_id, count)
return count
# Fast, interactive user actions (§6): short timeout, high concurrency.
INTERACTIVE_FUNCTIONS = [
push_flags_job,
move_job,
move_thread_to_folder_job,
delete_thread_job,
send_job,
push_draft_to_imap_job,
remove_imap_draft_job,
refresh_account_job,
]
# Bulk work (§6): long timeout, low concurrency, isolated from interactive jobs.
BULK_FUNCTIONS = [sync_bootstrap_account]
async def sync_on_startup(ctx: dict[str, Any]) -> None:
"""Sync-worker startup: launch the account-sync supervisor."""
from app.sync.supervisor import start_supervisor
await start_supervisor(ctx)
logger.info("sync worker: supervisor started")
async def sync_on_shutdown(ctx: dict[str, Any]) -> None:
"""Sync-worker shutdown: stop the supervisor (process cleanup is in the runner)."""
from app.sync.supervisor import stop_supervisor
await stop_supervisor(ctx)
logger.info("sync worker: supervisor stopped")