schedr.runner.task

Task mode: run one Python task attempt.

The runner imports the DAG file (the agent’s copy of the root’s latest code) under the same module name as parse mode, finds the task’s function, builds its arguments from the stored values, calls it with dag_ctx giving values, and sends back the encoded output or an error summary. It doesn’t enforce timeouts or watch memory: the agent does (see archive/PROJECT.md, Rust/Python integration).

type schedr.runner.task.EntriesOf = Callable[[str, Path, str], list[DefinedDag]]

Gives a DAG file’s DAGs: its root’s name and folder, and its path in it.

schedr.runner.task.SUCCESS = 'SUCCESS'

The outcome of a task without outcomes of its own (DefaultOutcome.SUCCESS).

exception schedr.runner.task.TaskInputError

Bases: Exception

A stored input or argument that can’t be given to the task’s function.

exception schedr.runner.task.TaskSetupError

Bases: Exception

The task can’t be started: its DAG or function isn’t in the DAG file.

schedr.runner.task.resolve(reference, inputs, run, outcomes=None)

The stored value a reference stands for.

Parameters:
  • reference (Mapping[str, Any]) – A reference as stored ({"kind": "output", "task": ..., "path": [...], "on": [...]}, {"kind": "outcome", "task": ...} or {"kind": "dag_ctx", "field": ...}).

  • inputs (Mapping[str, Any]) – The upstream outputs, by task ID, as stored JSON.

  • run (Mapping[str, Any]) – The request’s run information.

  • outcomes (Mapping[str, Any] | None) – The upstream tasks’ outcomes, by task ID: a name, or None for one that was skipped.

Returns:

The stored value (planned_for / since tagged as $datetime); None for a reference whose edge wasn’t taken (its upstream skipped, or narrowed by .on() to outcomes that didn’t happen); the outcome’s name for an outcome reference.

Raises:

TaskInputError – For a missing input or path, or a truncated stream.

Return type:

Any

schedr.runner.task.SECRETS = '$secrets'

Where execute() puts the request’s fetched secrets in the run mapping.

schedr.runner.task.CONNECTION_NOT_FOUND = 'connection_not_found'

The error kind of a connection the agent doesn’t know (not in the DAG folder’s registry).

schedr.runner.task.CONNECTION_DENIED = 'connection_denied'

The error kind of a connection the task doesn’t declare in connections=.

schedr.runner.task.secret_key(name)

A secret’s name as stored, as a key.

{"storage": ..., "path": ..., "key": ..., "default": ...}.

Parameters:

name (Mapping[str, Any]) – The stored name (or a secret reference).

Returns:

(storage, path, key, default).

Return type:

tuple[str | None, str, str | None, str | None]

schedr.runner.task.secret_reader(channel)

Reads a secret through the agent (secret_get), one request at a time.

Parameters:

channel (Asking) – The channel to the agent.

Returns:

The reader task_ctx.secret calls.

Return type:

SecretReader

schedr.runner.task.connection_opener(channel)

Opens connections, their values asked of the agent (connection_get).

The agent renders them from what the parser recorded; one request at a time.

Parameters:

channel (Asking) – The channel to the agent.

Returns:

The opener with name(): calls.

Return type:

ConnectionOpener

schedr.runner.task.PROCESS_RESULT = frozenset({'exit_code', 'stderr', 'stderr_truncated', 'stdout', 'stdout_truncated'})

only its streams are ever truncated (B75).

Type:

The fields of a stored ProcessResult

schedr.runner.task.render(parts, inputs, run, outcomes=None)

Renders a stored template to text, the way the agent renders command lines.

Parameters:
  • parts (list[Any]) – The template’s parts ({"text": ...} and {"ref": {...}}).

  • inputs (Mapping[str, Any]) – The upstream outputs.

  • run (Mapping[str, Any]) – The run information.

  • outcomes (Mapping[str, Any] | None) – The upstream tasks’ outcomes (see resolve()).

Returns:

The text.

Raises:

TaskInputError – For a reference that can’t be resolved or written as text.

Return type:

str

schedr.runner.task.build(value, annotation, inputs, run, outcomes=None)

An argument’s Python value from its stored form, decoded against the parameter type.

Parameters:
  • value (Mapping[str, Any]) – The stored value (literal, ref, template, list or map).

  • annotation (object) – The parameter’s annotation.

  • inputs (Mapping[str, Any]) – The upstream outputs.

  • run (Mapping[str, Any]) – The run information.

  • outcomes (Mapping[str, Any] | None) – The upstream tasks’ outcomes (see resolve()).

Returns:

The value to pass.

Raises:

TaskInputError – If a stored value doesn’t fit the parameter.

Return type:

Any

schedr.runner.task.member_named(annotation, outcome)

The member of a union annotation named by an outcome.

A member is named by its class, through Annotated and generic parameters (B123), as outcome detection names it.

Parameters:
  • annotation (object) – The receiving type.

  • outcome (object) – The recorded outcome’s name (anything else picks nothing).

Returns:

The member, else the annotation as it is.

Return type:

object

schedr.runner.task.mapped_arguments(inputs, hints, run=None, upstream_outcomes=None)

A mapped task instance’s own arguments: the elements the master resolved for it.

They come with the inputs under $expand (decision 76); one taken element by element from a mapped upstream task has that instance’s outcome under $expand_outcomes, which picks the member of a union to decode it as. An element written in the file with a t-string or a dag_ctx value comes in its stored form under $expand_values, built here as an argument is (decision 118).

Parameters:
  • inputs (Mapping[str, Any]) – The instance’s inputs.

  • hints (Mapping[str, Any]) – The task function’s type hints.

  • run (Mapping[str, Any] | None) – The run information, for $expand_values.

  • upstream_outcomes (Mapping[str, Any] | None) – The upstream tasks’ outcomes, for $expand_values.

Returns:

The arguments by parameter name (none for a task that isn’t mapped).

Raises:

TaskInputError – If an element doesn’t fit its parameter.

Return type:

dict[str, Any]

schedr.runner.task.run_values(run, params_class=None)

The dag_ctx values for a request’s run information.

Parameters:
  • run (Mapping[str, Any]) – The run information.

  • params_class (type | None) – The class the DAG declares its parameters with.

Returns:

The values.

Return type:

RunValues

schedr.runner.task.find_function(request)

Imports the DAG file and finds the task’s function.

Parameters:

request (Mapping[str, Any]) – The task message.

Returns:

The task’s function.

Return type:

Callable[[…], Any]

schedr.runner.task.find_task(request)

Imports the DAG file and finds the task’s function and its direct downstream tasks.

Parameters:

request (Mapping[str, Any]) – The task message.

Returns:

The function, and the IDs of the tasks right after it (a branch chooses among them).

Return type:

tuple[Callable[[…], Any], frozenset[str]]

schedr.runner.task.find_task_of_dag(request)

As find_task(), with the class the DAG declares its parameters with.

Parameters:

request (Mapping[str, Any]) – The task message.

Returns:

The function, the IDs of the tasks right after it, and the params class.

Raises:

TaskSetupError – If the DAG or the task isn’t in the file.

Return type:

tuple[Callable[[…], Any], frozenset[str], type | None]

schedr.runner.task.find_entry(request, entries_of=<function import_dag_entries>)

The DAG of a task request, its file imported, with the task’s function in it.

Parameters:
  • request (Mapping[str, Any]) – The task message.

  • entries_of (EntriesOf) – Gives a DAG file’s DAGs (imports it; a watcher host only once).

Returns:

The DAG.

Raises:

TaskSetupError – If the DAG or the task isn’t in the file.

Return type:

DefinedDag

schedr.runner.task.chosen_ids(result, downstream, dag_id)

The task IDs a @branch function’s result chose.

Parameters:
  • result (object) – What it returned: a task handle, or a list of them.

  • downstream (frozenset[str]) – Its direct downstream tasks, the ones it may choose.

  • dag_id (str) – The DAG being run: a handle of another DAG’s task is refused, though a task of this DAG has its ID (B126).

Returns:

The IDs, in the order returned, without repeats.

Raises:

CodecError – For None, something that isn’t a handle, or a task it can’t choose.

Return type:

list[str]

schedr.runner.task.failure(error, kind, message=None)

A task_failed message.

Parameters:
  • error (BaseException) – The exception.

  • kind (str | None) – The failure kind schedr recognises, or None for the task’s own exception.

  • message (str | None) – A message replacing the exception’s, if given.

Returns:

The message.

Return type:

dict[str, Any]

async schedr.runner.task.execute_trigger(request, channel, entries)

Runs a trigger in the watcher host’s loop, until it is stopped or it fails.

Parameters:
  • request (Mapping[str, Any]) – The task, as for execute(), with what it starts from in trigger.

  • channel (Asking) – The watcher’s channel to the agent.

  • entries (EntriesOf) – Gives a DAG file’s DAGs.

Returns:

a trigger only ends with an error, or by being cancelled.

Return type:

task_failed

schedr.runner.task.execute(request, imported=<function <lambda>>, secrets=None, channel=None)

Runs a task request and returns the message to send.

Parameters:
  • request (Mapping[str, Any]) – The task message.

  • imported (Callable[[], None]) – Called once the DAG file is imported and the arguments are built, just before the task’s function (not when that fails).

  • secrets (SecretReader | None) – Reads a secret for task_ctx.secret (through the agent); None: the task reads none.

  • channel (Channel | None) – The channel to the agent, for the connections it opens; None: none.

Returns:

task_succeeded with the encoded output, or task_failed.

Return type:

dict[str, Any]

async schedr.runner.task.execute_watcher(request, channel, entries, moving=None, go=None)

Runs a wait step in the watcher host’s loop, its file imported at most once there.

Parameters:
  • request (Mapping[str, Any]) – The task, as for execute().

  • channel (Asking) – The watcher’s channel to the agent.

  • entries (EntriesOf) – Gives a DAG file’s DAGs.

  • moving (Event | None) – Set when a poke is to stop between calls, for new code.

  • go (Callable[[], Awaitable[int | None]] | None) – For a poke moving here: everything is ready, and this waits for the old one’s stop. Gives what is left of its max_wait, in milliseconds.

Returns:

task_succeeded with the encoded output, task_failed, or watcher_moved.

Return type:

dict[str, Any]

schedr.runner.task.cleanup_call(entry, function, task_id, arguments, handles)

The cleanup of a task, with the task’s inputs it names and the attempt’s handles.

Parameters:
  • entry (DefinedDag) – The task’s DAG.

  • function (Callable[[...], Any]) – The task’s function, for the defaults of parameters no call gives.

  • task_id (str) – The task’s ID.

  • arguments (Mapping[str, object]) – The task’s arguments, built.

  • handles (Sequence[Mapping[str, Any]]) – The attempt’s handles, in registration order.

Returns:

The cleanup function and its arguments.

Raises:
Return type:

tuple[Callable[[…], Any], dict[str, object]]

schedr.runner.task.tell_note(note)

Prints what the agent says of an API request to the task’s log (stderr), once per note.

Parameters:

note (object) – The answer’s note: a secret in the request masked; None: nothing.

Return type:

None

schedr.runner.task.api_caller(channel)

Calls schedr’s REST API through the agent (api_call), one request at a time.

Parameters:

channel (Asking) – The channel to the agent.

Returns:

The caller schedr.api uses.

Return type:

ApiCaller

schedr.runner.task.handle_adder(channel)

Registers cleanup handles with the agent (handle_add), one request at a time.

Parameters:

channel (Asking) – The channel to the agent.

Returns:

The adder cleanup_handle calls.

Return type:

HandleAdder

schedr.runner.task.run_connection_test(request, imported=<function <lambda>>, channel=None)

Runs schedr connections test NAME on this worker (decision 198).

Imports the file defining the connection, opens it and runs its check (or opens and closes it, without one).

Parameters:
  • request (Mapping[str, Any]) – The task message, its connection_test the connection’s name.

  • imported (Callable[[], None]) – Called once the file is imported.

  • channel (Channel | None) – The channel to the agent, for the connection’s values.

Returns:

task_succeeded with no output, or task_failed.

Return type:

dict[str, Any]

schedr.runner.task.defined_connection(module, name)

The connection name the module defines at its top level.

Parameters:
  • module (str) – The module’s name.

  • name (str) – The connection’s.

Returns:

The connection.

Raises:

TaskSetupError – When the module defines none by that name (any more).

Return type:

ConnectionBase[object]

schedr.runner.task.outcome_of(result, annotation)

The outcome a returned value stands for, with the output to store and its type.

Parameters:
  • result (object) – What the task’s function returned.

  • annotation (object) – Its return annotation.

Returns:

an Outcome member’s name with no output, an outcome union member’s class name ("None" for None) with the value, or SUCCESS.

Return type:

(outcome, output, type)

Raises:

CodecError – For a value that isn’t one of the declared outcomes.

schedr.runner.task.run_task(channel, request)

Handles a task request: runs it and reports the result.

Parameters:
  • channel (Channel) – The channel to the agent.

  • request (Mapping[str, Any]) – The task message.

Returns:

0 if the task succeeded, 1 otherwise.

Return type:

The exit code