气流:将执行日期前的上一个星期二传递给BashOperator

时间:2018-10-19 04:45:12

标签: airflow

我有一个每天在星期日运行的dag。 BashOperator是一项任务,其设置如下:

t = BashOperator(
   task_id='print_date',
   bash_command="echo Previous Tuesday is: "

def get_latest_build_date(dt):
   bd = dt + relativedelta(weekday=TU(-1))
   return bd.strftime('%Y%m%d')

如何将当前执行日期传递给get_latest_build_date函数?

bash_command="echo Previous Tuesday is: {{ get_latest_build_date(ds) }} 将与jinja模板一起使用。

1 个答案:

答案 0 :(得分:2)

使用user_defined_macros中的DAG

def get_latest_build_date(dt):
   bd = dt + relativedelta(weekday=TU(-1))
   return bd.strftime('%Y%m%d')

with DAG('name',
         ...,
         user_defined_macros={'prior_tues': get_latest_build_date},) as dag:

  t = BashOperator(
     task_id='print_date',
     bash_command="echo Previous Tuesday is: {{ prior_tues(execution_date) }}"
相关问题