气流:当on_success_callback执行带参数

时间:2017-06-15 16:58:43

标签: python scheduled-tasks scheduler airflow apache-airflow

我想用一个从任务中传递的参数来执行一个函数。

这是我的状态参数函数:

def sns_notify(state):
    client = boto3.client('sns')
    if state == "failed":
        message = config.get('sns', 'message') + state
    else:
        message = config.get('sns', 'message') + state
    response = client.publish(TargetArn=config.get('sns', 'target_arn'),
                              Message=message,
                              Subject=config.get('sns', 'subject'))
    return response

这是我的状态为param的任务:

t1 = DummyOperator(task_id='Dummy-1', trigger_rule=TriggerRule.ALL_SUCCESS,
                   on_success_callback=sns_notify("ok"), dag=dag)

t2 = DummyOperator(task_id='Dummy-2', trigger_rule=TriggerRule.ONE_FAILED,
                   on_success_callback=sns_notify("failed"), dag=dag)

当我运行dag时,该功能不会停止发送邮件(对于此示例)

2 个答案:

答案 0 :(得分:3)

每次通过气流加载DAG时,它都会执行sns_notify("ok"),因为您正在调用该函数。您只需要传递函数指针sns_notify,它将接收context。请参阅文档:https://airflow.apache.org/code.html

trigger_rule与执行依赖项任务有关,因此与on_success_callback无关。

我不知道如何将变量传递给此回调 - 来到这里寻找答案!

答案 1 :(得分:1)

Hoju指出了确切的错误。

您可以使用函数式编程来帮助解决此问题。

a[0:3:2, :]                     # basic indexing, a view
... = a[0:3:2, :][:, [0, 2]]    # getitme version, a copy,
                                # because you use advanced
                                # indexing [:,[0,2]]
a[0:3:2, :][:, [0, 2]] = ...    # howver setitem version is
                                # like a view, setitem version
                                # is different from getitem version,
                                # this is not c++
a[:, [0, 2]]                    # getitem version, a copy,
                                # because you use advanced indexing
a[:, [0, 2]][0:3:2, :] = 0      # the copy is modied,
                                # but a keeps unchanged.