Skip to main content

sender

Background delivery of progress events to the message service.

A ProgressEventSender owns one daemon thread and one event loop. Callers on any thread hand it a finished event with submit, which never blocks on the network and never raises; the sender thread delivers the events one at a time, in the order they were submitted.

Because nothing reaches the caller, the sender reports what it did as telemetry (bitfount.telemetry.progress_events): counts of sent, failed and lost events, and an event when it stops sending altogether.

Module​

Functions​

progress_event_kind​

def progress_event_kind(event: ProgressEvent) ‑> ProgressEventKind:

Name which message a progress event is.

Arguments

  • event: The event.

Returns The event's kind.

Classes​

ProgressEventSender​

class ProgressEventSender(    transport: ProgressEventTransport,    *,    destination: _Destination = 'message_service',    enabled: bool | None = None,    max_queued_events: int = 1000,    max_consecutive_failures: int = 5,):

Delivers progress events from a bounded queue on a background thread.

Reporting progress must never fail or stall the run that reports it, so:

  • submit only enqueues. When the queue is full the oldest event is dropped to make room.
  • A failed send is logged and dropped, never re-raised.
  • After max_consecutive_failures failed sends in a row, or the first send rejected as UNIMPLEMENTED (a message service without the progress event rpcs) or raising ProgressEventsUnavailableError, the sender disables itself for the rest of its life and discards anything queued.
  • When the progress_events_enabled setting is off, the sender is disabled from the start and never starts its thread.

The transport is only ever used from the sender's own thread and event loop. A gRPC channel is bound to the loop it was created on, so the transport must not have been used on another loop: give the sender a message service client of its own.

Call flush (or close) before the process exits: the thread is a daemon, so events still queued at exit are lost.

What happens to the events is reported as telemetry: a delivery summary on close and, while events are flowing, at most every five minutes before that, and an ERROR event when the sender disables itself. The sender thread runs in a copy of the context of the first submit, so its telemetry carries that run's context.

Arguments

  • transport: Delivers the events.
  • destination: Where the transport delivers, for the telemetry: "message_service", or "pod" for a subprocess relaying through its pod.
  • enabled: Whether to send events. Defaults to the progress_events_enabled setting.
  • max_queued_events: The most events held waiting to be sent.
  • max_consecutive_failures: Failed sends in a row after which the sender disables itself.

Variables​

  • enabled : bool - Whether submitted events are still being sent.

Methods​


close​

def close(self, timeout: float) ‑> None:

Flush, then stop the sender thread.

Anything still queued when the flush times out is discarded. Further submissions are ignored. The first call reports the sender's last delivery summary. Safe to call more than once.

Arguments

  • timeout: The longest to wait for the flush, in seconds.

flush​

def flush(self, timeout: float) ‑> bool:

Wait until every queued event has been sent or dropped.

Arguments

  • timeout: The longest to wait, in seconds.

Returns True if nothing was left to send when this returned, False if the wait timed out first.

resume​

def resume(self) ‑> None:

Start the failure count afresh, sending again if failures had stopped it.

Called when the message service is reachable again. Failures counted during the outage are forgotten, so they cannot add up with later ones to disable the sender. A sender switched off by configuration, closed, or disabled because the destination does not accept progress events stays as it is.

submit​

def submit(self, event: ProgressEvent) ‑> None:

Queue an event for delivery.

Returns immediately. The event is discarded when the sender is disabled or closed. The delivery summary counts the ones discarded while it is disabled; after close it has already reported.

Arguments

  • event: The event to send, with its timestamp and sequence number already set.

ProgressEventTransport​

class ProgressEventTransport(*args, **kwargs):

The calls a ProgressEventSender needs to deliver events.

_MessageService satisfies this protocol.

Methods​


send_ehr_dataset_event​

async def send_ehr_dataset_event(self, event: EhrDatasetEvent) ‑> object:

Deliver an EHR dataset event, raising if it was not accepted.

send_eligibility_metrics_event​

async def send_eligibility_metrics_event(self, event: EligibilityMetricsEvent) ‑> object:

Deliver an eligibility metrics event, raising if it was not accepted.

send_imaging_dataset_event​

async def send_imaging_dataset_event(self, event: ImagingDatasetEvent) ‑> object:

Deliver an imaging dataset event, raising if it was not accepted.

send_task_run_event​

async def send_task_run_event(self, event: TaskRunEvent) ‑> object:

Deliver a task run event, raising if it was not accepted.

ProgressEventsUnavailableError​

class ProgressEventsUnavailableError(message: str, *, status_code: int | None = None):

Raised by a transport whose destination will never accept these events.

A ProgressEventSender disables itself on the first send that raises this, rather than counting it towards max_consecutive_failures.

Arguments

  • message: What the destination answered.
  • status_code: The HTTP status the destination answered with, if any.