简体   繁体   English

Airflow DockerOperator 如何在 Docker 映像创建错误时发送通知

[英]How Airflow DockerOperator send notification when Docker image creation error

I want to send notification when Airflow DockerOperator can't create Docker image.我想在 Airflow DockerOperator 无法创建 Docker 映像时发送通知。 Notification can be sent in case of DockerOperator execution errors using on_failure_callback .如果 DockerOperator 执行错误,可以使用on_failure_callback发送通知。

To be more specific, I want to catch 2 errors.更具体地说,我想捕获 2 个错误。

  1. private Docker repository is not running(10.11.12.13 is not running in example below)私有 Docker 存储库未运行(10.11.12.13 在下面的示例中未运行)
  2. execution server is not running(20.21.22.23:2345 is not running in example below)执行服务器未运行(20.21.22.23:2345 在下面的示例中未运行)
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()

If you want to handle the specific errors, you need to look at the code of the DockerOperator and create your own custom operator:如果你想处理特定的错误,你需要查看 DockerOperator 的代码并创建你自己的自定义操作符:

class MyCustomDockerOperator(DockerOperator):
    ... # here implement any customisations

You can look at how the operator is implemented and override specific methods, catch specific exception and handle it in the way you see best for you.您可以查看运算符的实现方式并覆盖特定方法,捕获特定异常并以您认为最适合您的方式处理它。

And then - use MyCustomDockerOperator in your DAG.然后 - 在您的 DAG 中使用 MyCustomDockerOperator。 You can even add it to a shared util code if you want to use it in multiple DAGs.如果您想在多个 DAG 中使用它,您甚至可以将它添加到共享实用程序代码中。

https://airflow.apache.org/docs/apache-airflow/stable/modules_management.html https://airflow.apache.org/docs/apache-airflow/stable/modules_management.html

According to Jarek 's answer, I implement very rough CustomOperator below.根据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.

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