execution_date jinja解析为字符串 [英] execution_date jinja resolving as a string
问题描述
我有一个使用以下jinja模板的气流控制器: {{execution_date.astimezone('Etc / GMT + 6')。subtract(days = 1).strftime('%Y- %m-%dT00:00:00')}}
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') }}"
此模板可在其他dag中使用,当<$将dag的c $ c> schedule_interval 设置为 timedelta(hours = 1)
。但是,当我们将调度间隔设置为 0 8 * * *
时,它将在运行时引发以下回溯:
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
似乎传递的执行日期是字符串,而不是日期时间对象;但我只能在此特定的dag上遇到此错误,而其他任何人都无法。我尝试完全删除该dag,然后重新创建它,但是没有运气。
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.
推荐答案
看起来像 astimezone( ..)
函数的行为不正常,它在您传递 str $ c时期望
datetime.tzinfo
$ c>自变量('Etc / GMT + 6'
)
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) }}
回想一下
-
execution_date
宏是摆锤
对象 -
in_timezone(..)
将其转换为datetime.datetime(..)
- 然后我们只添加一个
datetime.timedelta(days = 1)
execution_date
macro is aPendulum
objectin_timezone(..)
converts it into adatetime.datetime(..)
- then we just add a
datetime.timedelta(days=1)
to it
这篇关于execution_date jinja解析为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!