在所有Airflow DAG之前运行代码

时间:2017-03-28 16:51:00

标签: airflow

我正在使用相对较新的documentation项目。我有一堆编写并运行的DAG。现在我想集成一个错误报告服务,这样如果任何DAG中的任何代码都引发异常,那么这些信息将被发送到某个API。我可以将API调用放在每个DAG的bug_reporter.init(bug_reporter_token)中,但是我需要执行一个只需运行一次的In your code line# 4, dont assign spliced array to same array.Just splice it 初始化行。

Airflow中是否有用于初始化代码的地方?现在我正在每个DAG定义文件的开头初始化错误跟踪器。这似乎是多余的,但我找不到写一个在定义DAG之前运行的文件的地方。我曾尝试阅读有关airflow的内容,但似乎没有。

1 个答案:

答案 0 :(得分:2)

在DAG定义文件中,而不是DAG使用您自己的子类:

from airflow.utils.decorators import apply_defaults
import bug_reporter

class DAGWithBugReporter(DAG):
    @apply_defaults
    def __init__(
        self,
        bug_reporter_token,
        *args, **kwargs):

        super(DAGWithBugReporter, self).__init__(*args, **kwargs)
        bug_reporter.init(bug_reporter_token)

然后在你的dag定义中:

dag = DAGWithBugReporter(
    dag_id='my_dag',
    schedule_interval=None,
    start_date=datetime(2017, 2, 26),
    bug_reporter_token=my_token_from_somewhere
)


t1 = PythonOperator(
    task_id='t1',
    provide_context=True,
    python_callable=my_callable,
    xcom_push=True,
    dag=dag)