Utilities

Templating helpers

Typed helpers for Airflow templated fields that may hold callables.

class yellowdog_provider.utils.templating.TemplateCallable(*args, **kwargs)[source]

Bases: Protocol, Generic

A callable Airflow invokes at render time to produce a field value.

Airflow calls it as value(context=..., jinja_env=...), so the parameter names must be exactly context and jinja_env.

type yellowdog_provider.utils.templating.Templated = T | TemplateCallable
yellowdog_provider.utils.templating.resolve(value: Templated, field_name: str) → T[source]

Return a rendered templated field value, rejecting unrendered callables.

yellowdog_provider.utils.templating.resolve_optional_str(value: Templated[str] | None, field_name: str) → str | None[source]

Normalise an optional string field: empty/None -> None, ‘None’ -> error.

Lookup helpers

YellowDog utility functions.

class yellowdog_provider.utils.yellowdog_utils.SupportsLog(*args, **kwargs)[source]

Bases: Protocol

The subset of a logger used by the lookup helpers.

info(msg: str, /, *args: object) → None[source]
warning(msg: str, /, *args: object) → None[source]
yellowdog_provider.utils.yellowdog_utils.get_compute_requirement_by_id_or_name(client: PlatformClient, log: SupportsLog, compute_requirement_id: str | None, namespace: str | None, compute_requirement_name: str | None) → ComputeRequirement[source]

Get a compute requirement by ID, or by namespace and name if no ID.

yellowdog_provider.utils.yellowdog_utils.get_work_requirement_by_id_or_name(client: PlatformClient, log: SupportsLog, work_requirement_id: str | None, namespace: str | None, work_requirement_name: str | None) → WorkRequirement[source]

Get a work requirement by ID, or by namespace and name if no ID.

yellowdog_provider.utils.yellowdog_utils.get_worker_pool_by_id_or_name(client: PlatformClient, log: SupportsLog, worker_pool_id: str | None, namespace: str | None, worker_pool_name: str | None) → WorkerPool[source]

Get a worker pool by ID, or by namespace and name if no ID.

yellowdog_provider.utils.yellowdog_utils.require_id_or_name(kind: str, id_field: str, object_id: object | None, name_fields: dict[str, object | None]) → None[source]

Raise YellowDogArgumentError unless the ID is supplied, or every one of the name fields is supplied. Values may be unrendered templates or callables.

Run stamp and naming

Run stamp (DAG run/task identity) and YellowDog name derivation.

yellowdog_provider.utils.run_stamp.INSTANCE_TAG_PREFIX_PATTERN = re.compile('airflow:[0-9a-f]{12}:[0-9a-f]{12}:m-?[0-9]+')

Matches (with fullmatch) a task tag prefix for one map index, <task_tag_prefix()>:m<map_index>: a RunStamp.tag() value.

yellowdog_provider.utils.run_stamp.MAX_NAME_LENGTH = 60

Maximum length of a YellowDog resource name.

yellowdog_provider.utils.run_stamp.NAME_PATTERN = re.compile('[a-z](?:[a-z0-9_-]{0,58}[a-z0-9])?')

A valid YellowDog resource name (match with fullmatch): at most 60 characters, only lower-case letters, digits, _ and -, starting with a letter and ending with a letter or digit.

yellowdog_provider.utils.run_stamp.RUN_TAG_PREFIX_PATTERN = re.compile('airflow:[0-9a-f]{12}')

Matches (with fullmatch) a RunStamp.run_tag_prefix() value.

class yellowdog_provider.utils.run_stamp.RunStamp(dag_id: str, run_id: str, task_id: str, map_index: int, deployment_id: str = '')[source]

Bases: object

Identifies the DAG run and task that submitted a work requirement.

deployment_id distinguishes Airflow deployments that share YellowDog credentials (and so could have DAG runs with the same DAG and run IDs); when non-empty, it is part of both digests (run_tag_prefix() and task_tag_prefix()). When empty, the stamp is the same as without it.

dag_id: str
deployment_id: str = ''
classmethod from_context(context: Context) → RunStamp[source]

Build a stamp from the Airflow task context (ti attributes) and the [yellowdog] deployment_id setting (see configured_deployment_id()).

map_index: int
run_id: str
run_tag_prefix() → str[source]

Tag prefix shared by every work requirement of this DAG run.

tag() → str[source]

Tag unique to this task and map index within the DAG run: <task_tag_prefix()>:m<map_index> (m-1 if not mapped).

task_id: str
task_tag_prefix() → str[source]

Tag prefix shared by every work requirement of this task within the DAG run, whatever the map index: airflow:<run digest>:<task digest>, where the task digest covers the task ID (and the deployment ID, if set) but not the map index.

yellowdog_provider.utils.run_stamp.TASK_TAG_PREFIX_PATTERN = re.compile('airflow:[0-9a-f]{12}:[0-9a-f]{12}')

Matches (with fullmatch) a RunStamp.task_tag_prefix() value.

yellowdog_provider.utils.run_stamp.configured_deployment_id() → str[source]

The Airflow deployment’s [yellowdog] deployment_id setting (environment variable AIRFLOW__YELLOWDOG__DEPLOYMENT_ID), or "" if unset.

yellowdog_provider.utils.run_stamp.derive_name(stamp: RunStamp) → str[source]

Derive a unique-per-stamp YellowDog name that is valid (matches NAME_PATTERN): it starts with the (sanitised) DAG ID, which starts with a letter, and ends with an 8-hex digest.

yellowdog_provider.utils.run_stamp.sanitise_name(raw: str, max_length: int | None = None) → str[source]

Lower-case and reduce to [a-z0-9_-], collapsing other runs to -, then make it start with a letter and end with a letter or digit: leading and trailing - and _ are removed, and x- is put before a result that starts with a digit (x if nothing is left).

With max_length, a longer result is cut short and ends with - and an 8-hex hash of raw, so that names differing only beyond the cut stay distinct; sanitise_name(raw, MAX_NAME_LENGTH) always matches NAME_PATTERN. Without it, the result is not shortened.

Raises:

.YellowDogException – if max_length is less than 10

yellowdog_provider.utils.run_stamp.task_tag_prefix_for(stamp: RunStamp, task_id: str) → str[source]

The RunStamp.task_tag_prefix() of another task, task_id, in the same DAG run (same DAG ID, run ID and deployment ID) as stamp.

yellowdog_provider.utils.run_stamp.try_name(name: str, try_number: int) → str[source]

Append -try<N>, keeping the result within 60 characters.

When name is too long, characters are removed from just before its last 8 characters, so the tail (the uniqueness digest of a derived name, or the end of a user-supplied one) is always preserved. The result ends with a digit and starts as name does, so a valid name (see NAME_PATTERN) gives a valid name.

Waiting and states

Shared state resolution and a synchronous wait loop, used by the sensors and the operators that wait for a resource to reach a state.

yellowdog_provider.utils.waiting.MAX_CONSECUTIVE_TRANSIENT_ERRORS = 3

How many consecutive transient errors (see is_transient_error()) a wait tolerates; the next one ends the wait.

yellowdog_provider.utils.waiting.is_transient_error(exc: BaseException) → bool[source]

Whether exc is worth retrying on the next poll: a connection error, a request timeout, an HTTP error with status 429 or 5xx, or the SDK’s server-side errors (InternalServerException, ServerErrorException). Anything else (including other HTTP 4xx errors such as 404, and the SDK’s other exceptions) is not.

yellowdog_provider.utils.waiting.resolve_states(target_states: Sequence, failure_states: Sequence | None, final_states: Sequence, kind: str) → tuple[tuple[S, ...], tuple[S, ...]][source]

Validate the target and failure states and return (targets, failures). The failure states are as given or, if None, every final state that is not a target state. The status type is that of final_states.

Parameters:

kind – the kind of resource (e.g. "work requirement"), used in error messages

Raises:

YellowDogArgumentError – if the targets are empty, of the wrong type, or overlap the failure states

yellowdog_provider.utils.waiting.wait_for_state(get_state: ~collections.abc.Callable[[], S], targets: ~collections.abc.Sequence, failures: ~collections.abc.Sequence, *, poke_interval: float, timeout: float, describe: ~collections.abc.Callable[[S], ~yellowdog_provider.exceptions.yellowdog_exceptions.YellowDogStateError], log: ~yellowdog_provider.utils.yellowdog_utils.SupportsLog, sleep: ~collections.abc.Callable[[float], None] = <built-in function sleep>, clock: ~collections.abc.Callable[[], float] = <built-in function monotonic>, kind: str = 'resource', resource_id: str | None = None) → S[source]

Poll get_state every poke_interval seconds until it returns a target state (which is returned), a failure state (describe(state) is raised) or timeout seconds have passed (WaitTimeoutError). The state is always checked once more at the deadline before timing out. Up to MAX_CONSECUTIVE_TRANSIENT_ERRORS consecutive transient errors from get_state (see is_transient_error()) are logged and the wait continues; the next one, or any other error, is raised. If the deadline passes after a transient error, WaitTimeoutError is raised (or the error itself, if no state was ever read). kind and resource_id only label the timeout message.