来自动态任务名称的Apache Airflow Xcom Pull

时间:2018-10-25 18:49:53

标签: python jinja2 airflow

我已经在DAG(Bash和Docker Operators)中成功创建了动态任务,但是我很难将那些动态创建的任务传递给xcom_pull来获取数据。

for i in range(0, max_tasks):
    task_scp_queue = BashOperator(task_id="scp_queue_task_{}".format(i), bash_command="""python foo""", retries=3, dag=dag, pool="scp_queue_pool", queue="foo", provide_context=True, xcom_push=True) # Pull the manifest ID from the previous task via xcom'

    task_process_queue = DockerOperator(task_id="process_task_{}".format(i), command="""python foo --queue-name={{ task_instance.xcom_pull(task_ids=scp_queue_task_{}) }}""".format(i), retries=3, dag=dag, pool="process_pool", api_version="auto", image="foo", queue="foo", execution_timeout=timedelta(minutes=5))
    task_manifest = DockerOperator(api_version="auto", task_id="manifest_task_{}".format(i), image="foo", retries=3, dag=dag, command=""" python --manifestid={{ task_instance.xcom_pull(task_ids=scp_queue_task_{}) }}""".format(i), pool="manfiest_pool", queue="d_parser")

    task_psql_queue.set_downstream(task_scp_queue)
    task_process_queue.set_upstream(task_scp_queue)
    task_manifest.set_upstream(task_process_queue)

如您所见,我尝试仅在Jinja模板中使用Python格式的字符串在其中传递i变量,但这不起作用。

我也尝试过使用“ task.task_id”,并仅使用task_id创建一个新字符串,但这也不起作用。

编辑:

现在命令如下所示

command="""python foo \ 
    --queue-name="{{ 
    task_instance.xcom_pull(task_ids='scp_queue_task_{}') }}" 
     """.format(i)

我来自Airflow的调试日志看起来像

Using Master Queue: process_{ 
task_instance.xcom_pull(task_ids='scp_queue_task_31') }

因此正在填充字符串值,但未执行xcom_pull。

1 个答案:

答案 0 :(得分:1)

我很困惑这怎么不起作用。记录您遇到的错误将很有帮助。

简而言之,您的工作看起来不错,如果max_tasks=2您会得到:

task_psql_queue.taskid --> scp_queue_task_0 >> process_task_0 >> manifest_task_0
                       \-> scp_queue_task_1 >> process_task_1 >> manifest_task_1

我怀疑您不需要超时,这确实很短。因为您的行很长,并且会随机重命名命名的参数,所以我将重新格式化您写的内容:

for i in range(0, max_tasks):
    task_scp_queue = BashOperator(
        task_id="scp_queue_task_{}".format(i),
        dag=dag,
        retries=3,  # you could make it a default arg on the dag
        pool="scp_queue_pool",
        queue="foo", # you really want both queue and pool? When debugging remove them.
        bash_command="python foo",  # Maybe you snipped a multiline command
        provide_context=True,  # BashOp doesn't have this argument
        xcom_push=True,  # PUSH the manifest ID FOR the NEXT task via xcom
    )

    task_process_queue = DockerOperator(
        task_id="process_task_{}".format(i),
        dag=dag,
        retries=3,
        pool="process_pool",
        queue="foo",
        execution_timeout=timedelta(minutes=5),
        api_version="auto",
        image="foo",
        command="python foo --queue-name="
                "{{{{ task_instance.xcom_pull(task_ids=scp_queue_task_{}) }}}}".format(i),
    )

    task_manifest = DockerOperator(
        task_id="manifest_task_{}".format(i),
        retries=3,
        dag=dag,
        pool="manfiest_pool",
        queue="d_parser",
        api_version="auto",
        image="foo",
        command="python --manifestid="
                "{{{{ task_instance.xcom_pull(task_ids=scp_queue_task_{}) }}}}".format(i),
    )

    task_psql_queue >> task_scp_queue >> task_process_queue >> task_manifest

哦,现在看,您没有将task_ids作为字符串传递。试试:

        command="python foo --queue-name="
                "{{{{ task_instance.xcom_pull(task_ids='scp_queue_task_{}') }}}}".format(i),
… … …
        command="python --manifestid="
                "{{{{ task_instance.xcom_pull(task_ids='scp_queue_task_{}') }}}}".format(i),