0
0
Apache Airflowdevops~10 mins

Idempotent task design in Apache Airflow - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a task that runs only if it hasn't run before.

Apache Airflow
def task_function(**kwargs):
    ti = kwargs['ti']
    if ti.xcom_pull(task_ids='task_id') is [1]:
        print('Running task')
Drag options to blanks, or click blank then click option'
ANone
BFalse
CTrue
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using False or 0 instead of None causes wrong checks.
2fill in blank
medium

Complete the code to push a result to XCom after task completion.

Apache Airflow
def task_function(**kwargs):
    ti = kwargs['ti']
    result = 'done'
    ti.xcom_push(key='status', value=[1])
Drag options to blanks, or click blank then click option'
A'result'
Bresult
C'done'
D'status'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a string literal instead of the variable causes wrong data to be pushed.
3fill in blank
hard

Fix the error in the task decorator to ensure idempotency with retries.

Apache Airflow
@task(retries=[1])
def my_task():
    pass
Drag options to blanks, or click blank then click option'
A3
B-1
CNone
D'3'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of an integer causes type errors.
4fill in blank
hard

Fill both blanks to create an idempotent task that skips if output exists.

Apache Airflow
def task_function(**kwargs):
    ti = kwargs['ti']
    output = ti.xcom_pull(task_ids=[1], key=[2])
    if output is not None:
        print('Skipping task')
Drag options to blanks, or click blank then click option'
A'previous_task'
B'result'
C'current_task'
D'status'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong task IDs or keys causes the check to fail.
5fill in blank
hard

Fill all three blanks to define an idempotent task that updates a database only once.

Apache Airflow
def update_db_task(**kwargs):
    ti = kwargs['ti']
    already_done = ti.xcom_pull(task_ids=[1], key=[2])
    if not already_done:
        # perform update
        ti.xcom_push(key=[3], value=True)
Drag options to blanks, or click blank then click option'
A'check_update'
B'update_status'
C'done'
D'update_db_task'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up keys or task IDs breaks the idempotency check.