通过电子邮件将电子邮件列表作为来自气流中其他DAG的参数
我创建了一个自定义的电子邮件警报Python文件,我在另一个DAG中调用该功能以发送电子邮件。 假设 - 电子邮件警报文件是 custom_alert.py
from airflow.operators.email import EmailOperator
from airflow.utils.email import send_email
def custom_failure_email(context, **kwargs):
"""Send custom email alerts."""
dag_run = context.get('task_instance').dag_id
subject = f"[ActionReq]-dag failure-{dag_run}"
# email contents
body= """Hi Team,<br><br>
<b style="font-size:15px;color:red;">Airflow job on error, please find details below.</b>
Thank you!,<br>
)
email_list = ['[email protected]', '[email protected]']
for i in range(len(email_list)):
send_email(str(email_list[i]),subject,body)
in parent dag:say ** email.py **
- 我在调用上述发送失败电子邮件的功能。
from custom_alert import custom_failure_email
default_args = {
'owner': 'airflow',
'depends_on_past': False,
'email': ['[email protected]'],
'email_on_failure': False,
'on_failure_callback': custom_failure_email}
这样,我就可以发送自定义电子邮件,但是每个DAG的收件人列表相同。 请让我知道,如何自定义它以发送不同的收件人电子邮件地址以进行不同的DAG。 我如何通过父级dag传递收件人电子邮件地址。?
I have created a custom email alert python file, I am calling that function in another dag to send email.
Lets say - email alert file is custom_alert.py
from airflow.operators.email import EmailOperator
from airflow.utils.email import send_email
def custom_failure_email(context, **kwargs):
"""Send custom email alerts."""
dag_run = context.get('task_instance').dag_id
subject = f"[ActionReq]-dag failure-{dag_run}"
# email contents
body= """Hi Team,<br><br>
<b style="font-size:15px;color:red;">Airflow job on error, please find details below.</b>
Thank you!,<br>
)
email_list = ['[email protected]', '[email protected]']
for i in range(len(email_list)):
send_email(str(email_list[i]),subject,body)
In Parent DAG : Lets Say **email.py**
- I am calling the above function to send failure email.
from custom_alert import custom_failure_email
default_args = {
'owner': 'airflow',
'depends_on_past': False,
'email': ['[email protected]'],
'email_on_failure': False,
'on_failure_callback': custom_failure_email}
This way I am able to send the custom email, but my recipient list is same for every dag.
Please let me know, How I can customize it to send different recipient email address for different dag. How I can pass recipient email address from Parent Dag.?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个选项是在您的
dag
中设置params
dict,然后从上下文中检索此值。An option is to set
params
dict in yourDAG
then retrieve this value from the context.还有另一种方法可以使用Functools实现此目标。
我们需要通过 email_list 作为
** custom_alert.py **
中的参数There is one more way to achieve this using functools.
and we need to pass email_list as argument in
**custom_alert.py**