Skip to content

API Reference parrot.services.comm_center.dispatch

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.services.comm_center.dispatch

View source

Fan-out, status state machine, aggregation and retry (spec §3 Module 6).

This is the only module in CommCenter that talks to Redis. It publishes one xadd per recipient via NotifyClient.stream(), drives the per-row duplicate-delivery-containment state machine (spec §2), and provides batch aggregation and retry over the flat NotificationBatchRecipient table.

async-notify is lazy-imported (spec G11): importing this module never requires it, and a clear, actionable error is raised only when a function that actually needs it is called without the comm-center extra installed.

Attribute DEFAULT_DETAILS_LIMIT

Attribute MAX_DETAILS_LIMIT

Attribute MIN_ASYNC_NOTIFY_VERSION

Alias NotificationBatchRecipient

Re-exported from parrot.handlers.models.NotificationBatchRecipient.

Alias PARROT_SCHEMA

Re-exported from parrot.conf.PARROT_SCHEMA.

Alias RecipientIn

Re-exported from parrot.services.comm_center.models.RecipientIn.

Function aggregate_batch_status

Aggregate a batch's progress from the flat tracking table.

aggregate_batch_status(batch_id: uuid.UUID, *, details: bool = False, status: str | None = None, limit: int = DEFAULT_DETAILS_LIMIT, offset: int = 0) -> dict

Parameter Type Default Description
batch_id uuid.UUID The batch to summarize.
details bool False When True, also return paginated per-recipient rows.
status str | None None Optional status filter, only applied to details rows.
limit int DEFAULT_DETAILS_LIMIT Page size for details rows, clamped to :data:MAX_DETAILS_LIMIT.
offset int 0 Page offset for details rows.

Returns: dict — {"batch_id", "total", "by_status", "rows"} — rows is None unless details=True.

Alias build_wire_payload

Re-exported from parrot.services.comm_center.render.build_wire_payload.

Alias default_dsn

Re-exported from parrot.conf.default_dsn.

Function fan_out

Publish every recipient in a batch, never aborting on one failure.

Reuses a single NotifyClient connection for the whole batch (connect once, close in finally — spec Key Constraints).

Also accepts a PreparedBatch directly in place of payloads — defense in depth for dry-run (spec §3 Module 9): duck-typed via a dry_run attribute, since importing :class:~parrot.services.comm_center.models.PreparedBatch here would be a needless coupling for what is otherwise a plain list of tuples. A batch prepared with dry_run=True is refused unconditionally — the real handler already never reaches this (TASK-2159/2161 skip fan-out/publish_one entirely for a dry run), but a future caller of this service bypassing the handler must not be able to publish one either.

fan_out(batch_id: uuid.UUID, payloads: list) -> None

Parameter Type Default Description
batch_id uuid.UUID The batch these payloads belong to.
payloads list (row_id, payload) pairs — the already-persisted :class:NotificationBatchRecipient id and its wire payload from :func:build_wire_payload — or a PreparedBatch.

Raises:

  • RuntimeError — payloads is a PreparedBatch with dry_run=True.

Function launch_fan_out

Launch :func:fan_out as a background task with a finalizing done-callback.

launch_fan_out(batch_id: uuid.UUID, payloads: list)

Parameter Type Default Description
batch_id uuid.UUID The batch to fan out.
payloads list (row_id, payload) pairs, see :func:fan_out.

Attribute logger

Function publish_one

Publish a single recipient, driving the row status state machine.

Writes publishing before calling client.stream() (the pre-xadd marker that narrows duplicate-delivery risk to rows caught mid-call — spec §2), then queued + published_at on success or publish_failed + reason on failure. attempts is incremented on every attempt, success or failure.

publish_one(batch_id: uuid.UUID, payload: dict, row_id: uuid.UUID, *, client=None, dry_run: bool = False) -> str | None

Parameter Type Default Description
batch_id uuid.UUID The batch this row belongs to.
payload dict The wire payload to publish (from :func:build_wire_payload).
row_id uuid.UUID The :class:NotificationBatchRecipient id to update.
client None An already-connected NotifyClient to reuse — :func:fan_out passes one so a whole batch shares a single Redis connection. When omitted, a client is created and closed here, which is what makes this function directly reusable by the single-recipient endpoint (TASK-2161) without any batch machinery.
dry_run bool False Defense-in-depth guard (spec §3 Module 9 — the dry-run guard "must be enforced at the service layer, not only in the handler, so no future caller of CommCenterService can bypass it"). The real handler already never reaches this with dry_run=True (both send endpoints short-circuit before ever calling publish_one/fan_out); this parameter exists so a caller building a PreparedBatch directly and passing one of its payloads to publish_one cannot publish it either. Mirrors the equivalent guard in :func:fan_out.

Returns: str | None — The Redis stream entry id returned by client.stream(). Verified today's NotifyClient.stream() does not return one (always None) — published_at is the authoritative "this succeeded" marker, not message_id.

Raises:

  • RuntimeError — async-notify is not installed, or dry_run is True.
  • Exception — Whatever client.stream() raised, re-raised after the row has been marked publish_failed — so :func:fan_out can catch it and continue with the rest of the batch, and the single-recipient endpoint can map it to 502.

Function retry_batch

Re-publish retry-eligible rows for a batch (spec §2 state machine).

Re-publishes pending and publish_failed rows; never queued or skipped. publishing rows are only included when force=True — otherwise they are reported as ambiguous and left untouched (they may already have been published; retrying them without force risks a duplicate send).

retry_batch(batch_id: uuid.UUID, *, force: bool = False) -> dict

Parameter Type Default Description
batch_id uuid.UUID The batch to retry.
force bool False When True, also re-publish rows stuck in publishing.

Returns: dict — {"retried": <n>, "ambiguous": <n>}. Row selection (which rows qualify) has already happened by the time this returns, but the actual re-publishing is backgrounded via :func:launch_fan_out — a batch stranded with thousands of retryable rows after an outage must not block the HTTP response (and risk a client/gateway timeout) while they are sequentially re-published, mirroring how the initial send already backgrounds its own fan-out.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally