Need to access schedule time in DockerOperator in Airflow

两盒软妹~` 提交于 2020-01-06 05:08:39

问题


Need to access schedule time in airflow's docker operator. For example

t1 = DockerOperator(
task_id="task",
dag=dag,
image="test-runner:1.0",
docker_url="xxx.xxx.xxx.xxx:2376",
environment={"FROM": "{{(execution_date + macros.timedelta(hours=6,minutes=(30))).isoformat()}}"})

Basically, I need to populate schedule time as docker environment.


回答1:


First macros only works if it is a template_fields. Second, you need to check which version of airflow you are using, if you are using 1.9 or below, it is likely it won't work for DockerOperator which @tobi6 mentioned in the comment, since template_fields only included command (https://github.com/apache/incubator-airflow/blob/v1-9-stable/airflow/operators/docker_operator.py#L91)
However, 1.10 stable add environment there for DockerOperator. https://github.com/apache/incubator-airflow/blob/v1-10-stable/airflow/operators/docker_operator.py#L103




回答2:


As other poster mentioned, the DockerOperator in Airflow 1.9 is only expecting the command field to be templated, but it is fairly trivial to modify the templatable fields on an Operator.

from airflow.operators import DockerOperator
DockerOperator.template_fields = (command, environment)

And then you can instantiate your operator as you normally would.




回答3:


A quick update :)

The environment and command parameters of the DockerOperator can be templated. I made an article about it: https://marclamberti.com/blog/how-to-use-dockeroperator-apache-airflow/

Also, don't hesitate to take a look at the source code to see which parameter can be templated or not: https://github.com/apache/airflow/blob/master/airflow/operators/docker_operator.py Have a good day



来源:https://stackoverflow.com/questions/51608355/need-to-access-schedule-time-in-dockeroperator-in-airflow

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