我认为宏 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运行的执行时间
发布评论