我正在寻找一种方法,允许动态设置给定EmailOperator任务发送的电子邮件内容.理想情况下,我想使电子邮件内容取决于xcom调用的结果,最好是通过html_content参数.
alert = EmailOperator(
task_id=alertTaskID,
to='please@dontreply.com',
subject='Airflow processing report',
html_content='raw content #2',
dag=dag
)
Run Code Online (Sandbox Code Playgroud)
我注意到Airflow文档说xcom调用可以嵌入到模板中.也许有一种方法可以在指定的任务ID上使用模板来制定xcom pull,然后将结果作为html_content传递?谢谢
小智 12
使用PythonOperator
+send_email
代替:
from airflow.operators import PythonOperator
from airflow.utils.email import send_email
def email_callback(**kwargs):
with open('/path/to.html') as f:
content = f.read()
send_email(
to=[
# emails
],
subject='subject',
html_content=content,
)
email_task = PythonOperator(
task_id='task_id',
python_callable=email_callback,
provide_context=True,
dag=dag,
)
Run Code Online (Sandbox Code Playgroud)
不妨自己回答这个问题.事实证明,使用模板+ xcom路线非常简单.此代码段在已定义的dag的上下文中工作.它使用BashOperator而不是EmailOperator,因为它更容易测试.
def pushparam(param, ds, **kwargs):
kwargs['ti'].xcom_push(key='specificKey', value=param)
return
loadxcom = PythonOperator(
task_id='loadxcom',
python_callable=pushparam,
provide_context=True,
op_args=['your_message_here'],
dag=dag)
template2 = """
echo "{{ params.my_param }}"
echo "{{ task_instance.xcom_pull(task_ids='loadxcom', key='specificKey') }}"
"""
t5 = BashOperator(
task_id='tt2',
bash_command=template2,
params={'my_param': 'PARAMETER1'},
dag=dag)
Run Code Online (Sandbox Code Playgroud)
可以使用以下内容在命令行上进行测试:
airflow test dag_name loadxcom 2015-12-31
airflow test dag_name tt2 2015-12-31
Run Code Online (Sandbox Code Playgroud)
我最终将使用EmailOperator测试并在此处添加一些内容,如果它不起作用...
对于那些寻找使用带有EmailOperator的jinja模板的确切示例的人来说,这里有一个
from airflow.operators.email_operator import EmailOperator
from datetime import timedelta, datetime
email_task = EmailOperator(
to='some@email.com',
task_id='email_task',
subject='Templated Subject: start_date {{ ds }}',
params={'content1': 'random'},
html_content="Templated Content: content1 - {{ params.content1 }} task_key - {{ task_instance_key_str }} test_mode - {{ test_mode }} task_owner - {{ task.owner}} hostname - {{ ti.hostname }}",
dag=dag)
Run Code Online (Sandbox Code Playgroud)
您可以使用测试运行上面的代码段
airflow test dag_name email_task 2017-05-10
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9414 次 |
最近记录: |