RTM
RTM

Reputation: 789

How can get the execution hour in Airflow DAG context

I would like to get the execution hour inside a DAG context. I checked and found that {{ds}} provides only the execution date and not time. Is there any way to get the hour at which the DAG gets executed on any given day ?

with DAG(dag_id="dag_name", schedule_interval="30 * * * *", max_active_runs=1) as dag:
        features_hourly = KubernetesPodOperator(
        task_id="task-name",
        name="task-name",
        cmds=[
            "python", "-m", "sql_library.scripts.sql_executor",
            "--template", "format",
            "--env-names", "'" + json.dumps(["SCHEMA"]) + "'",
            "--vars", "'" + json.dumps({
                "EXECUTION_DATE": "{{ ds }}", 
                "PREDICTION_HOUR": ??,
                }) + "'",
            "sql_filename.sql",
        ],
        **default_task_params,
    )

Upvotes: 0

Views: 4513

Answers (1)

Bas Harenslak
Bas Harenslak

Reputation: 3064

execution_date is a Pendulum.DateTime object which holds an attribute hour (docs):

{{ execution_date.hour }}

You can find examples and more details about the template variables in the docs.

Note that execution_date is deprecated since Airflow 2.2. The equivalent is now {{ dag_run.logical_date }}.

Upvotes: 2

Related Questions