execution_date jinja resolving as a string

怎甘沉沦 提交于 2020-01-16 08:59:24

问题


I have an airflow dag that uses the following jinja template: "{{ execution_date.astimezone('Etc/GMT+6').subtract(days=1).strftime('%Y-%m-%dT00:00:00') }}"

This template works in other dags, and it works when the schedule_interval for the dag is set to timedelta(hours=1). However, when we set the schedule interval to 0 8 * * *, it throws the following traceback at runtime:

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

It appears the execution date being passed in is a string, not a datetime object; but I am only able to hit this error on this specific dag, and no others. I've tried deleting the dag entirely and recreating it with no luck.


回答1:


Looks like astimezone(..) function is misbehaving, it expects a datetime.tzinfo while you are passing it an str argument ('Etc/GMT+6')

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

While I couldn't make the exact thing work, I believe following achieves pretty much the same effect as what you are trying

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

Recall that

  • execution_date macro is a Pendulum object
  • in_timezone(..) converts it into a datetime.datetime(..)
  • then we just add a datetime.timedelta(days=1) to it


来源:https://stackoverflow.com/questions/56958884/execution-date-jinja-resolving-as-a-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!