Skip to main content

rerun_requests

Requests for lineages to be rerun when next appropriate.

A rerun request is a timestamp, a scope and optionally one lineage. The periodic-rerun sweep makes every lineage the request covers due on its next tick, as a BackgroundTrigger.PERIODIC_RERUN through every gate the cron passes, and setup moves that rerun's freshness horizon to the request so it re-sweeps everything the cron occurrence's run already covered. A request counts only until a cron occurrence passes after it: from then on the cron's own rerun and horizon carry it.

A scope is a named predicate over a lineage's background DAG. all covers every lineage and ehr every lineage that reads the EHR; a caller with its own notion of which lineages a request is for registers it with register_rerun_scope and passes the name to request_rerun. A request for one lineage passes its lineage_key_for key as well.

A long-lived caller judging the same lineages again and again (the sweep, on every tick) holds a RerunScopeMemo, so each lineage is judged once per stored spec and scope rather than once per tick.

Module​

Functions​

applicable_rerun_at​

def applicable_rerun_at(    record: FlowSpecRecord,    requests: Sequence[RerunRequestRecord],    *,    after: datetime | None = None,) ‑> datetime | None:

Return when the latest request covering a lineage was made.

A request covers the lineage when it is for every lineage or for this one, and its scope holds for the lineage's DAG. The lineage key and the time are checked first; the DAG is built only when a request survives them, and at most once. A scope that is not registered, or whose predicate raises, covers nothing, and is logged at warning.

Blocking: may build the lineage's DAG.

Arguments

  • record: The lineage's flow spec record.
  • requests: The requests to judge it against.
  • after: Requests at or before this time are ignored; None ignores none.

Returns The request time in UTC, or None when no request covers it.

applicable_rerun_at_for_dag​

def applicable_rerun_at_for_dag(    dag: BackgroundDAG, lineage_key: str, requests: Sequence[RerunRequestRecord],) ‑> datetime | None:

applicable_rerun_at for a caller already holding the lineage's DAG.

Arguments

  • dag: The lineage's background DAG.
  • lineage_key: The lineage's lineage_key_for key.
  • requests: The requests to judge it against.

Returns The request time in UTC, or None when no request covers it.

dag_uses_ehr​

def dag_uses_ehr(dag: BackgroundDAG) ‑> bool:

Return whether dag reads the EHR patient listing or has an EHR step.

The ehr scope: what makes a lineage subject to a pod-wide EHR refresh request.

Arguments

  • dag: The parsed background DAG.

Returns Whether any step reads $ehr_patients or declares the EHR resource.

latest_covering_request​

def latest_covering_request(    lineage_key: str,    requests: Iterable[RerunRequestRecord],    in_scope: Callable[[str], bool],    *,    after: datetime | None = None,) ‑> datetime | None:

Return the latest request covering a lineage, asking in_scope lazily.

Requests for another lineage, and those at or before after, are dropped before in_scope is asked; it is asked at most once per scope, newest request first, and not at all once a request is found.

Arguments

  • lineage_key: The lineage's lineage_key_for key.
  • requests: The requests to judge it against.
  • in_scope: Whether the lineage is in the named scope.
  • after: Requests at or before this time are ignored; None ignores none.

Returns The request time in UTC, or None when no request covers it.

lineage_in_scope​

def lineage_in_scope(record: FlowSpecRecord, scope: str) ‑> bool:

Return whether a lineage is in scope.

Arguments

  • record: The lineage's flow spec record.
  • scope: A registered scope's name.

Returns Whether a request in scope covers it; False for a spec that cannot be built, or when the scope's predicate raises.

Raises

  • ValueError: If scope is not registered.

lineage_key_for​

def lineage_key_for(project_id: str, datasource_name: str) ‑> str:

Return the key a request for one lineage stores.

JSON rather than a joined string, so no project or datasource name can make two lineages share a key.

Arguments

  • project_id: The lineage's project.
  • datasource_name: The lineage's datasource.

Returns The lineage key.

register_rerun_scope​

def register_rerun_scope(name: str, predicate: RerunScope) ‑> None:

Register a scope that rerun requests can be made in.

Registering the same predicate under the same name again does nothing, so a module registering its scope at import is safe to import twice.

A request is judged in two processes: the sweep in the pod process, and setup in the background run's (Prefect) child process. The registering module must be imported by both, or the process that has not loaded it cannot judge the request and ignores it, with a warning: in the pod no lineage is made due, in the child the run plans as if no request stood.

Arguments

  • name: The scope's name, as requests store it.
  • predicate: Whether a lineage's DAG is in the scope. Called by the sweep off the event loop. An exception counts as "not in the scope" and is logged.

Raises

  • ValueError: If name is already registered with a different predicate.

request_rerun​

def request_rerun(    cache: CacheProtocol,    *,    scope: str,    now: datetime,    cooldown: timedelta,    reason: str,    lineage_key: str | None = None,) ‑> RerunRequestOutcome:

Ask for lineages to be rerun when next appropriate, unless one stands.

A request inside cooldown of the standing one for the same scope and lineage key (or at a clock earlier than it) writes nothing and reports the standing request.

Blocking: reads the cache and builds each candidate lineage's DAG, and holds a process-wide lock while it does, so call it from a worker thread rather than an event loop. Concurrent callers are taken one at a time.

Arguments

  • cache: The pod's background cache.
  • scope: A registered scope's name.
  • now: The request time; must be timezone-aware.
  • cooldown: How long a standing request absorbs another.
  • reason: Why the request is made, for support.
  • lineage_key: The one lineage to rerun (lineage_key_for), or None for every lineage in scope.

Returns The outcome, carrying the request now in force.

Raises

  • ValueError: If scope is not registered or now is naive.

Classes​

RerunRequestOutcome​

class RerunRequestOutcome(applied: bool, requested_at: datetime, lineages_due: int):

What a rerun request did.

Attributes

  • applied: Whether the request was recorded; False when one inside the cooldown already stood.
  • requested_at: When the request now in force was made, in UTC.
  • lineages_due: Lineages that request covers.

Variables​

  • applied : bool
  • lineages_due : int

RerunScopeMemo​

class RerunScopeMemo():

Scope verdicts for a caller that judges the same lineages repeatedly.

A verdict depends only on the lineage's stored spec and the scope, so it is reached once per stored spec and scope (keyed by the spec's content hash, so a relink with a changed spec is judged afresh): a lineage's DAG is built for the first judgement in each scope, and a spec that does not build is tried once. Verdicts are kept, not DAGs. A lineage that could not be judged is remembered too, with why, so the caller can report it on its own schedule; nothing here logs above debug.

Holds one entry per lineage; retain drops the ones no longer stored. Not thread-safe: one caller at a time.

Methods​


judge​

def judge(    self, record: FlowSpecRecord, scope: str, *, dag: BackgroundDAG | None = None,) ‑> ScopeVerdict:

Return whether a lineage is in scope, remembering the verdict.

Blocking: the first judgement of a stored spec in a scope builds its DAG, unless dag is given or the spec is already known not to build.

Arguments

  • record: The lineage's flow spec record.
  • scope: A scope's name; one not registered here is unjudged.
  • dag: The lineage's DAG, when the caller already holds it; used instead of building one.

Returns The verdict.

retain​

def retain(self, records: Iterable[FlowSpecRecord]) ‑> None:

Forget every lineage not among records.

Arguments

  • records: The lineages still stored.

ScopeVerdict​

class ScopeVerdict(in_scope: bool, unjudged: str | None = None, detail: str = ''):

Whether a lineage is in a scope.

Attributes

  • in_scope: Whether a request in the scope covers the lineage.
  • unjudged: Why it could not be judged, when it could not: spec_builds (its stored spec does not build), scope_registered (no scope of that name is registered in this process) or scope_judges (the scope's predicate raised). A lineage that cannot be judged is in no scope. None when it was judged.
  • detail: What went wrong, when it could not be judged.

Variables​

  • detail : str
  • in_scope : bool
  • unjudged : str | None