schedr.context¶
The run’s context, dag_ctx, and the running task’s own, task_ctx.
dag_ctx is the current run’s information.
In the DAG body, dag_ctx.run.planned_for is a reference, filled in at run time; it
can be passed to tasks and commands, but not used as a value in the DAG file. Inside a
running task (while the runner calls its function) the same expression gives the value
(see archive/PROJECT.md, Run parameters and the DAG context). Anywhere else it fails (“must be run
inside a schedr task”); tests set values with schedr.testing.task_context(...).
dag_ctx.params.<field> is the same for the run’s parameters (Dag(params=...)), and
dag_ctx.params(ReportParams) gives them typed by the class.
- class schedr.context.DagCtxReference(field)¶
Bases:
objectA field of the run’s context, as a reference in the DAG body.
- Parameters:
field (DagCtxField)
- field¶
Which field.
- class schedr.context.RunValues(dag_id, run_id, kind, planned_for, since, params=<factory>, params_class=None, from_script=False, event=None)¶
Bases:
objectThe run’s context as values, inside a running task.
- Parameters:
dag_id (str)
run_id (str)
kind (str)
planned_for (datetime)
since (datetime)
params (Mapping[str, Any])
params_class (type | None)
from_script (bool)
event (Mapping[str, Any] | None)
- dag_id¶
The DAG’s ID.
- Type:
str
- run_id¶
The run’s ID.
- Type:
str
- kind¶
The run’s kind.
- Type:
str
- planned_for¶
The moment the run is for.
- Type:
datetime.datetime
- since¶
The start of the run’s window.
- Type:
datetime.datetime
- params¶
The run’s parameters, as stored (decision 138).
- Type:
collections.abc.Mapping[str, Any]
- event¶
The event that started it, as stored;
Nonefor other runs.- Type:
collections.abc.Mapping[str, Any] | None
- from_script¶
Read by a plain script (
schedr.script), which doesn’t know the declared class: a field is decoded by its stored form, anddag_ctx.params(Cls)decodes intoClsstrictly without comparing it with the declaration.- Type:
bool
- exception schedr.context.SecretError(kind, message)¶
Bases:
ExceptionA secret the task reads can’t be read: not declared, not found, or its storage down.
- Parameters:
kind (str)
message (str)
- Return type:
None
- kind¶
secret_denied,secret_not_foundorsecret_unavailable. Raised out of the task, the attempt fails with this kind (onlysecret_unavailableis retried).
- type schedr.context.SecretReader = Callable[[SecretRef], str | dict[str, str]]¶
Reads one secret: a key’s value, or a whole secret’s keys and values.
- type schedr.context.ConnectionEnter = Callable[[Mapping[str, object]], AbstractContextManager[object]]¶
A connection’s function called with its rendered values (stored JSON, by parameter).
- type schedr.context.ConnectionOpener = Callable[[str, ConnectionEnter], AbstractContextManager[object]]¶
Opens a connection by name: its values rendered by the agent and given to
enter, or a test’s double.
- type schedr.context.HandleAdder = Callable[[object, str, object], None]¶
Registers a cleanup handle: the handle, its type’s name and its encoded value.
- type schedr.context.ApiCaller = Callable[[str, str, object, str | None], tuple[int, object]]¶
Makes a request to schedr’s REST API as the task (
schedr.api): the method, the path and query, the JSON body (None: none) and theIdempotency-Key; gives the status and the JSON answer.
- class schedr.context.TaskValues(task_id, try_number, map_index, secrets=None, connections=None, handles=None, api=None, first_started=None)¶
Bases:
objectThe running task’s own context (decision 84).
- Parameters:
task_id (str)
try_number (int)
map_index (int | None)
secrets (SecretReader | None)
connections (ConnectionOpener | None)
handles (HandleAdder | None)
api (ApiCaller | None)
first_started (datetime | None)
- task_id¶
The task’s ID.
- Type:
str
- try_number¶
The attempt’s try number, from 1.
- Type:
int
- map_index¶
Which instance of a mapped task, from 0;
Nonefor an unmapped task.- Type:
int | None
- secrets¶
Reads a secret the task declares (
task_ctx.secret(...));None: it can read none.- Type:
schedr.context.SecretReader | None
- connections¶
Opens a connection (
with name():);None: none here.- Type:
- handles¶
Registers a cleanup handle (
cleanup_handle);None: it can register none.- Type:
schedr.context.HandleAdder | None
- api¶
Calls schedr’s REST API as the task (
schedr.api);None: it can’t.- Type:
schedr.context.ApiCaller | None
- first_started¶
When a wait step’s first try started, for one tried again (
delaycounts from it);Noneon its first try.- Type:
datetime.datetime | None
- schedr.context.declaring(declared, connections=())¶
Makes what a
@taskfunction declares what a test’s call of it may use.declaredare the secretstask_ctx.secretmay read;connectionsthose it may open, in the block.- Parameters:
declared (tuple[SecretDecl, ...]) – The
@taskfunction’ssecrets=.connections (tuple[str, ...]) – Its
connections=.
- Yields:
Nothing.
- Return type:
Iterator[None]
- schedr.context.declared_connections()¶
The
connections=of the@taskfunction a test is calling;Noneoutside one.- Returns:
The connections’ names, or
None.- Return type:
tuple[str, …] | None
- schedr.context.declared_secrets()¶
The
secrets=of the@taskfunction a test is calling;Noneoutside one.- Returns:
The declarations, or
None.- Return type:
tuple[SecretDecl, …] | None
- schedr.context.use_script_values(load)¶
Makes
dag_ctxgiveload()’s values outside a running task and a DAG body.schedr.scriptcalls it when imported;Nonestops it (tests).- Parameters:
load (Callable[[], RunValues] | None) – Gives the run’s context (loaded once, on first use), or raises
RuntimeErroroutside a task.- Return type:
None
- schedr.context.running(values, task=None)¶
Makes
dag_ctxgive values inside the block, andtask_ctxthe task’s own.The runner calls a task’s function inside it; references stand for values there.
- Parameters:
values (RunValues) – The run’s context.
task (TaskValues | None) – The task’s own context.
- Yields:
Nothing.
- Return type:
Iterator[None]
- class schedr.context.RunContext¶
Bases:
objectdag_ctx.run: the run’s information.Each field is a reference in the DAG body and a value inside a running task; type checkers read it as the value in both (as they read a task call), so it passes to a task taking that type. Reading a field anywhere else raises
RuntimeError.- property id: str¶
The run’s ID (a UUIDv7).
- property kind: str¶
scheduled,manual,backfill,chainedorevent.- Type:
The run’s kind
- property planned_for: datetime¶
The moment the run is for (a fire time for scheduled runs).
- property since: datetime¶
The start of the run’s window, which is
(since, planned_for].
- class schedr.context.EventContext¶
Bases:
objectdag_ctx.event: the event that started the run, in an event-triggered DAG.As
dag_ctx.run, each field is a reference in the DAG body and a value in a running task.- property key: str¶
Its key.
- property data: Any¶
Its data, read by the type of the parameter it is passed to.
- property at: datetime¶
When it happened; if it didn’t say, when schedr received it.
- class schedr.context.DagContext¶
Bases:
objectdag_ctx: the current run’s context.- property event: EventContext¶
The event that started the run, in an event-triggered DAG.
- property run: RunContext¶
The run’s information.
- property dag_id: str¶
The DAG’s ID.
- property params: Params¶
dag_ctx.params.region, ordag_ctx.params(Cls).A reference in the DAG body (its path checked against the declared class), the values inside a running task.
- Raises:
RuntimeError – Outside a DAG body and a running task.
TypeError – In a DAG body that declares no params.
- Type:
The run’s parameters
- class schedr.context.Params¶
Bases:
objectdag_ctx.params: a reference in the DAG body, the values in a running task.Statically a field is untyped (
Any), as an output’s path is (archive/PROJECT.md, Run parameters);dag_ctx.params(ReportParams)is typed by the class passed.
- class schedr.context.ParamsReference(builder, path)¶
Bases:
ParamsThe run’s parameters, or a part of them, as a reference in the DAG body.
- Parameters:
builder (DagBuilder)
path (tuple[str | int, ...])
- class schedr.context.ParamsValues(values)¶
Bases:
ParamsThe run’s parameters inside a running task, decoded into the declared class.
- Parameters:
values (RunValues)
- schedr.context.dag_ctx = <schedr.context.DagContext object>¶
dag_ctx.run.planned_for,dag_ctx.run.since, …- Type:
The current run’s context
- schedr.context.current_task()¶
The running task’s own context, if a task is running.
- Returns:
Its values, or
Noneoutside a running task.- Return type:
TaskValues | None
- schedr.context.HANDLES_PARAMETER = 'handles'¶
The cleanup parameter that takes the attempt’s handles.
- schedr.context.handle_classes(annotation)¶
The classes a cleanup’s
handlesdeclares:list[A]orlist[A | B].- Parameters:
annotation (object) – The parameter’s resolved annotation.
- Returns:
The dataclasses, in the order written.
- Raises:
TypeError – For any other annotation, or two classes of one name (handles are stored with their class’s name).
- Return type:
tuple[type, …]
- schedr.context.cleanup_handle(handle)¶
Registers what this attempt’s cleanup must clean up, like Go’s
defer.Call it before the work it stands for: it reaches the agent before this returns, so an attempt killed right after still has it. The cleanup gets every handle of the attempt, in this order, however the attempt ended; a handle is never released.
- Parameters:
handle (object) – A dataclass instance, of a type the cleanup’s
handlesdeclares. Its values are masked like the task’s logs.- Raises:
TypeError – For anything but a dataclass instance.
RuntimeError – Outside a running task, or when the agent refuses it (no
cleanup=, a type the cleanup doesn’t take, too many handles, in a cleanup).
- Return type:
None
- async schedr.context.acleanup_handle(handle)¶
cleanup_handle()for async code: it doesn’t block the event loop while it waits.- Parameters:
handle (object) – A dataclass instance, as for
cleanup_handle().- Return type:
None
- class schedr.context.TaskContext¶
Bases:
objecttask_ctx: the running task’s own context (decision 84), values only.Reading it outside a running task (in the DAG body too) raises
RuntimeError.- property task_id: str¶
The task’s ID.
- property try_number: int¶
The attempt’s try number, from 1 (retries and requeues count).
- property map_index: int | None¶
Which instance of a mapped task this is, from 0;
Nonefor an unmapped task.
- secret(path: str, key: str, *, storage: str | None = None) str¶
- secret(path: str, *, storage: str | None = None) dict[str, str]
A secret the task declares in
secrets=, read now:secret('etl/api', 'token').One declared by name was fetched before the task started; one under a declared
SecretPrefixis fetched now. Its value is masked in the task’s log and output.- Parameters:
path (str) – The secret’s path,
etl/api.key (str | None) – The key; none for the whole secret.
storage (str | None) – The storage;
Nonefor the default one.
- Returns:
A key’s value (
str), or the whole secret’s keys and values (dict[str, str]).- Raises:
SecretError – When it can’t be read (not declared, not found, the storage down).
ValueError – For a bad path or key, or a path with
#(that issecret_value).
- Return type:
str | dict[str, str]
- secret_value(spec, *, storage=None)¶
One value the task declares, written
path#key:secret_value('etl/api#token').- Parameters:
spec (str) –
path#key.storage (str | None) – The storage;
Nonefor the default one.
- Returns:
The value.
- Raises:
SecretError – When it can’t be read (not declared, not found, the storage down).
ValueError – For a spec without
#key(the whole secret issecret(path)) or a bad one.
- Return type:
str
- async asecret(path: str, key: str, *, storage: str | None = None) str¶
- async asecret(path: str, *, storage: str | None = None) dict[str, str]
secret()for async code: it doesn’t block the event loop while it waits.- Parameters:
path (str) – The secret’s path,
etl/api.key (str | None) – The key; none for the whole secret.
storage (str | None) – The storage;
Nonefor the default one.
- Returns:
A key’s value (
str), or the whole secret’s keys and values.- Return type:
str | dict[str, str]
- async asecret_value(spec, *, storage=None)¶
secret_value()for async code: it doesn’t block the event loop while it waits.- Parameters:
spec (str) –
path#key.storage (str | None) – The storage;
Nonefor the default one.
- Returns:
The value.
- Return type:
str
- schedr.context.task_ctx = <schedr.context.TaskContext object>¶
task_ctx.map_index,task_ctx.try_number, …- Type:
The running task’s own context