How to Mark a Task as Success in Airflow
In Airflow, you can mark a task as success by calling
task_instance.set_state("success"). This is typically done inside a task's Python callable or via Airflow CLI commands.Syntax
To mark a task as success programmatically, use the set_state method on the task instance object with the argument "success". This changes the task's state in the Airflow metadata database.
task_instance: The current task instance object.set_state(state): Method to set the task state, wherestateis a string like"success".
python
task_instance.set_state("success")Example
This example shows how to mark a task as success inside a PythonOperator's callable function by accessing the task instance from the context and setting its state.
python
from airflow import DAG from airflow.operators.python import PythonOperator from airflow.utils.dates import days_ago def mark_success(**context): ti = context['ti'] ti.set_state("success") with DAG(dag_id="example_mark_success", start_date=days_ago(1), schedule_interval=None) as dag: task = PythonOperator( task_id="mark_task_success", python_callable=mark_success )
Output
Task instance state is set to success in Airflow metadata after task runs.
Common Pitfalls
Common mistakes when marking a task as success include:
- Trying to set the state outside of a running task context, which causes errors because
task_instanceis not available. - Not importing or passing the
task_instancecorrectly in the callable. - Using deprecated methods or manipulating the database directly instead of using
set_state.
Always use the Airflow context and APIs to change task states safely.
python
def wrong_mark_success(): # This will fail because no task_instance context is available task_instance.set_state("success") # Correct way inside a task callable def correct_mark_success(**context): ti = context['ti'] ti.set_state("success")
Quick Reference
Summary tips to mark a task as success in Airflow:
- Use
task_instance.set_state("success")inside task code. - Access
task_instancevia thecontext['ti']parameter. - Do not manipulate task states outside of Airflow's API.
- Use Airflow CLI
airflow tasks mark_successfor manual marking.
Key Takeaways
Use task_instance.set_state("success") inside task code to mark success.
Access the task instance via context parameter 'ti' in PythonOperator.
Avoid changing task states outside Airflow APIs to prevent errors.
Airflow CLI can also mark tasks as success manually.
Always run state changes within a valid task context.