Getting XComs from cleared tasks in Airflow

I have slack alerts for when tasks fail, but I also want to have recovery messages as well.

When the task initially fails, in its on_failure_callback it does an xcom_push. What I save here is available in the next DAG run using:

context['ti'].xcom_pull(key='my_task_state',
                        task_ids=context['task'].task_id,
                        include_prior_dates=True)

However, if I clear the failed task so that it re-runs, in its on_failure_callback/on_success_callback I try this to get the value I saved in the initial attempt:

context['ti'].xcom_pull(key='my_task_state',
                        task_ids=context['task'].task_id,
                        include_prior_dates=False)

This returns None. If I set include_prior_dates=True it’ll return the value from the previous DAG run, but not the current one where the task was cleared.

Am I doing something wrong, or is there a workaround that I can use to get the XCom value I’m looking for?

1 answer

  • answered 2019-08-13 16:29 Yong Wang

    In my understanding:

    Xcom is designed to for inter-exchange message between tasks. And Xcom status is depends on the task instance. If the task instance is cleared(delted), the xcom history info belong to this instance will be deleted as well.

    That's why you get

    • none when include_prior_dates=False, (task instance deleted, there is no such xcom record)

    • last dag info when include_prior_dates=True,(task instance delted, but another dags tasks instance xcome was poped(most recented).

    Here is show case by default example_xcom:

      1. all_success status:
        • 1.1 dag enter image description here
        • 1.2 the xcom list enter image description here
      1. clear one task status:
        • 2.1 clear enter image description here
        • 2.2 the deletd xcom list the xcome should be deleted, but I failed to screen capture.

      Thats' why your program get none.

      • 2.3 the redo xcom list(but timestamp is different, the new record) enter image description here

    if you find the answer is help, pls vote it up. Thanks