Apache airflow宏获取上一次dag运行的执行时间

编程入门 行业动态 更新时间:2024-10-13 12:19:01
本文介绍了Apache airflow宏获取上一次dag运行的执行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我认为宏 prev_execution_date 列出了这里可以让我获得上一次DAG运行的执行日期,但是查看源代码似乎只能得出基于DAG时间表的最后日期。

I thought the macro prev_execution_date listed here would get me the execution date of the last DAG run, but looking at the source code it seems to only get the last date based on the DAG schedule.

prev_execution_date = task.dag.previous_schedule(self.execution_date)

当DAG未按计划运行时,是否可以通过宏获取DAG的执行日期?

Is there any way via macros to get the execution date of the DAG when it doesn't run on a schedule?

推荐答案

是的,您可以为此定义自己的自定义宏,如下所示:

Yes, you can define your own custom macro for this as follows:

# custom macro function def get_last_dag_run(dag): last_dag_run = dag.get_last_dagrun() if last_dag_run is None: return "no prev run" else: return last_dag_run.execution_date.strftime("%Y-%m-%d") # add macro in user_defined_macros in dag definition dag = DAG(dag_id="my_test_dag", schedule_interval='@daily', user_defined_macros={ 'last_dag_run_execution_date': get_last_dag_run } ) # example of using it in practice print_vals = BashOperator( task_id='print_vals', bash_command='echo {{ last_dag_run_execution_date(dag) }}', dag=dag )

请注意,dag.get_last_run()只是Dag对象上可用的众多功能之一。我在这里找到它的地方: https:/ /github/apache/incubator-airflow/blob/v1-10-stable/airflow/models.py#L3396

Note that the dag.get_last_run() is just one of the many functions available on the Dag object. Here's where I found it: github/apache/incubator-airflow/blob/v1-10-stable/airflow/models.py#L3396

您还可以调整日期格式的字符串格式,如果没有以前的运行,则要输出什么。

You can also tweak the formatting of the string for the date format, and what you want output if there is no previous run.

更多推荐

Apache airflow宏获取上一次dag运行的执行时间

本文发布于:2023-11-24 02:32:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623647.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:执行时间   Apache   airflow   dag

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!