简体   繁体   English

Airflow - 将 Xcom 拉取结果传递给 TriggerDagRunOperator conf

[英]Airflow - Pass Xcom Pull result to TriggerDagRunOperator conf

Does anyone know what's the issue with the below code:有谁知道下面的代码有什么问题:

Essentially I am calling a TriggerDagRunOperator, and i am trying to pass some conf through to it, based off an XCOM Pull.本质上,我正在调用 TriggerDagRunOperator,并且我正在尝试基于 XCOM Pull 将一些 conf 传递给它。

TRIGGER DAG:触发 DAG:

def _should_trigger(**_):
    return {'Message': 'Hello World'}


should_trigger = PythonOperator(
    task_id="should_trigger",
    python_callable=_should_trigger,
    provide_context=True,
)

trigger_bar_dag = TriggerDagRunOperator(
    task_id="trigger_bar_dag",
    trigger_dag_id="bar",
    conf={"payload": "{{ task_instance.xcom_pull('should_trigger') }}"},
)

TARGET DAG:目标 DAG:

@dag(dag_id="bar",
     default_args=default_args,
     schedule_interval=None
     )
def tasks():
    run_this = PythonOperator(
        task_id="run_this",
        python_callable=run_this_func,
        provide_context=True)

For some reason, in the run_this_func, i get payload: None.出于某种原因,在 run_this_func 中,我得到了有效载荷:无。 I can't seem to pass in values through the conf stream from an xcom pull.我似乎无法通过来自 xcom pull 的 conf stream 传递值。 Does anyone know how this can be accomplished.有谁知道如何做到这一点。 I have also tried with different variations of xcom pull, like: ti.xcom_pull(key='return_value', task_ids=['should_trigger']) to no avail.我还尝试了不同的 xcom pull 变体,例如: ti.xcom_pull(key='return_value', task_ids=['should_trigger']) 无济于事。

Thanks,谢谢,

You need to specify should_trigger >> trigger_bar_dag because otherwise the XCom record may not yet exists and you will get:您需要指定should_trigger >> trigger_bar_dag否则 XCom 记录可能还不存在,您将获得:

[2021-06-06 08:23:35,898] {logging_mixin.py:104} INFO - {'payload': 'None'}

But once I add this relation then I'm getting:但是一旦我添加了这个关系,我就会得到:

[2021-06-06 08:21:41,356] {logging_mixin.py:104} INFO - {'payload': "{'Message': 'Hello World'}"}

with

def run_this_func(**context):
    print(context['params'])

here is my real code:这是我的真实代码:


def external_trigger(name):

  def modify_dro(context, dagrun_order):
    log.info('context: {}'.format(context))
    # run_id from here
    log.info('dagrun_order: {}'.format(dagrun_order))
    dagrun_order.payload = {
      'branch': "branch123",
      'revision': 'revision123'
    }
    return dagrun_order

  exteranl_run = TriggerDagRunOperator(task_id='external_' + name,
                          trigger_dag_id='se_perf_post_test',
                          python_callable=modify_dro,
                          on_failure_callback=airflow_on_fail,
                          task_concurrency=256,
                          provide_context=True,
                          trigger_rule='all_done',
                          dag=dag)
  return exteranl_run

Use modify_dro func to pass variables for the triggered dag.使用 modify_dro 函数为触发的 dag 传递变量。

On the be triggered DAG:在被触发的 DAG 上:

def prepare_build(**args):
  log.info("args: " + str(args))
  run_id = args['run_id']
  log.info("run id " + run_id)
  conf = args['dag_run'].conf
  log.info('conf: {}'.format(conf))
  branch = conf.get('branch')
  revision = conf.get('revision')
  log.info("run build on branch: {}, revision: {}".format(branch, revision))

Hope it can help.希望它可以提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM