What you will learn: What Airflow injects into every task at runtime.
Starter file: dagscode/concepts/03_context_starter.py
Every @task function can accept **kwargs. When Airflow runs your task, it injects a dictionary of runtime information -- who triggered the run, what date it represents, which task instance is executing. You don't have to use it, but it's always there.
@task
def my_task(**kwargs):
ti = kwargs["ti"] # TaskInstance -- the running task
ds = kwargs["ds"] # execution date as a string (YYYY-MM-DD)
run_id = kwargs["run_id"] # unique ID for this DAG runKey fields:
| Key | What it is |
|---|---|
ti |
TaskInstance -- the current task run, used to access XCom and metadata |
ds |
Execution date string -- the logical date of the run, not wall-clock time |
run_id |
Unique identifier for the DAG run |
dag |
The DAG object itself |
conf |
Any config passed when triggering the run manually |
cp dagscode/concepts/03_context_starter.py dags/concepts/The DAG will appear in the Airflow UI within 30 seconds.
- Open
dags/concepts/03_context_starter.py - Trigger the DAG. Open
explore_contextin Grid view and read the printed table. - Find
ti,ds, andrun_idin the table. Note their types. - TODO -- Extract and print individual values:
ti = kwargs["ti"]
ds = kwargs["ds"]
run_id = kwargs["run_id"]
print(f"Task: {ti.task_id}, Date: {ds}, Run: {run_id}")- Trigger again. Confirm each value appears in the log.
- Trigger a second run manually. Compare
run_idbetween the two runs -- it changes every time.dsstays the same because no schedule is set.
- Task logs: the full context table on the first trigger, then your extracted values
run_idis unique per trigger;dsis determined by the schedule (or trigger time for manual runs)