[英]How Airflow DockerOperator send notification when Docker image creation error
我想在 Airflow DockerOperator 无法创建 Docker 映像时发送通知。 如果 DockerOperator 执行错误,可以使用on_failure_callback
发送通知。
更具体地说,我想捕获 2 个错误。
from airflow import DAG
from datetime import datetime, timedelta
from airflow.providers.docker.operators.docker import DockerOperator
def send_slack():
print('send error message')
default_args = {
'on_failure_callback': send_slack,
}
with DAG(
dag_id='test_dag',
default_args=default_args,
schedule_interval='45 * * * *',
start_date=datetime(2021, 1, 1),
catchup=False,
dagrun_timeout=timedelta(minutes=420),
concurrency=1,
tags=['test']
) as dag:
t = DockerOperator(
task_id="test_operator",
container_name="test_container",
image=f"10.11.12.13/myapp:latest",
force_pull=False,
auto_remove=True,
command = " python my_test.py ",
docker_url="tcp://20.21.22.23:2375",
cpus=1,
mem_limit="1g",
mount_tmp_dir=False
)
t
if __name__ == "__main__":
dag.cli()
如果你想处理特定的错误,你需要查看 DockerOperator 的代码并创建你自己的自定义操作符:
class MyCustomDockerOperator(DockerOperator):
... # here implement any customisations
您可以查看运算符的实现方式并覆盖特定方法,捕获特定异常并以您认为最适合您的方式处理它。
然后 - 在您的 DAG 中使用 MyCustomDockerOperator。 如果您想在多个 DAG 中使用它,您甚至可以将它添加到共享实用程序代码中。
https://airflow.apache.org/docs/apache-airflow/stable/modules_management.html
根据Jarek的回答,我在下面实现了非常粗略的 CustomOperator。
class MyCustomDockerOperator(DockerOperator):
def execute(self, context) -> Option[str]:
try:
super().execute(context)
except AirflowException:
raise
except Exception as e:
raise AirflowException('something is wrong in creating docker image.') from e
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.