DAG运行标记为成功,而任务失败。

huangapple go评论51阅读模式
英文:

DAG run marked as success while task failed

问题

def my_main(**kwargs):
    try:
        ....
    except:
        print("错误:", pprint.pformat(traceback.format_exc(), indent=1))

dag = DAG(
    'my_main',
    description='my_main',
    start_date=days_ago(0),
    schedule_interval='@daily',
    catchup=False
)
task_checker = PythonOperator(task_id='task_checker_my_main', provide_context=True, python_callable=my_main, dag=dag)
英文:

I have some PythonOperator in my DAG which time to time throws error therefore simply it goes into except statement and entire process is stopped due to that. What i do not understand is my dag run is marked as success. Can anyone explains that to me please as i thought if code inside task throws an error the dag should be marked as failed, not? Why dag is marked success?

DAG (cutted):

import time
import datetime
import json
import pprint
import requests
from airflow import DAG
from airflow.operators.dummy_operator import DummyOperator
from airflow.operators.python_operator import PythonOperator
from airflow.operators.python_operator import BranchPythonOperator

def my_main(**kwargs):
	try:
		....
	 except:
        print("Error:", pprint.pformat(traceback.format_exc(), indent=1))


dag = DAG(
    'my_main', 
    description='my_main', 
    start_date=days_ago(0), 
    schedule_interval='@daily',
    catchup=False
)
task_checker = PythonOperator(task_id='task_checker_my_main', provide_context=True, python_callable=my_main, dag=dag)

答案1

得分: 2

你需要触发异常吞掉了真正的异常所以函数以无错误的方式结束在打印后直接触发异常

    def my_main(**kwargs):
        try:
            ....
        except:
            print("错误:", pprint.pformat(traceback.format_exc(), indent=1))
            raise AirflowException("你的消息")
英文:

You need to raise Exception. you "swallow" the real exception so the function ends with no error . just raise exception after the print.

def my_main(**kwargs):
try:
    ....
 except:
    print("Error:", pprint.pformat(traceback.format_exc(), indent=1))
    raise AirflowException("your message")

huangapple
  • 本文由 发表于 2023年5月22日 16:05:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76304145.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定