Airflow Xcom_pull returns None

Viewed 61

I am trying to use XCom Push and Pull to use Variables between tasks. I tried the following code

def check_type_func(**kwargs):
    auto_job1 = ''
    execution_date = kwargs['execution_date']
    dag_date = execution_date + timedelta(days=1)
    execution_day=dag_date.strftime("%a")
    if  execution_day== 'Mon' or execution_day== 'Tue' or execution_day== 'Wed' or execution_day== 'Fri' or execution_day == 'Sat':
        auto_job1 = 'dpst_001'
    elif execution_day== 'Sun' or execution_day== 'Thu':
        auto_job1 = 'dpst_002'
    kwargs['task_instance'].xcom_push(key='auto_job1', value=auto_job1)
    return auto_job1

check_type = PythonOperator(
    task_id='check_type',
    provide_context=True,
    depends_on_past=False,
    python_callable=check_type_func,
    wait_for_downstream=False,
    xcom_push=True,
    dag=dag)

auto_job_id = """{{ task_instance.xcom_pull(key="auto_job1",task_ids="check_type") }}"""

I am trying to use the auto_job_id in a different operator and the operator fails thinking it received None Type instead of a string. Any idea to solve the issue will be greatly appreciated. Do you think there is a better way of doing it.

1 Answers

in push you have key='auto_job1'

kwargs['task_instance'].xcom_push(key='auto_job1', value=auto_job1)

and auto_job_id have key='autosys_job1'

auto_job_id = """{{ task_instance.xcom_pull(key="autosys_job1",task_ids="check_type") }}"""

Are you sure that you have the same key name in the code?

Related