简体   繁体   English

从 Airflow 中的另一个 DAG 获取 DAG 电子邮件

[英]Get DAG Email from another DAG in Airflow

I want to get the email mentioned in this DAG's default args using another DAG in the airflow.我想使用气流中的另一个 DAG 来获取此 DAG 的默认参数中提到的电子邮件。 How can I do that?我怎样才能做到这一点? Please help, I am new to airflow!请帮忙,我是气流新手!

from airflow.models import DagRun
from airflow.operators.python_operator import PythonOperator
from datetime import datetime, timedelta
from datetime import datetime, timedelta
from airflow import DAG

def first_function(**context):
    print("hello")

default_args = {
    'owner': 'airflow',
    'depends_on_past': False,
    'email': ['example@gmail.com'],
    'email_on_failure': False,
    'email_on_retry': False,
    'retries': 1,
    'retry_delay': timedelta(minutes=5)
}

with DAG(
    'main',
    default_args=default_args,
    description='Sample DAG',
    schedule_interval=timedelta(days=1),
    start_date=datetime(2022,6,10),
    catchup=False
) as dag:
    first_function = PythonOperator(
        task_id="first_function",
        python_callable=first_function,
    )
first_function

You can use a custom module in Airflow to share config options/Operators/or any arbitrary Python code across DAGs.您可以使用 Airflow 中的自定义模块在 DAG 之间共享配置选项/运算符/或任何任意 Python 代码。

Typically you would create a directory in your DAGs directory (which by default is {AIRFLOW_HOME}/dags .通常,您会在 DAGs 目录中创建一个目录(默认为{AIRFLOW_HOME}/dags

To share default_args between DAGs, you could create the following layout:要在 DAG 之间共享default_args ,您可以创建以下布局:

  • Create {AIRFLOW_HOME}/dags/custom/__init__.py创建{AIRFLOW_HOME}/dags/custom/__init__.py
  • Create {AIRFLOW_HOME}/dags/custom/shared_config.py创建{AIRFLOW_HOME}/dags/custom/shared_config.py
  • Create {AIRFLOW_HOME}/dags/.airflowignore创建{AIRFLOW_HOME}/dags/.airflowignore
  • Add the directory name custom to the first line of .airflowignore .将目录名称custom添加到.airflowignore的第一行。
  • Cut and paste your default_args dictionary from your DAG into {AIRFLOW_HOME}/dags/custom/shared_config.py将您的default_args字典从 DAG 剪切并粘贴到{AIRFLOW_HOME}/dags/custom/shared_config.py

You can see this layout suggested in the Airflow documentation here .您可以在此处查看Airflow 文档中建议的此布局。

The .airflowignore tells the scheduler to skip the custom directory when it parses your DAGs (which by default happens every 30s) - because the custom directory contains Python, but never any DAGs, the scheduler should skip these files to avoid adding unnecessary load to the scheduler. .airflowignore告诉调度程序在解析 DAG 时跳过custom目录(默认情况下每 30 秒发生一次) - 因为custom目录包含 Python,但从不包含任何 DAG,调度程序应该跳过这些文件以避免向调度器。 This is explained in the documentation link above.这在上面的文档链接中进行了解释。

You need to add an __init__.py to the custom directory - airflow requires it even though when writing in Python3 you don't need it because of implicit namespaces (again this is explained in the same link above).您需要将__init__.py添加到自定义目录 - 即使在 Python3 中编写时,由于隐式命名空间您不需要它(同样在上面的同一链接中对此进行了解释),气流也需要它。

From your dag you can then import as needed:然后,您可以根据需要从您的 dag 导入:

from custom.shared_config import default_args

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

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