schedr.resources

Resources: one thing of two parts, a setup and its cleanup (decision 259).

A task is a set of instructions, and may have no cleanup at all. A resource is by definition created and undone: both parts always exist, as tasks of the DAG, the cleanup running once every user is done. One without @cleanup has an empty cleanup (decision 260).

schedr.resources.NO_VALUE = (None, <class 'NoneType'>)

What a setup without a value returns.

class schedr.resources.ResourceOptions

Bases: TypedDict

@resource’s options: as for @task.

class schedr.resources.CleanupOptions

Bases: TypedDict

.cleanup()’s options.

timeout

How long one try may run (default: the worker’s resource_cleanup_timeout, 30 minutes), for each try: with retries, a hung one holds its run up to that times the tries. It isn’t cancelled with its run; schedr tasks cancel stops it.

Type:

datetime.timedelta | None

retries

How many times it is tried again.

Type:

int | None

retry_delay

The delay before each try again.

Type:

datetime.timedelta | None

queue

Its queue; the setup’s if not given.

Type:

str | None

secrets

Secrets it declares, besides its setup’s.

Type:

schedr._secrets.SecretsArg

connections

Connections it declares, besides its setup’s.

Type:

schedr.connections.ConnectionsArg

blocking

Whether what follows the resource’s group waits for this cleanup (the default). With False it goes on, and isn’t stopped if the cleanup fails; the run still fails.

Type:

bool

unschedulable_timeout

How long a user may wait for a worker (unschedulable) before it fails, so this cleanup can run; no limit if not given.

Type:

datetime.timedelta | None

class schedr.resources.ResourceFunction(fn, task_id, options, cleanup=None, declared=None)

Bases: TaskFunction[P, T], Generic[P, T]

A function decorated with @resource: the setup, called like a task.

Parameters:
  • fn (Callable[P, T])

  • task_id (str)

  • options (_build.TaskOptions)

  • cleanup (Callable[..., object] | None)

  • declared (_Declared | None)

cleanup(fn: Callable[[], object], /) → Callable[[], object]
cleanup(fn: Callable[[T], object], /) → Callable[[T], object]
cleanup(**options: Unpack[CleanupOptions]) → _WithoutValue
cleanup(**options: Unpack[CleanupOptions]) → _WithValue[T]

Marks a function as the resource’s cleanup: it gets what the setup returned.

Used bare (@cluster.cleanup) or with options (@cluster.cleanup(retries=1)).

Parameters:
  • fn (Callable[[...], object] | None) – The function, when used bare.

  • **options (Unpack[CleanupOptions]) – See CleanupOptions.

Returns:

The function as it is, or a decorator keeping it so.

Raises:

TypeError – For a second cleanup.

Return type:

Callable[[…], object]

property value: T

t"{cluster.value.cluster_id}".

Type checkers read it as the value; it is a reference, resolved when the DAG sets the resource up.

Type:

The resource’s value, for its cleanup_command

cleanup_command(command, /, *, env=None, **options)

Gives the resource a command as its cleanup (decision 271).

It reads the value with schedr task-ctx resource get, or takes it in its command line or env as CommandOperator takes outputs: t"{cluster.value.cluster_id}".

Parameters:
  • command (str | Template | Sequence[object]) – A shell string, or a list of arguments.

  • env (Mapping[str, object] | None) – Extra environment variables for the process.

  • **options (Unpack[CleanupOptions]) – See CleanupOptions.

Raises:

TypeError – For a second cleanup, or a command of the wrong type.

Return type:

None

cleanup_script(job, /, *, args=None, env=None, **options)

Gives the resource a declared script as its cleanup (decision 271).

It reads the value with schedr task-ctx resource get, or takes it in its arguments or env: t"{cluster.value.cluster_id}".

Its options win over the declaration’s settings, which win over a cleanup’s defaults.

Parameters:
  • job (str) – The declaration: "<root>:<path>/schedr.toml#<name>". One with [outcomes] or [cleanup] is refused.

  • args (Sequence[object] | None) – Its arguments instead of the declared ones.

  • env (Mapping[str, object] | None) – Environment variables added to (or replacing) the declared ones.

  • **options (Unpack[CleanupOptions]) – See CleanupOptions.

Raises:

TypeError – For a second cleanup, or a reference that isn’t a string.

Return type:

None

class schedr.resources.Resource(name)

Bases: object

A resource without setup code: tmp = Resource("tmp-prefix").

The master completes its setup, with no process. It has no value: its cleanup takes no parameter, and its users name it with uses=.

Parameters:

name (str) – Its name: the ID of its setup.

Raises:

RuntimeError – Outside with Dag(...).

cleanup(fn: Callable[[], object], /) → Callable[[], object]
cleanup(**options: Unpack[CleanupOptions]) → _WithoutValue

Marks a function as the resource’s cleanup; it takes no parameter.

Used bare (@tmp.cleanup) or with options (@tmp.cleanup(retries=1)).

Parameters:
  • fn (Callable[[...], object] | None) – The function, when used bare.

  • **options (Unpack[CleanupOptions]) – See CleanupOptions.

Returns:

The function as it is, or a decorator keeping it so.

Raises:

TypeError – For a second cleanup.

Return type:

Callable[[…], object]

cleanup_command(command, /, *, env=None, **options)

Gives the resource a command as its cleanup (decision 271).

It takes no value: the resource has none.

Parameters:
  • command (str | Template | Sequence[object]) – A shell string, or a list of arguments.

  • env (Mapping[str, object] | None) – Extra environment variables for the process.

  • **options (Unpack[CleanupOptions]) – See CleanupOptions.

Raises:

TypeError – For a second cleanup, or a command of the wrong type.

Return type:

None

cleanup_script(job, /, *, args=None, env=None, **options)

Gives the resource a declared script as its cleanup (decision 271).

It takes no value: the resource has none.

Its options win over the declaration’s settings, which win over a cleanup’s defaults.

Parameters:
  • job (str) – The declaration: "<root>:<path>/schedr.toml#<name>". One with [outcomes] or [cleanup] is refused.

  • args (Sequence[object] | None) – Its arguments instead of the declared ones.

  • env (Mapping[str, object] | None) – Environment variables added to (or replacing) the declared ones.

  • **options (Unpack[CleanupOptions]) – See CleanupOptions.

Raises:

TypeError – For a second cleanup, or a reference that isn’t a string.

Return type:

None

class schedr.resources.ScriptResourceOptions

Bases: TypedDict

ScriptResource’s options besides its value: as for ScriptOperator.

class schedr.resources.CommandResourceOptions

Bases: TypedDict

CommandResource’s options besides its value: as for CommandOperator.

class schedr.resources.CommandResource(name: str, command: str | Template | Sequence[object], *, value: type[T], **options: Unpack[CommandResourceOptions])
class schedr.resources.CommandResource(name: str, command: str | Template | Sequence[object], *, value: object = None, **options: Unpack[CommandResourceOptions])

Bases: _ReportingResource, Generic

A resource whose setup is a command (decision 271).

The command reports the value with schedr task-ctx resource set, checked against value= at once. The setup succeeds only if it exits with 0 and, with value=, reported the value; its output is that value, not its ProcessResult. Its users take cluster.value (or uses=[cluster]); a command can take a field as t"{cluster.cluster_id}".

Parameters:
  • name (str) – Its name: the ID of its setup.

  • command (str | TString | Sequence[object]) – A shell string, or a list of arguments (as for CommandOperator).

  • value (object) – The type of the value it reports; none: the resource has no value. A union or a Literal needs the type given twice for mypy: CommandResource[int | None]("n", ..., value=int | None).

  • env – Extra environment variables for the process.

  • retries – How many times a failed attempt is retried.

  • retry_delay – The delay before each retry.

  • timeout – The attempt’s timeout.

  • queue – The queue the task goes to.

  • max_output_size – The output size limit in bytes (per stream).

  • run_if – When it runs, from its upstream edges.

  • max_active – At most this many running at once, across all runs.

  • secrets – The secrets it reads with schedr task-ctx secret get.

  • connections – The connections it reads, as for CommandOperator.

  • cleanup – A per-task cleanup: for a setup failing halfway (cleanup=).

  • cleanup_timeout – How long one try of that cleanup may run.

  • cleanup_retries – How many times it is retried.

  • cleanup_retry_delay – The delay before each retry.

  • cleanup_anywhere – Whether it may run on another worker when this one is lost.

  • cleanup_handles – The handle types the attempt may register.

  • uses – The resources it needs.

  • options (Unpack[CommandResourceOptions])

Raises:
  • RuntimeError – Outside with Dag(...).

  • TypeError – For a value type that can’t be stored, or a command of the wrong type.

class schedr.resources.ScriptResource(name: str, job: str, *, value: type[T], **options: Unpack[ScriptResourceOptions])
class schedr.resources.ScriptResource(name: str, job: str, *, value: object = None, **options: Unpack[ScriptResourceOptions])

Bases: _ReportingResource, Generic

A resource whose setup is a script declared in a schedr.toml (decision 271).

The script reports the value with schedr task-ctx resource set, checked against value= at once. The setup succeeds only if it exits with 0 and, with value=, reported the value; its output is that value, not its ProcessResult. Its users take cluster.value (or uses=[cluster]). A declared [cleanup] is the setup’s own, for a setup failing halfway; .cleanup_script and the others free the resource.

Parameters:
  • name (str) – Its name: the ID of its setup.

  • job (str) – The declaration: "<root>:<path>/schedr.toml#<name>". One with [outcomes] is refused.

  • value (object) – The type of the value it reports; none: the resource has no value.

  • args – Its arguments instead of the declared ones.

  • env – Environment variables added to (or replacing) the declared ones.

  • retries – How many times a failed attempt is retried.

  • retry_delay – The delay before each retry.

  • timeout – The attempt’s timeout.

  • queue – The queue the task goes to.

  • max_output_size – The output size limit in bytes (per stream).

  • run_if – When it runs, from its upstream edges.

  • max_active – At most this many running at once, across all runs.

  • secrets – Secrets declared besides the declared ones.

  • cleanup_timeout – How long one try of the declared cleanup may run.

  • cleanup_retries – How many times it is retried.

  • cleanup_retry_delay – The delay before each retry.

  • cleanup_anywhere – Whether it may run on another worker when this one is lost.

  • uses – The resources it needs.

  • options (Unpack[ScriptResourceOptions])

Raises:
  • RuntimeError – Outside with Dag(...).

  • TypeError – For a value type that can’t be stored, or a declaration with [outcomes].

  • LookupError – For a declaration the worker parsing the file doesn’t have.

class schedr.resources.AsyncResourceFunction(fn, task_id, options, cleanup=None, declared=None)

Bases: ResourceFunction[P, T], AsyncTaskFunction[P, T, T], Generic[P, T]

A setup that is async def: await area.fn(...) in a test (decision 319).

Parameters:
  • fn (Callable[P, T])

  • task_id (str)

  • options (_build.TaskOptions)

  • cleanup (Callable[..., object] | None)

  • declared (_Declared | None)

schedr.resources.resource(fn: Callable[P, Coroutine[object, object, T]], /) → AsyncResourceFunction[P, T]
schedr.resources.resource(fn: Callable[P, T], /) → ResourceFunction[P, T]
schedr.resources.resource(**options: Unpack[ResourceOptions]) → _ResourceDecorator

Marks a function as a resource’s setup: what it returns is the resource.

Parameters:
  • fn (Callable[[P], T] | Callable[[P], Coroutine[object, object, T]] | None) – The function (@resource without arguments).

  • **options (Unpack[ResourceOptions]) – As for @task.

Returns:

The setup, called like a task, or a decorator making one.

Raises:

TypeError – If the function can’t be a task.

Return type:

ResourceFunction[P, T] | _ResourceDecorator