Using the hook from TaskFlow
The operators cover the common workflow, but sometimes it is simpler to call
YellowDog from your own Python code, in a TaskFlow @task function. The
YellowDogHook has typed
methods for the same operations the operators use, including the retry-safe
submission, so your own tasks can behave like the operators.
Hook operations
Use the hook as a context manager, so its YellowDog client is closed afterwards. All methods raise the SDK’s errors unchanged, except where noted.
Method |
What it does |
|---|---|
|
Return the resource’s current status enum ( |
|
Submit a work requirement with its task groups and tasks, safely
across retries and clears (as |
|
The same for a work requirement on its own (as
|
|
Cancel a work requirement; |
|
IDs of the unfinished work requirements submitted by one DAG run, in a
namespace or ( |
|
Provision a worker pool; returns its ID. |
|
Shut down a provisioned worker pool and optionally terminate its compute requirement (returning its ID). A pool that is already shut down, or a compute requirement already terminating or terminated, is not an error. |
|
Terminate a compute requirement; already terminating or terminated is not an error. |
|
The YellowDog Portal URL for the connection (see Connections). |
|
The SDK |
To wait for a state, use
wait_for_state(), the loop that the
operators use when they do not defer. It polls a function until it returns a
target state, raises the error you describe for a failure state, raises
WaitTimeoutError
when the timeout passes, and tolerates transient errors.
resolve_states() validates target
and failure states and works out the default failure states.
The full signatures are in the API reference:
YellowDogHook, and
Utilities.
Example: submit, wait, cancel
This @task does what RunWorkRequirement does without deferral:
it submits a work requirement (resuming this task’s earlier attempt on a
retry), waits until it completes, and cancels it if the wait times out or
fails:
from airflow.sdk import dag, get_current_context, task
from yellowdog_client.model import (
RunSpecification,
Task,
TaskGroup,
WorkRequirement,
WorkRequirementStatus,
)
from yellowdog_provider.exceptions.yellowdog_exceptions import YellowDogStateError
from yellowdog_provider.hooks.yellowdog_hooks import YellowDogHook
from yellowdog_provider.sensors.yellowdog_sensors import (
WORK_REQUIREMENT_FINAL_STATES,
)
from yellowdog_provider.utils.run_stamp import RunStamp
from yellowdog_provider.utils.waiting import resolve_states, wait_for_state
@task
def run_hello_world(namespace: str, worker_tag: str) -> str:
context = get_current_context()
assert "ti" in context # For type checkers: always present in a task
ti = context["ti"]
spec = RunSpecification(taskTypes=["docker"], workerTags=[worker_tag])
task = Task(name="task-1", taskType="docker", arguments=["hello-world"])
groups = [(TaskGroup(name="group-1", runSpecification=spec), [task])]
# Wait for COMPLETED; fail on FAILED or CANCELLED
targets, failures = resolve_states(
[WorkRequirementStatus.COMPLETED],
None,
WORK_REQUIREMENT_FINAL_STATES,
"work requirement",
)
with YellowDogHook("yellowdog_default") as hook:
result = hook.submit_work_requirement(
WorkRequirement(namespace=namespace, name=""), # name derived
groups,
stamp=RunStamp.from_context(context),
try_number=ti.try_number,
cycle=ti.max_tries, # the clear cycle
)
wr_id = result.work_requirement_id
if result.action == "already_completed":
return wr_id
try:
wait_for_state(
lambda: hook.get_work_requirement_state(wr_id),
targets,
failures,
poke_interval=30,
timeout=3600,
describe=lambda state: YellowDogStateError(
kind="work requirement",
resource_id=wr_id,
current_state=state,
target_states=targets,
),
log=hook.log,
kind="work requirement",
resource_id=wr_id,
)
except YellowDogStateError:
raise # A final state: nothing to cancel
except Exception:
hook.cancel_work_requirement(wr_id, abort=True)
raise
return wr_id
@dag(dag_id="yellowdog-taskflow")
def yellowdog_taskflow():
run_hello_world("my-namespace", "my-tag")
yellowdog_taskflow()
Notes:
stamp,try_numberandcyclemake the submission retry-safe: pass the run stamp built from the task’s context, Airflow’s try number (used in-try<N>names) and the task instance’smax_tries(the clear cycle). See Safe retries and the run stamp. The work requirement is tagged with the run stamp, so a teardown’s cancellation (The teardown cancels its scope’s work) covers it too when the task is in that teardown’s setup/teardown scope (or withcancel_work_requirements="run").Unlike the operators, a
@taskfunction has noon_kill, so this task does not cancel its work requirement when it is killed (for example marked failed). Use aShutdownProvisionedWorkerPoolteardown whose scope includes this task, which cancels its unfinished work requirements, as the backstop.This task holds a worker slot while it waits. For long waits, use
RunWorkRequirementwithdeferrable=Trueinstead.
Passing SDK objects between tasks
A @task cannot return a WorkRequirement (or other SDK object) for
an operator to use: Airflow cannot pass SDK objects through XCom (see
Why SDK objects cannot come from XCom). Pass plain values (IDs, names, numbers, lists and
dicts) between tasks, and build SDK objects inside the task that uses them,
or in an operator’s callable.