python - 仅在某些异常上重试 Airflow 任务实例
问题描述
仅针对某些故障/异常重试 Airflow 操作员的最佳方法是什么?
例如,假设我有一个 Airflow 任务,它依赖于外部服务的可用性。如果此服务在任务执行期间不可用,我想稍后重试(最多重试 3 次)。对于其他失败,我不想重试。
我目前的方法是通过 parsing 使用on_failure_callback
和操作context["ti"].task.retries
所需的异常context["exception"]
,但我认为这很混乱且难以理解。有更好的选择吗?
解决方案
Most of airflow's operators use a Hook
class to complete the work.
If you can create your own PythonOperator
and try/catch the exceptions you want to avoid and throw the exceptions you want to trigger the retry it will comply with airflow architecture seamlessly:
# python operator function
def my_operation():
try:
hook = SomeHook()
hook.use_it()
catch IgnorableException e:
pass
# then:
my_operator = PythonOperator(
task_id='my-operator',
python_callable=my_operation
)
It gives you more control over your Operator and DAG life-cycle.
推荐阅读
- bash - 在 bash 脚本中提供 Sudo 密码
- c++ - 如何在 Linux/g++/gdb 中调试 VSCode 时跳过不必要的函数/文件
- python - 在 Yolo v3 Darknet 中测量检测到的对象的 X、Y、Z 坐标
- spring-boot - 将 Spring Security 与 Spring Gateway 一起使用时出现“无法访问 javax.servlet.Filter”错误
- javascript - 这个异步 JavaScript 返回一个空数组。我不确定我是否将异步和等待放在正确的位置
- python - 如何在C++中用文字制作图片的白色背景
- r - Rmarkdown 无法运行 Chunk 但可以在控制台中运行
- python-3.x - (Python) Netmiko : 将路由器的配置与模板进行比较
- sql - 如何根据 postgres 中的数据执行交叉表?
- php - VS Code 显示可能引发未处理异常的地方