触发的DAG无法从TriggerDagRunOperator获取参数

时间:2019-01-18 16:48:27

标签: airflow

我试图在TriggerDagRunOperator中用一些参数触发另一个dag,但是在触发的dag中,dag_run对象始终为None。

在TriggerDagRunOperator中,将消息参数添加到dag_run_obj的有效负载中。

id

我希望触发的DAG可以使用kwargs ['dag_run']。conf ['message'])来获取它,但不幸的是它不起作用。

def conditionally_trigger(context, dag_run_obj):
if context['params']['condition_param']:
    dag_run_obj.payload = {'message': context['params']['message']}
    pp.pprint(dag_run_obj.payload)
    return dag_run_obj


trigger = TriggerDagRunOperator(
    task_id='test_trigger_dagrun',
    trigger_dag_id="example_trigger_target_dag",
    python_callable=conditionally_trigger,
    params={'condition_param': True, 'message': 'Hello World'},
    dag=dag,
)

kwargs中的dag_run对象为None

def run_this_func(ds, **kwargs):
print("Remotely received value of {} for key=message".
      format(kwargs['dag_run'].conf['message']))


run_this = PythonOperator(
    task_id='run_this',
    provide_context=True,
    python_callable=run_this_func,
    dag=dag,
)

我也打印出了kwargs,实际上'dag_run'对象是None。 这些问题是Airflow中的示例代码,因此我不确定发生了什么。 有人知道原因吗?

INFO - Executing <Task(PythonOperator): run_this> on 2019-01-18 16:10:18 
INFO - Subtask: [2019-01-18 16:10:27,007] {models.py:1433} ERROR - 'NoneType' object has no attribute 'conf'
INFO - Subtask: Traceback (most recent call last):
INFO - Subtask:   File "/Library/Python/2.7/site-packages/airflow/models.py", line 1390, in run
INFO - Subtask:     result = task_copy.execute(context=context)
INFO - Subtask:   File "/Library/Python/2.7/site-packages/airflow/operators/python_operator.py", line 80, in execute
INFO - Subtask:     return_value = self.python_callable(*self.op_args, **self.op_kwargs)
INFO - Subtask:   File "/Library/Python/2.7/site-packages/airflow/example_dags/example_trigger_target_dag.py", line 52, in run_this_func
INFO - Subtask:     print("Remotely received value of {} for key=message".format(kwargs['dag_run'].conf['message']))
INFO - Subtask: AttributeError: 'NoneType' object has no attribute 'conf'

顺便说一句,如果我从CLI触发DAG,它会起作用:

INFO - Subtask: kwargs: {u'next_execution_date': None, u'dag_run': None, u'tomorrow_ds_nodash': u'20190119', u'run_id': None, u'dag': <DAG: example_trigger_target_dag>, u'prev_execution_date': None, ...

日志:

$ airflow trigger_dag 'example_trigger_target_dag' -r 'run_id' --conf '{"message":"test_cli"}'

0 个答案:

没有答案