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

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

DAG run marked as success while task failed

问题

  1. def my_main(**kwargs):
  2. try:
  3. ....
  4. except:
  5. print("错误:", pprint.pformat(traceback.format_exc(), indent=1))
  6. dag = DAG(
  7. 'my_main',
  8. description='my_main',
  9. start_date=days_ago(0),
  10. schedule_interval='@daily',
  11. catchup=False
  12. )
  13. 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):

  1. import time
  2. import datetime
  3. import json
  4. import pprint
  5. import requests
  6. from airflow import DAG
  7. from airflow.operators.dummy_operator import DummyOperator
  8. from airflow.operators.python_operator import PythonOperator
  9. from airflow.operators.python_operator import BranchPythonOperator
  10. def my_main(**kwargs):
  11. try:
  12. ....
  13. except:
  14. print("Error:", pprint.pformat(traceback.format_exc(), indent=1))
  15. dag = DAG(
  16. 'my_main',
  17. description='my_main',
  18. start_date=days_ago(0),
  19. schedule_interval='@daily',
  20. catchup=False
  21. )
  22. task_checker = PythonOperator(task_id='task_checker_my_main', provide_context=True, python_callable=my_main, dag=dag)

答案1

得分: 2

  1. 你需要触发异常吞掉了真正的异常所以函数以无错误的方式结束在打印后直接触发异常
  2. def my_main(**kwargs):
  3. try:
  4. ....
  5. except:
  6. print("错误:", pprint.pformat(traceback.format_exc(), indent=1))
  7. 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.

  1. def my_main(**kwargs):
  2. try:
  3. ....
  4. except:
  5. print("Error:", pprint.pformat(traceback.format_exc(), indent=1))
  6. 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:

确定