progress_events
Progress events for dataset preprocessing and task runs.
A ProgressEventSender delivers events to the message service in the
background. An ImagingDatasetEventReporter, EhrDatasetEventReporter or
TaskRunEventReporter builds the events for one preprocessing pass or one task
run and hands them to the sender.
A run in a Prefect subprocess relays its events through the pod instead; see
bitfount.progress_events.relay.
Module
Submodules
- bitfount.progress_events.dataset_grants - The grants the metadata runtimes present to relay their progress events.
- bitfount.progress_events.relay - Relaying progress events from a Prefect subprocess through the pod.
- bitfount.progress_events.reporters - Builders for the progress events of one preprocessing pass or task run.
- bitfount.progress_events.sender - Background delivery of progress events to the message service.
Functions
never_raises
def never_raises(method: Callable[P, None]) ‑> Callable[P, None]:Log and swallow any exception raised by a reporting method.
Arguments
method: The reporting method to guard.
Returns The guarded method.
Classes
EhrDatasetEventReporter
class EhrDatasetEventReporter( sender: ProgressEventSender | None, *, dataset_id: str, dataset_owner_id: str, preprocessing_id: str,):Reports the progress of one preprocessing pass over an EHR dataset.
Arguments
sender: Delivers the events. None makes the reporter a no-op.dataset_id: The dataset being preprocessed.dataset_owner_id: The username of the dataset's owner, which must be the user the sender authenticates as.preprocessing_id: The UUIDv7 identifying this preprocessing pass, fromuuid6.uuid7().
Ancestors
Variables
next_sequence_number : int- The sequence number the next event will carry.
-
sender : ProgressEventSender | None- The sender this reporter hands its events to, if any.Read-only: a caller composing a second reporter for the same run shares this one rather than holding the sender separately.
Methods
error
def error(self, reason: LiteralString, exc: BaseException | None = None) ‑> None:Report that the preprocessing pass failed.
Arguments
reason: A fixed description of what failed. Must be a string literal, so no data can reach it.exc: The exception behind the failure. Only its class name is sent.
patient_listing_completed
def patient_listing_completed(self, patients_listed_count: int) ‑> None:Report that the patient listing step has completed.
Arguments
patients_listed_count: The distinct patients the listing stored. Zero is a valid listing of an empty EHR.
patient_listing_started
def patient_listing_started(self) ‑> None:Report that the patient listing step has started.
preprocessing_started
def preprocessing_started( self, trigger: DatasetPreprocessingStartTrigger.ValueType,) ‑> None:Report that the preprocessing pass has started.
Arguments
trigger: What started the pass.
EventReporter
class EventReporter(sender: ProgressEventSender | None, start_sequence_number: int):Base for progress event reporters: sequence numbering and safe submission.
Subclasses build their events in methods guarded by never_raises and
hand each one to _submit.
Every event a run emits carries a sequence number: its position among that run's events, counting up by one per event in the order they were reported. Consumers order a run's events by it, since timestamps can tie and events can arrive out of order. A number is never reused within a run, so a retried send overwrites rather than duplicates the stored event. The sequence has a gap wherever an event was dropped.
Arguments
sender: Delivers the events. None makes the reporter a no-op.start_sequence_number: The sequence number of the first event.
Subclasses
Variables
next_sequence_number : int- The sequence number the next event will carry.
-
sender : ProgressEventSender | None- The sender this reporter hands its events to, if any.Read-only: a caller composing a second reporter for the same run shares this one rather than holding the sender separately.
ImagingDatasetEventReporter
class ImagingDatasetEventReporter( sender: ProgressEventSender | None, *, dataset_id: str, dataset_owner_id: str, preprocessing_id: str, start_sequence_number: int = 0,):Reports the progress of one preprocessing pass over an imaging dataset.
Arguments
sender: Delivers the events. None makes the reporter a no-op.dataset_id: The dataset being preprocessed.dataset_owner_id: The username of the dataset's owner, which must be the user the sender authenticates as.preprocessing_id: The UUIDv7 identifying this preprocessing pass, fromuuid6.uuid7().start_sequence_number: The sequence number of the first event. A pass reported from more than one process continues from the previous reporter'snext_sequence_number.
Ancestors
Variables
next_sequence_number : int- The sequence number the next event will carry.
-
sender : ProgressEventSender | None- The sender this reporter hands its events to, if any.Read-only: a caller composing a second reporter for the same run shares this one rather than holding the sender separately.
Methods
error
def error(self, reason: LiteralString, exc: BaseException | None = None) ‑> None:Report that the preprocessing pass failed.
Arguments
reason: A fixed description of what failed. Must be a string literal, so no data can reach it.exc: The exception behind the failure. Only its class name is sent.
file_metadata_completed
def file_metadata_completed( self, files_processed_count: int, files_skipped_count: int, cache_entries_used_count: int = 0,) ‑> None:Report that the file metadata step has completed.
Arguments
files_processed_count: Files the step processed.files_skipped_count: Files the step skipped.cache_entries_used_count: Files answered from the cache.
file_metadata_started
def file_metadata_started(self, files_count: int) ‑> None:Report that the file metadata step has started.
Arguments
files_count: The number of files the step will look at.
preprocessing_started
def preprocessing_started( self, trigger: DatasetPreprocessingStartTrigger.ValueType,) ‑> None:Report that the preprocessing pass has started.
Arguments
trigger: What started the pass.
scan_metadata_completed
def scan_metadata_completed( self, files_processed_count: int, files_skipped_count: int, cache_entries_used_count: int = 0,) ‑> None:Report that the scan metadata step has completed.
Arguments
files_processed_count: Files the step processed.files_skipped_count: Files the step skipped.cache_entries_used_count: Files answered from the cache.
scan_metadata_started
def scan_metadata_started(self, files_count: int) ‑> None:Report that the scan metadata step has started.
Arguments
files_count: The number of files the step will look at.
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:
submitonly 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_failuresfailed sends in a row, or the first send rejected asUNIMPLEMENTED(a message service without the progress event rpcs) or raisingProgressEventsUnavailableError, the sender disables itself for the rest of its life and discards anything queued. - When the
progress_events_enabledsetting 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 theprogress_events_enabledsetting.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.
Ancestors
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.
TaskRunEventReporter
class TaskRunEventReporter( sender: ProgressEventSender | None, *, dataset_id: str, dataset_owner_id: str, project_id: str, task_run_id: str, batch_event_interval_seconds: float = 30.0, clock: Callable[[], float] = <built-in function monotonic>,):Reports the progress of one task run.
Batch events are coalesced per step: a step's batch event is sent at most
once per batch_event_interval_seconds, and the latest batch held back is
sent before the step's completion event, so consumers always see the final
batch.
Arguments
sender: Delivers the events. None makes the reporter a no-op.dataset_id: The dataset the task runs on.dataset_owner_id: The username of the dataset's owner, which must be the user the sender authenticates as.project_id: The project the task belongs to.task_run_id: The UUIDv7 identifying this task run, fromuuid6.uuid7().batch_event_interval_seconds: The shortest time between two batch events for the same step. One interval applies to every step in the run, and each step's interval is timed separately.clock: Monotonic clock in seconds, for tests.
Ancestors
Variables
next_sequence_number : int- The sequence number the next event will carry.
-
sender : ProgressEventSender | None- The sender this reporter hands its events to, if any.Read-only: a caller composing a second reporter for the same run shares this one rather than holding the sender separately.
Methods
completed
def completed(self) ‑> None:Report that the task run has completed.
deferred
def deferred(self) ‑> None:Report that the task run was deferred until the dataset is ready.
error
def error(self, reason: LiteralString, exc: BaseException | None = None) ‑> None:Report that the task run failed.
Arguments
reason: A fixed description of what failed. Must be a string literal, so no data can reach it.exc: The exception behind the failure. Only its class name is sent.
started
def started(self, trigger: TaskRunStartTrigger.ValueType) ‑> None:Report that the task run has started.
Arguments
trigger: What started the run.
step_batch_started
def step_batch_started(self, step_name: str, step_version: str, batch_index: int) ‑> None:Report that a step has started a batch, subject to coalescing.
Arguments
step_name: The step's name in the task template.step_version: The step's version.batch_index: The index of the batch.
step_completed
def step_completed( self, step_name: str, step_version: str, entries_created_count: int, items_skipped_count: int, cache_entries_used_count: int = 0,) ‑> None:Report that a step has completed.
Sends the step's latest held-back batch event first, if there is one.
Arguments
step_name: The step's name in the task template.step_version: The step's version.entries_created_count: Entries the step created.items_skipped_count: Items the step skipped.cache_entries_used_count: Items answered from the cache.
step_started
def step_started( self, step_name: str, step_version: str, batches_count: int, batch_size: int,) ‑> None:Report that a step has started.
Arguments
step_name: The step's name in the task template.step_version: The step's version.batches_count: The number of batches the step will run.batch_size: The number of items in each batch.