Matipedia
Matipedia

Reputation: 15

Getting execution date on an operator in Airflow

I created an operator I want to use.

On the execute method I'm calling a function that requiers a date for when to run, but I just haven't been able to get the date. I could use a datetime.now but that would not work for when I need to reprocess the past.

On the operator I need to have:

def execute(self, context):
  log.info("Starting my example function")

  running_date = 'How do I get the date?  :('
  example_function(running_date)

  log.info("The function ran for date" + str(running_date))

Upvotes: 0

Views: 1729

Answers (1)

Alessandro Cosentino
Alessandro Cosentino

Reputation: 2364

From your code snippet, I assume you are interested in a PythonOperator. If you set provide_context=True when you create the operator and you have the signature of the function accepting **kwargs, then you can have access to the execution date from within the function through kwargs['execution_date'].

def execute(self, **kwargs):
    execution_date = kwargs['execution_date']
    ...

PythonOperator(
    task_id='execution_date_task_example',
    provide_context=True,
    python_callable=execute,
    ...
)

Upvotes: 2

Related Questions