Skip to main content

relay

Relaying progress events from a Prefect subprocess through the pod.

The runs that produce progress events execute in subprocesses Prefect launches, which hold no message service client and, for the metadata runtimes, no credential to build one with. So a subprocess hands each finished event to the pod control server over loopback instead, presenting the grant the pod issued for its run, and the pod sends it with its own client.

The pod sets the event's identity fields itself, from the grant and its own username, so a subprocess can only report on the dataset and project its grant was issued for. A background DAG run presents its run grant, scoped to a datasource and a project, and reports task run events. A metadata run presents its datasource's dataset grant (bitfount.progress_events.dataset_grants), scoped to the datasource alone, and reports imaging dataset events. An EHR patient listing presents its listing grant, scoped to the pod's EHR, and reports EHR dataset events, which the pod sends once for each of its EHR datasources.

Both ends import this module, so the request the subprocess builds and the one the pod reads cannot drift apart.

Module​

Functions​

build_relay_sender​

def build_relay_sender(    pod_control_url: str | None,    nonce: str | None,    task_hash: str,    project_id: str | None = None,) ‑> ProgressEventSender | None:

Build a sender that relays a subprocess's events through its pod.

Arguments

  • pod_control_url: Base URL of the pod control server, if there is one.
  • nonce: The grant the pod issued for this run, if it issued one.
  • task_hash: The task hash the grant is scoped to.
  • project_id: The project a run grant is scoped to, or None for a dataset grant.

Returns The sender, or None when there is no pod to relay through or no grant to present, in which case reporters given it report nothing.

decode_relay_event​

def decode_relay_event(kind: object, encoded: object) ‑> ProgressEvent:

Read the event out of a relay request.

Arguments

  • kind: The request's kind field.
  • encoded: The request's event field.

Returns The event.

Raises

  • ValueError: If the kind is unknown or the event cannot be parsed.

encode_relay_request​

def encode_relay_request(    event: ProgressEvent, *, nonce: str, task_hash: str, project_id: str | None = None,) ‑> _JSONDict:

Build the body of a relay request.

Arguments

  • event: The finished event.
  • nonce: The grant the pod issued for this run.
  • task_hash: The task hash the grant is scoped to.
  • project_id: The project a run grant is scoped to, or None for a dataset grant, which is scoped to the datasource alone.

Returns The JSON request body. It carries project_id only for a run grant.

local_pod_control_url​

def local_pod_control_url() ‑> str:

Return the base URL of the pod control server on this machine.

Read from the pod_control_server_port setting, which a subprocess Prefect launches resolves from the same environment as the pod that serves it.

Returns The loopback base URL.

Classes​

PodRelayTransport​

class PodRelayTransport(    pod_control_url: str, *, nonce: str, task_hash: str, project_id: str | None = None,):

Delivers progress events to the pod that launched this subprocess.

Satisfies ProgressEventTransport, so a ProgressEventSender in the subprocess sends through the pod rather than to the message service.

Arguments

  • pod_control_url: Base URL of the pod control server.
  • nonce: The grant the pod issued for this run.
  • task_hash: The task hash the grant is scoped to.
  • project_id: The project a run grant is scoped to, or None for a dataset grant.

Methods​


send_ehr_dataset_event​

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

Hand an EHR dataset event to the pod.

Arguments

  • event: The event.

send_eligibility_metrics_event​

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

Hand an eligibility metrics event to the pod.

Arguments

  • event: The event.

send_imaging_dataset_event​

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

Hand an imaging dataset event to the pod.

Arguments

  • event: The event.

send_task_run_event​

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

Hand a task run event to the pod.

Arguments

  • event: The event.

ProgressEventRelay​

class ProgressEventRelay(sender: ProgressEventSender, username: str):

Sends the events the pod's subprocesses hand it, under the pod's identity.

Lives in the pod. Never blocks: it stamps the event and queues it on the sender.

Arguments

  • sender: Delivers the events to the message service.
  • username: The user the pod authenticates as, which the message service requires every event's datasetOwnerId to be.

Variables​

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

Methods​


close​

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

Flush what is queued, then stop sending.

Arguments

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

relay​

def relay(    self,    event: ProgressEvent,    *,    datasource_names: Iterable[str],    project_id: str | None,) ‑> None:

Set the event's identity fields and queue it for delivery.

The identity fields come from the grant the subprocess presented, not the event, so whatever the subprocess set there is overwritten.

Arguments

  • event: The event a subprocess built.
  • datasource_names: The datasources the grant covers. The event is sent once for each, each copy carrying that datasource's datasetId.
  • project_id: The project the grant is scoped to, or None for a grant with no project. Only task run events carry it.

resume​

def resume(self) ‑> None:

Send again if repeated failures had disabled the sender.