schedr.operators¶
Operators: the task kinds other than Python functions.
CommandOperator runs an inline command; ScriptOperator a script declared in a
schedr.toml; EmptyOperator does nothing and is completed by the master. Operators
can’t be subclassed: reuse is by composition, e.g. a function that creates tasks (see
archive/PROJECT.md, Native API: no custom operators).
- class schedr.operators.Operator¶
Bases:
objectThe base of the operator classes; not for subclassing outside schedr.
- property task_id: str¶
The task’s ID.
- class schedr.operators.CommandSettings(env=None, options=<factory>, secrets=None, connections=None, cleanup=None, cleanup_handles=None, outcomes=None, output_type=<factory>, resource=None)¶
Bases:
objectWhat a command task takes besides its command line, as given.
- Parameters:
env (Mapping[str, object] | None)
options (TaskOptions)
secrets (SecretsArg)
connections (ConnectionsArg | Mapping[str, ConnectionBase[object] | str])
cleanup (str | Template | Sequence[object] | None)
cleanup_handles (Sequence[str] | None)
output_type (TypeDesc)
resource (ResourceRole | None)
- env¶
Extra environment variables.
- Type:
collections.abc.Mapping[str, object] | None
- options¶
Its options, but secrets and connections.
- Type:
schedr._build.TaskOptions
- secrets¶
Its
secrets=.- Type:
schedr._secrets.SecretsArg
- connections¶
Its
connections=.- Type:
schedr.connections.ConnectionsArg | collections.abc.Mapping[str, schedr.connections.ConnectionBase[object] | str]
- cleanup¶
Its per-task
cleanup=command.- Type:
str | string.templatelib.Template | collections.abc.Sequence[object] | None
- cleanup_handles¶
Its
cleanup_handles=.- Type:
collections.abc.Sequence[str] | None
- outcomes¶
Its
outcomes=.- Type:
type[schedr.outcomes.Outcome] | None
- output_type¶
Its output’s type: its
ProcessResult, unless it is a resource’s setup.
- resource¶
Its part in a resource.
- Type:
- schedr.operators.add_command(builder, task_id, command, settings)¶
Adds a command task, and the data edges from the outputs its templates reference.
- Parameters:
builder (DagBuilder) – The DAG.
task_id (str) – Its ID, qualified.
command (str | Template | Sequence[object]) – A shell string, or a list of arguments.
settings (CommandSettings) – The rest.
- Returns:
Its ID.
- Raises:
TypeError – For a command or environment value of the wrong type.
ValueError – For an invalid environment variable name.
- Return type:
str
- class schedr.operators.CommandOperator(task_id, command, *, env=None, retries=None, retry_delay=None, timeout=None, queue=None, max_output_size=None, outcomes=None, run_if=None, max_active=None, max_active_per_run=None, memory=None, hard_memory_limit=None, secrets=None, connections=None, cleanup=None, cleanup_timeout=None, cleanup_retries=None, cleanup_retry_delay=None, cleanup_anywhere=None, uses=None, cleanup_handles=None)¶
Bases:
_ProcessOperator,GenericRuns an inline command.
A string is run by a shell, and takes no placeholders (a t-string with one is refused): the shell would read a value as code. A list is a program and its arguments, run without a shell (each element one argument, so nothing needs quoting). List elements and
envvalues may be strings, t-strings or references, filled in at run time; a shell command reads values fromenv(command='./notify.sh "$URL"', env={"URL": out.report_url}). The output is aProcessResult;.stdoutand.stderrreference its parts.- Parameters:
task_id (str) – The task’s ID.
command (str | Template | Sequence[object]) – A shell string, or a list of arguments.
env (Mapping[str, object] | None) – Extra environment variables for the process.
retries (int | None) – How many times a failed attempt is retried.
retry_delay (timedelta | None) – The delay before each retry.
timeout (timedelta | None) – The attempt’s timeout.
queue (str | None) – The queue the task goes to.
max_output_size (int | None) – The output size limit in bytes (per stream).
outcomes (type[E] | None) – An
Outcomeenum: its members’ values are the exit codes meaning them (each a success with that outcome, any other code a failure). Default:SUCCESSon 0. Type checkers then read the operator asCommandOperator[Check]:.ontakes that enum’s members and.outcomeis one.run_if (RunIf | None) – When it runs, from its upstream edges (default
RunIf.NONE_FAILED).max_active (int | None) – At most this many running at once, across all runs (
None: no limit).max_active_per_run (int | None) – Accepted for symmetry with
@task; an operator isn’t mapped, so it limits nothing (aDagHintsays so).memory (int | str | None) – Its own memory budget (bytes or a size such as
"2GiB"), as@task’s.hard_memory_limit (bool | None) – Also cap the command hard at
memory, as@task’s.secrets (SecretsArg) – The secrets the command reads on demand, with
schedr task-ctx secret get(never in its environment):secrets['path']['key'],'path#key','path'(every key) orSecretPrefix(...). A secret it gets inenvneeds no declaring; one in the command line is refused (the process list shows it).connections (ConnectionsArg | Mapping[str, ConnectionBase[object] | str]) – The connections it reads with
schedr task-ctx connection NAME, each the@connectionfunction or its name; or a dict of prefix to connection ({"DB": warehouse}), each given as variables (DB_URL, …) and the files its tools read, written for the attempt and removed after it.cleanup (str | Template | Sequence[object] | None) – A command run after each attempt, whatever its end, like
finally: the same rules ascommand, the same environment. It reads what the attempt registered (schedr task-ctx cleanup add) withschedr task-ctx cleanup list.cleanup_timeout (timedelta | None) – How long one try of the cleanup may run (default: the worker’s
cleanup_timeout: 5 minutes, or less to fit in its grace period).cleanup_retries (int | None) – How many times a failed cleanup is retried (default 3).
cleanup_retry_delay (timedelta | None) – The delay before each cleanup retry (default 30 seconds).
cleanup_anywhere (bool | None) – Whether the cleanup may run on another worker when this one is lost (default no: only you know whether it depends on where the task ran).
cleanup_handles (Sequence[str] | None) – The handle types the attempt may register; default: any.
uses (Sequence[object] | None) – The resources it needs (decision 259).
- Raises:
RuntimeError – Outside
with Dag(...).TypeError – For a command or environment value of the wrong type.
ValueError – For an empty argument list or an invalid environment variable name.
- class schedr.operators.ScriptSettings(options, args=None, env=None, secrets=None, outcomes=None, output_type=<factory>, fallback=<factory>, resource=None)¶
Bases:
objectWhat a script task takes besides its declaration, as given.
- Parameters:
options (TaskOptions)
args (Sequence[object] | None)
env (Mapping[str, object] | None)
secrets (SecretsArg)
output_type (TypeDesc)
fallback (TaskOptions)
resource (ResourceRole | None)
- options¶
Its own options: they win over the declaration’s.
- Type:
schedr._build.TaskOptions
- args¶
Its
args=;None: the declared ones.- Type:
collections.abc.Sequence[object] | None
- env¶
Its
env=, merged over the declared ones.- Type:
collections.abc.Mapping[str, object] | None
- secrets¶
Its
secrets=, added to the declared ones.- Type:
schedr._secrets.SecretsArg
- outcomes¶
Its
outcomes=, instead of the declared ones.- Type:
type[schedr.outcomes.Outcome] | None
- output_type¶
Its output’s type: its
ProcessResult, unless it is a resource’s setup.
- fallback¶
What the declaration leaves out takes from here, then the DAG’s defaults.
- Type:
schedr._build.TaskOptions
- resource¶
Its part in a resource, if any.
- Type:
- schedr.operators.add_script(builder, task_id, job, settings)¶
Adds a script task from its declaration, and the data edges its templates reference.
- Parameters:
builder (DagBuilder) – The DAG.
task_id (str) – Its ID, qualified.
job (str) – The declaration’s reference.
settings (ScriptSettings) – The rest.
- Returns:
Its ID.
- Raises:
TypeError – For an argument or environment value of the wrong type, or a declaration this use can’t take.
ValueError – For an invalid environment variable name.
LookupError – For a declaration the worker parsing the file doesn’t have.
- Return type:
str
- class schedr.operators.ScriptOperator(task_id, job, *, args=None, env=None, secrets=None, retries=None, retry_delay=None, timeout=None, queue=None, max_output_size=None, outcomes=None, run_if=None, max_active=None, max_active_per_run=None, memory=None, hard_memory_limit=None, cleanup_timeout=None, cleanup_retries=None, cleanup_retry_delay=None, cleanup_anywhere=None, uses=None)¶
Bases:
_ProcessOperator,GenericRuns a script declared in a
schedr.tomlof a script root (decision 293).The declaration gives the script, its interpreter, its arguments, its environment, its secrets and its settings; the operator may change them:
argsreplaces the declared arguments,envadds variables (and replaces those of the same name),secretsadds declarations;retriesand the other settings win over the declaration’s, which win over the DAG’s defaults. The output is aProcessResult, as aCommandOperator’s.A declared
[outcomes]applies as it is: its exit codes are successes. To route on them, giveoutcomes=an enum, which replaces them. A declared[cleanup]runs after each attempt; the operator may change its settings, not its script.- Parameters:
task_id (str) – The task’s ID.
job (str) – The declaration:
"<root>:<path>/schedr.toml#<name>", or the absolute path of theschedr.tomlin a script root, then#<name>.args (Sequence[object] | None) – Its arguments instead of the declared ones; each a string, a t-string or a reference, one argument (never a shell string).
env (Mapping[str, object] | None) – Environment variables added to (or replacing) the declared ones.
secrets (SecretsArg) – Secrets declared besides the declared ones (as
CommandOperator’s).retries (int | None) – How many times a failed attempt is retried.
retry_delay (timedelta | None) – The delay before each retry.
timeout (timedelta | None) – The attempt’s timeout.
queue (str | None) – The queue the task goes to.
max_output_size (int | None) – The output size limit in bytes (per stream).
outcomes (type[E] | None) – An
Outcomeenum of exit codes, asCommandOperator’s, instead of the declared ones.run_if (RunIf | None) – When it runs, from its upstream edges (default
RunIf.NONE_FAILED).max_active (int | None) – At most this many running at once, across all runs (
None: no limit).max_active_per_run (int | None) – Accepted for symmetry; an operator isn’t mapped (a
DagHintsays so).memory (int | str | None) – Its own memory budget (bytes or a size such as
"2GiB"), as@task’s.hard_memory_limit (bool | None) – Also cap the script hard at
memory, as@task’s.cleanup_timeout (timedelta | None) – How long one try of the declared cleanup may run.
cleanup_retries (int | None) – How many times the declared cleanup is retried.
cleanup_retry_delay (timedelta | None) – The delay before each of its retries.
cleanup_anywhere (bool | None) – Whether it may run on another worker when this one is lost.
uses (Sequence[object] | None) – The resources it needs (decision 259).
- Raises:
RuntimeError – Outside
with Dag(...).TypeError – For an argument or environment value of the wrong type, or a
cleanup_*setting on a declaration without[cleanup].ValueError – For an invalid environment variable name.
LookupError – For a declaration the worker parsing the file doesn’t have.
- class schedr.operators.EmptyOperator(task_id, *, run_if=None, uses=None)¶
Bases:
OperatorA task that does nothing; the master completes it without a worker.
- Parameters:
task_id (str) – The task’s ID.
uses (Sequence[object] | None) – The resources it needs (decision 259).
run_if (RunIf | None) – When it completes, from its upstream edges (default
RunIf.NONE_FAILED); a join of branches that must all succeed takesRunIf.ALL_SUCCEEDED.
- Raises:
RuntimeError – Outside
with Dag(...).
- on(*members)¶
Depends on it only for these outcomes (it has only
SUCCESS; seeOutput.on).- Parameters:
*members (DefaultOutcome) –
DefaultOutcome.SUCCESS.- Returns:
The narrowed reference, to order with
>>.- Return type:
- property outcome: DefaultOutcome¶
Its outcome as data (always
SUCCESS), for a task takingDefaultOutcome.