gpt4 book ai didi

airflow - Apache Airflow 宏获取最后一次 dag 运行执行时间

转载 作者:行者123 更新时间:2023-12-02 07:11:54 24 4
gpt4 key购买 nike

我认为宏prev_execution_date列出了here会给我最后一次 DAG 运行的执行日期,但查看源代码,它似乎只能根据 DAG 时间表获取最后日期。

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

当 DAG 没有按计划运行时,有没有办法通过宏来获取 DAG 的执行日期?

最佳答案

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

# 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.com/apache/incubator-airflow/blob/v1-10-stable/airflow/models.py#L3396

您还可以调整日期格式的字符串格式,以及在没有之前运行的情况下想要输出的内容。

关于airflow - Apache Airflow 宏获取最后一次 dag 运行执行时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51923684/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com