【问题标题】:Airflow wrong execution_date for timezone aware DAGs时区感知 DAG 的气流错误 execution_date
【发布时间】:2022-01-31 10:52:43
【问题描述】:

我们在 Kubernetes (KubernetesExecutor) 上使用 Airflow v2.2.3, 我们的环境需要一个 DAG 预客户,每个客户可以在不同的时区。

每个 DAG 都应安排在自己的时区午夜, 我看到可以使用Airflow的timezone aware DAGs

因此为每个 DAG 配置时区感知 start_date 有效,并使每个 DAG 在其自己的时区午夜执行:

start_date_utc = (datetime.now() - timedelta(days=2)).replace(
        hour=0, minute=0, second=0, microsecond=0)

timezone = pendulum.timezone(get_customer_timezone(customer))
START_DATE = start_date_utc.replace(tzinfo=timezone)

default_args = {
        "owner": "owner",
        "depends_on_past": False,
        "start_date": START_DATE,
}

dag = DAG(
    dag_id,
    schedule_interval="0 0 * * *",
    default_args=default_args,
    tags=[cusotmer_name]
)

date = '{{ execution_date | ds }}'

operator_args = {
    "customer_date": date,
}

我的问题是 jinja template 和 dag_run execution_date (dag_run.logical_date) 仍然是 UTC,并且没有根据 DAGs 时区进行调整。

这会导致在不同时区运行 DAG 时出现意外行为,尽管时区偏移早于 UTC 的 DAG execution_date 是错误的(早于 2 天而不是 1 天)

我需要一些关于如何根据 DAG 时区更改 execution_date 的建议

谢谢

【问题讨论】:

  • 例如:{{ execution_date.in_timezone('Europe/Amsterdam')?
  • @Elad 很酷,但是每个 DAG 都有自己的时区,所以我需要从模板中访问 dag 的时区
  • 你有它在dag.timezone
  • 感谢@Elad,我能够按照您的建议解决它:在模板中使用:{{ dag_run.logical_date.astimezone(dag.timezone) }},我想知道时区感知 dags 的 execution_date 是否可以在 DAGs 时区中,目前就我而言可以在文档中看到它只在 UTC ...

标签: timezone jinja2 airflow


【解决方案1】:

execution_date 的值采用 UTC。 要转换为不同的时区,您可以这样做:

{{ execution_date.in_timezone('Europe/Amsterdam') }}

如果您在 DAG 中设置时区,您可以从 dag.timezone 访问它并使用 Jinja。

{{ dag_run.execution_date.astimezone(dag.timezone) }}

您已经注意到 execution_date 是不推荐使用的宏,因此您应该使用 logical_date 作为:

{{ dag_run.logical_date.astimezone(dag.timezone) }}

【讨论】:

    猜你喜欢
    • 2020-12-07
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    • 2021-01-20
    • 1970-01-01
    相关资源
    最近更新 更多