2026-07-19 07:18:19 +02:00
|
|
|
"""Worker job callables and queue function-sets (refactor §1, §6).
|
2026-07-17 23:20:34 +02:00
|
|
|
|
2026-07-18 19:53:31 +02:00
|
|
|
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.
|
2026-07-19 07:18:19 +02:00
|
|
|
The sync role runs the coordinator directly (see ``workers.run``), not via arq.
|
2026-07-17 23:20:34 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
import uuid
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
2026-10-05 22:08:42 +02:00
|
|
|
from arq import func
|
|
|
|
|
|
2026-10-08 20:20:21 +02:00
|
|
|
from app.indexer.attachments import dedupe_existing_job
|
2026-07-18 15:27:09 +02:00
|
|
|
from app.outbox.draft_sync import push_draft_to_imap_job, remove_imap_draft_job
|
2026-10-05 22:24:23 +02:00
|
|
|
from app.outbox.jobs import file_sent_copy_job, send_job
|
2026-10-08 18:52:26 +02:00
|
|
|
from app.push.service import push_new_mail_job
|
2026-07-17 23:20:34 +02:00
|
|
|
from app.sync.actions import (
|
2026-10-05 22:08:42 +02:00
|
|
|
ACTION_MAX_TRIES,
|
2026-07-17 23:20:34 +02:00
|
|
|
delete_thread_job,
|
2026-07-22 07:40:29 +02:00
|
|
|
empty_folder_job,
|
2026-07-17 23:20:34 +02:00
|
|
|
move_job,
|
|
|
|
|
move_thread_to_folder_job,
|
|
|
|
|
push_flags_job,
|
2026-07-20 23:12:25 +02:00
|
|
|
restore_thread_job,
|
2026-07-17 23:20:34 +02:00
|
|
|
)
|
2026-07-18 15:27:09 +02:00
|
|
|
from app.sync.engine import bootstrap_and_backfill, refresh_account_job
|
2026-07-17 23:20:34 +02:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
2026-10-05 22:08:42 +02:00
|
|
|
# Fast, interactive user actions (§6): short timeout, high concurrency. The ones
|
|
|
|
|
# pushing a change to the mail server retry (arq ``Retry``) for ~24 hours while
|
|
|
|
|
# the server is unreachable — more tries than arq's default of 5 allows.
|
|
|
|
|
_RETRYING_ACTIONS: list[Any] = [
|
2026-07-18 19:53:31 +02:00
|
|
|
push_flags_job,
|
|
|
|
|
move_job,
|
|
|
|
|
move_thread_to_folder_job,
|
2026-07-20 23:12:25 +02:00
|
|
|
restore_thread_job,
|
2026-07-18 19:53:31 +02:00
|
|
|
delete_thread_job,
|
2026-07-22 07:40:29 +02:00
|
|
|
empty_folder_job,
|
2026-10-05 22:24:23 +02:00
|
|
|
file_sent_copy_job,
|
2026-10-05 22:08:42 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
INTERACTIVE_FUNCTIONS: list[Any] = [
|
|
|
|
|
*(func(job, max_tries=ACTION_MAX_TRIES) for job in _RETRYING_ACTIONS),
|
2026-07-18 19:53:31 +02:00
|
|
|
send_job,
|
|
|
|
|
push_draft_to_imap_job,
|
|
|
|
|
remove_imap_draft_job,
|
|
|
|
|
refresh_account_job,
|
2026-10-08 18:52:26 +02:00
|
|
|
push_new_mail_job,
|
2026-07-18 19:53:31 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
# Bulk work (§6): long timeout, low concurrency, isolated from interactive jobs.
|
2026-10-08 20:20:21 +02:00
|
|
|
BULK_FUNCTIONS = [
|
|
|
|
|
sync_bootstrap_account,
|
|
|
|
|
# Resumable (each batch is saved), so a long first run may span timeouts.
|
|
|
|
|
func(dedupe_existing_job, timeout=6 * 3600),
|
|
|
|
|
]
|