Utilities
Templating helpers
Typed helpers for Airflow templated fields that may hold callables.
- class yellowdog_provider.utils.templating.TemplateCallable(*args, **kwargs)[source]
Bases:
Protocol,GenericA 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 exactlycontextandjinja_env.
- type yellowdog_provider.utils.templating.Templated = T | TemplateCallable
Lookup helpers
YellowDog utility functions.
- class yellowdog_provider.utils.yellowdog_utils.SupportsLog(*args, **kwargs)[source]
Bases:
ProtocolThe subset of a logger used by the lookup helpers.
- 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
YellowDogArgumentErrorunless 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>: aRunStamp.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) aRunStamp.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:
objectIdentifies the DAG run and task that submitted a work requirement.
deployment_iddistinguishes 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()andtask_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 (
tiattributes) and the[yellowdog] deployment_idsetting (seeconfigured_deployment_id()).
- map_index: int
- run_id: str
- tag() str[source]
Tag unique to this task and map index within the DAG run:
<task_tag_prefix()>:m<map_index>(m-1if not mapped).
- task_id: str
- yellowdog_provider.utils.run_stamp.TASK_TAG_PREFIX_PATTERN = re.compile('airflow:[0-9a-f]{12}:[0-9a-f]{12}')
Matches (with
fullmatch) aRunStamp.task_tag_prefix()value.
- yellowdog_provider.utils.run_stamp.configured_deployment_id() str[source]
The Airflow deployment’s
[yellowdog] deployment_idsetting (environment variableAIRFLOW__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, andx-is put before a result that starts with a digit (xif nothing is left).With
max_length, a longer result is cut short and ends with-and an 8-hex hash ofraw, so that names differing only beyond the cut stay distinct;sanitise_name(raw, MAX_NAME_LENGTH)always matchesNAME_PATTERN. Without it, the result is not shortened.- Raises:
.YellowDogException – if
max_lengthis 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) asstamp.
- yellowdog_provider.utils.run_stamp.try_name(name: str, try_number: int) str[source]
Append
-try<N>, keeping the result within 60 characters.When
nameis 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 asnamedoes, so a valid name (seeNAME_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
excis 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, ifNone, every final state that is not a target state. The status type is that offinal_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_stateeverypoke_intervalseconds until it returns a target state (which is returned), a failure state (describe(state)is raised) ortimeoutseconds have passed (WaitTimeoutError). The state is always checked once more at the deadline before timing out. Up toMAX_CONSECUTIVE_TRANSIENT_ERRORSconsecutive transient errors fromget_state(seeis_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,WaitTimeoutErroris raised (or the error itself, if no state was ever read).kindandresource_idonly label the timeout message.