0
0
Apache Airflowdevops~5 mins

Debugging with Airflow CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes your Airflow tasks don't run as expected. Debugging with the Airflow CLI helps you find and fix problems by checking logs and task status directly from the command line.
When a task in your Airflow DAG fails and you want to see the error details quickly.
When you want to check the status of a specific task instance without opening the Airflow web UI.
When you need to clear a failed task to retry it from the command line.
When you want to test if a DAG file is valid before deploying it.
When you want to see the list of all DAGs and their last run status to spot issues.
Commands
This command lists all the DAGs currently available in Airflow. It helps you confirm your DAG is loaded and ready.
Terminal
airflow dags list
Expected OutputExpected
dag_id | filepath example_dag | /usr/local/airflow/dags/example_dag.py my_test_dag | /usr/local/airflow/dags/my_test_dag.py
This shows all the tasks inside the DAG named 'example_dag'. It helps you know the task names you can debug.
Terminal
airflow tasks list example_dag
Expected OutputExpected
task_id start_task process_data end_task
Runs the 'process_data' task from 'example_dag' for the date 2024-06-01 locally without affecting the scheduler. Useful for quick debugging.
Terminal
airflow tasks test example_dag process_data 2024-06-01
Expected OutputExpected
[2024-06-01 12:00:00,000] {taskinstance.py:876} INFO - Starting attempt 1 of 1 [2024-06-01 12:00:01,000] {taskinstance.py:900} INFO - Task succeeded
Clears the state of the 'process_data' task for the given date so it can be retried. This is useful if you want to rerun a failed task.
Terminal
airflow tasks clear example_dag process_data --start-date 2024-06-01 --end-date 2024-06-01
Expected OutputExpected
Cleared the state of 1 task instance from example_dag.process_data for execution dates between 2024-06-01 00:00:00 and 2024-06-01 00:00:00
--start-date - Defines the start date of the task instances to clear
--end-date - Defines the end date of the task instances to clear
Shows the logs of the 'process_data' task for the date 2024-06-01. Logs help you understand what happened during task execution.
Terminal
airflow tasks logs example_dag process_data 2024-06-01
Expected OutputExpected
[2024-06-01 12:00:00,000] INFO - Running process_data task [2024-06-01 12:00:01,000] ERROR - Failed to connect to database [2024-06-01 12:00:02,000] INFO - Task failed
Key Concept

If you remember nothing else from debugging Airflow tasks, remember: use the CLI to check task status, logs, and rerun tasks quickly without the web UI.

Common Mistakes
Trying to run 'airflow tasks test' without specifying the exact execution date.
Airflow needs the execution date to know which task instance to run for testing.
Always provide the execution date in YYYY-MM-DD format when using 'airflow tasks test'.
Clearing tasks without specifying the date range.
This can clear many task instances unintentionally, causing unnecessary reruns.
Use --start-date and --end-date flags to clear only the specific task instances you want.
Checking logs for a task that has never run.
No logs exist yet, so the command will show empty or error messages.
Make sure the task has run at least once before checking logs.
Summary
Use 'airflow dags list' to see all available DAGs.
Use 'airflow tasks list DAG_NAME' to see tasks inside a DAG.
Use 'airflow tasks test' to run a task locally for debugging.
Use 'airflow tasks clear' with date flags to reset task state for retries.
Use 'airflow tasks logs' to view detailed logs of task runs.