Sensors
All sensors live in yellowdog_provider.sensors.yellowdog_sensors:
from yellowdog_provider.sensors.yellowdog_sensors import (
ComputeRequirementStateSensor,
WorkerPoolStateSensor,
WorkRequirementStateSensor,
)
Each sensor looks up one YellowDog resource on every poke and succeeds when
the resource’s status is one of the target_states you give. It fails at
once if the status is one of its failure_states instead: by default, the
final states (from which the resource never moves) that are not targets.
The waiting operators (RunWorkRequirement, and ProvisionWorkerPool
and ShutdownProvisionedWorkerPool with their wait_until_ready and
wait options) wait for their own resources, so DAGs built from them
(Provision, run, shut down) need no sensors. Use a sensor to wait for a resource that
another task, or something outside the DAG, created (Step by step).
Common behaviour
Keyword-only arguments. Every argument must be passed by name.
Examples on this page assume these imports:
from yellowdog_client.model import (
ComputeRequirementStatus,
WorkerPoolStatus,
WorkRequirementStatus,
)
Common parameters. task_id (str) is required, as for any Airflow
task. As well as the parameters listed for each sensor, every sensor takes:
Parameter |
Type |
Default |
Jinja |
Callable |
|---|---|---|---|---|
|
|
|
yes |
yes |
|
|
|
no |
no |
|
|
|
no |
no |
and all the standard Airflow BaseSensorOperator arguments, notably:
timeout: seconds before the sensor fails with a timeout. The default comes from Airflow’s[sensors] default_timeoutsetting (7 days unless changed), so set it explicitly.mode:"poke"(default) holds a worker slot for the whole wait;"reschedule"frees it between pokes, and suits long waits with a poke interval of a minute or more.soft_fail,silent_fail,exponential_backoff,max_wait, and the usualBaseOperatorarguments such asretriesandtrigger_rule.
Deferrable mode. With deferrable=True, the sensor checks the resource
once on the worker (looking it up, pushing its ID to XCom, and succeeding or
failing at once if it is already in a target or failure state). Otherwise it
defers to Airflow’s triggerer, which checks the state every
poke_interval seconds without holding a worker slot, until a target
state, a failure state, or timeout seconds after the sensor started.
deferrable defaults to Airflow’s [operators] default_deferrable
setting (False unless changed), and needs a running triggerer; see
Deferral. A deferrable sensor ignores mode="reschedule", and
exponential_backoff and max_wait do not apply to the triggerer’s
polling. While deferred, up to three consecutive transient errors are tolerated.
The Jinja and Callable columns have the same meaning as in
Operators. target_states and failure_states are fixed when the
DAG is parsed.
ID or namespace and name. Each sensor identifies its resource either by ID
or by namespace and name, with the same rules as the operators: one or the other must be supplied (checked when the sensor is
constructed and again after rendering), the ID wins if both are given (with
a warning), an empty rendered value counts as not supplied, and a value that
renders to "None" raises
YellowDogArgumentError.
target_states must be a non-empty list of the SDK status enum for the
resource; an empty list, or a value of another type (such as the string
"COMPLETED"), raises YellowDogArgumentError when the sensor is
constructed.
failure_states is an optional, keyword-only list of the same status enum.
When a poke finds the resource in one of these states, the sensor pushes the
resource ID to XCom (as on any first poke) and then raises
YellowDogStateError,
failing the task without waiting for timeout. The error message names the
kind of resource, its ID, its namespace and name, its current state and the
target states; the same values are attributes of the exception
(kind, resource_id, namespace, name, current_state,
target_states).
None(the default): every final state that is not intarget_states. The final states are:Sensor
Final states
WorkRequirementStateSensorCOMPLETED,FAILED,CANCELLED(the SDK’sWorkRequirementStatus.finishedstates)WorkerPoolStateSensorSHUTDOWN,TERMINATED(the SDK’sWorkerPoolStatus.finishedstates)ComputeRequirementStateSensorTERMINATED(STOPPEDis not final: a stopped compute requirement can be restarted)So
target_states=[WorkRequirementStatus.COMPLETED]fails onFAILEDorCANCELLED, while a worker pool sensor waiting for[SHUTDOWN, TERMINATED]has no failure states. The final-state tuples are available asWORK_REQUIREMENT_FINAL_STATES,WORKER_POOL_FINAL_STATESandCOMPUTE_REQUIREMENT_FINAL_STATESinyellowdog_provider.sensors.yellowdog_sensors.An explicit list replaces the default; it may include non-final states (for example
WorkRequirementStatus.HELD).[]disables failure detection: the sensor waits only fortarget_statesuntiltimeout, as in 1.x.
A state may not be in both lists: an overlap raises YellowDogArgumentError
(naming the overlapping states) when the sensor is constructed.
Failure states and retries. YellowDogStateError is also Airflow’s
AirflowFailException, so Airflow fails the sensor task at once and does
not retry it, whatever its retries (a resource in a final state will not
change).
Failure states and the fail arguments. silent_fail=True does not
suppress a failure state: the task still fails. soft_fail=True or
never_fail=True turns a failure state into a skipped task.
The fail arguments in deferrable mode:
Outcome |
|
|
|
|---|---|---|---|
Failure state |
skipped |
skipped |
fails |
Timeout |
skipped |
skipped (when polling: fails with |
fails |
Error (in the first check, or reported by the triggerer) |
fails |
skipped |
fails (there is no later poke to retry) |
So the only difference from a polling sensor is a timeout with
never_fail=True, which skips a deferred sensor but fails a polling one,
and silent_fail, which has no effect when deferred.
Portal links. WorkRequirementStateSensor and WorkerPoolStateSensor
show a link to their resource in the YellowDog Portal; see
Connections.
XCom. On its first poke, each sensor pushes the resolved resource ID to
XCom under the key return_value. This happens regardless of
do_xcom_push, and means a sensor that looked a resource up by name gives
downstream tasks its ID: "{{ ti.xcom_pull(task_ids='<sensor_task_id>') }}".
Errors during a poke. If a poke raises (for example, the SDK’s 404
because no resource has that name yet, or bad credentials), the sensor task
fails at once rather than waiting for the next poke; it is then retried
according to its retries. If the resource may not exist yet when the
sensor starts, either start the sensor after the task that creates it, or set
silent_fail=True so that errors are logged and poking continues (failure
states still fail the task; see above). silent_fail does not apply to a
deferred sensor; its triggerer tolerates only transient errors.
Timeouts. If neither a target state nor a failure state is reached
within timeout, the sensor fails with AirflowSensorTimeout.
WorkRequirementStateSensor
Waits for a work requirement to reach one of target_states, failing if
it reaches one of failure_states.
Parameter |
Type |
Default |
Jinja |
Callable |
|---|---|---|---|---|
|
|
required |
no |
no |
|
|
|
no |
no |
|
|
|
yes |
yes |
|
|
|
yes |
yes |
|
|
|
yes |
yes |
WorkRequirementStatus values: RUNNING, HELD, FINISHING,
COMPLETED, FAILED, CANCELLING, CANCELLED. To wait for the
work to succeed, use COMPLETED alone: the sensor then fails if the work
requirement fails or is cancelled. To wait for it to finish whatever the
outcome, use COMPLETED, FAILED and CANCELLED (there are then no
failure states, and the sensor succeeds even if the work failed).
Example:
wait = WorkRequirementStateSensor(
task_id="wait-for-work",
work_requirement_id="{{ ti.xcom_pull(task_ids='submit') }}",
target_states=[WorkRequirementStatus.COMPLETED],
poke_interval=30,
timeout=60 * 60,
)
WorkerPoolStateSensor
Waits for a worker pool to reach one of target_states, failing if it
reaches one of failure_states.
Parameter |
Type |
Default |
Jinja |
Callable |
|---|---|---|---|---|
|
|
required |
no |
no |
|
|
|
no |
no |
|
|
|
yes |
yes |
|
|
|
yes |
yes |
|
|
|
yes |
yes |
WorkerPoolStatus values: PENDING, CONFIGURING, EMPTY,
IDLE, RUNNING, SHUTDOWN, TERMINATED.
To wait for a newly provisioned pool to be ready for work, use both
IDLE and RUNNING: RUNNING means one or more workers have claimed
work, so a ready pool with nothing to do stays at IDLE and a sensor
waiting for RUNNING alone may never succeed. By default such a sensor
fails if the pool shuts down (SHUTDOWN or TERMINATED) first. To wait
for a pool to go away, use SHUTDOWN and TERMINATED.
Example:
wait_for_pool = WorkerPoolStateSensor(
task_id="wait-for-pool",
worker_pool_id="{{ ti.xcom_pull(task_ids='provision-pool') }}",
target_states=[WorkerPoolStatus.IDLE, WorkerPoolStatus.RUNNING],
timeout=30 * 60,
)
ComputeRequirementStateSensor
Waits for a compute requirement to reach one of target_states, failing
if it reaches one of failure_states (by default TERMINATED, unless it
is a target). A
provisioned worker pool’s compute requirement has the name and namespace given
in the ComputeRequirementTemplateUsage used to provision it.
Parameter |
Type |
Default |
Jinja |
Callable |
|---|---|---|---|---|
|
|
required |
no |
no |
|
|
|
no |
no |
|
|
|
yes |
yes |
|
|
|
yes |
yes |
|
|
|
yes |
yes |
ComputeRequirementStatus values: NEW, PROVISIONING, STARTING,
RUNNING, STOPPING, STOPPED, TERMINATING, TERMINATED.
Example, confirming that a compute requirement’s instances have gone
(ShutdownProvisionedWorkerPool with terminate_immediately=True and
wait=True does this for its own pool):
wait_for_termination = ComputeRequirementStateSensor(
task_id="wait-for-termination",
namespace="{{ params.namespace }}",
compute_requirement_name="{{ params.pool_name }}",
target_states=[ComputeRequirementStatus.TERMINATED],
timeout=30 * 60,
)