I want to create a snippet that passes the correct date based on whether the DAG was scheduled or whether it was triggered manually. The DAG runs monthly. The DAG generates a report (A SQL query) based on the data of the previous month.
If I run the DAG scheduled, I can fetch the previous month with the following jinja snippet:
execution_date.month
given that the DAG is scheduled at the end of the previous period (last month) the execution_date will correctly return the last month. However on manual runs this will return the current month (execution date will be the date of the manual trigger).
I want to write a simple macro that deals with this case. However I could not find a good way to programmatically query whether the DAG is triggered programmatically. The best I could come up with is to fetch the run_id
from the database (by creating a macro that has a DB session), check wheter the run_id
contains the word manual
. Is there a better way to solve this problem?