execution_date jinja解析为字符串

时间:2019-07-09 18:44:36

标签: airflow

我有一个气流障碍使用以下jinja模板:"{{ execution_date.astimezone('Etc/GMT+6').subtract(days=1).strftime('%Y-%m-%dT00:00:00') }}"

此模板可在其他dag中使用,并且当dag的schedule_interval设置为timedelta(hours=1)时可以使用。但是,当我们将调度间隔设置为0 8 * * *时,它将在运行时引发以下回溯:

Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/airflow/models/__init__.py", line 1426, in _run_raw_task
    self.render_templates()
  File "/usr/lib/python2.7/site-packages/airflow/models/__init__.py", line 1790, in render_templates
    rendered_content = rt(attr, content, jinja_context)
  File "/usr/lib/python2.7/site-packages/airflow/models/__init__.py", line 2538, in render_template
    return self.render_template_from_field(attr, content, context, jinja_env)
  File "/usr/lib/python2.7/site-packages/airflow/models/__init__.py", line 2520, in render_template_from_field
    for k, v in list(content.items())}
  File "/usr/lib/python2.7/site-packages/airflow/models/__init__.py", line 2520, in <dictcomp>
    for k, v in list(content.items())}
  File "/usr/lib/python2.7/site-packages/airflow/models/__init__.py", line 2538, in render_template
    return self.render_template_from_field(attr, content, context, jinja_env)
  File "/usr/lib/python2.7/site-packages/airflow/models/__init__.py", line 2514, in render_template_from_field
    result = jinja_env.from_string(content).render(**context)
  File "/usr/lib64/python2.7/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/lib64/python2.7/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "<template>", line 1, in top-level template code
TypeError: astimezone() argument 1 must be datetime.tzinfo, not str

似乎传递的执行日期是一个字符串,而不是日期时间对象;但我只能在此特定的dag上遇到此错误,而其他任何人都无法。我尝试完全删除dag并重新创建它,但是没有运气。

1 个答案:

答案 0 :(得分:1)

好像astimezone(..)函数的行为不正常,它在传递datetime.tzinfo参数(str)时期望'Etc/GMT+6'

TypeError: astimezone() argument 1 must be datetime.tzinfo, not str

虽然我无法使确切的事情起作用,但我相信追随可以达到与您尝试的效果几乎相同的效果

{{ execution_date.in_timezone("US/Eastern") - timedelta(days=1) }}

回想一下

  • execution_date macroPendulum对象
  • in_timezone(..)将其转换为datetime.datetime(..)
  • 然后我们只需在其中添加一个datetime.timedelta(days=1)