Pianificare i Dag

Introduzione ad Apache Airflow in Python

Mike Metzger

Data Engineer

Dag Run

  • Un'istanza specifica di un workflow in un momento
  • Avviabile manualmente o via schedule
  • Mantiene lo stato del workflow e dei task
    • running
    • failed
    • success
1 https://airflow.apache.org/docs/stable/scheduler.html
Introduzione ad Apache Airflow in Python

Vista Dag Run

Pagina Dag Runs di Airflow con l'elenco delle esecuzioni recenti di tutti i Dag

Introduzione ad Apache Airflow in Python

Stato dei Dag Run

Pagina Dag Runs di Airflow che mostra la colonna dello stato per ogni esecuzione

Introduzione ad Apache Airflow in Python

Dettagli dello schedule

Quando pianifichi un Dag, considera questi attributi:

  • start_date - Data/ora iniziale per pianificare il run del Dag
  • end_date - Opzionale: quando smettere di creare nuove istanze del Dag
    • start_date ed end_date usano un oggetto datetime(year, month, day), ad esempio:
       from pendulum import datetime
       start_date=datetime(2026, 4, 10, tz="UTC")
      
Introduzione ad Apache Airflow in Python

Schedule

schedule indica:

  • Frequenza di esecuzione del Dag
  • Tra start_date ed end_date
  • Definibile con sintassi cron, preset integrati o timedeltas.
Introduzione ad Apache Airflow in Python

Sintassi cron

Diagramma della sintassi cron con i cinque campi tempo separati da spazi

  • Dal formato Unix cron
  • 5 campi separati da spazio
  • * indica ogni intervallo (es. ogni minuto, ogni giorno)
  • Campi con valori separati da virgole per liste di valori
Introduzione ad Apache Airflow in Python

Esempi cron

Diagramma della sintassi cron con i cinque campi tempo separati da spazi

0 12 * * *              # Esegui ogni giorno a mezzogiorno
* * 25 2 *              # Esegui ogni minuto il 25 febbraio
0,15,30,45 * * * *      # Esegui ogni 15 minuti
Introduzione ad Apache Airflow in Python

Preset dello scheduler Airflow

Preset:

  • @hourly
  • @daily
  • @weekly
  • @monthly
  • @yearly

equivalente cron:

  • 0 * * * *
  • 0 0 * * *
  • 0 0 * * 0
  • 0 0 1 * *
  • 0 0 1 1 *
1 https://airflow.apache.org/docs/stable/scheduler.html
Introduzione ad Apache Airflow in Python

Preset speciali

Airflow ha tre preset speciali per schedule:

  • None - Nessuna pianificazione, per Dag avviati manualmente
  • @once - Pianifica una sola volta
  • @continuous - Avvia subito dopo la fine del run precedente
Introduzione ad Apache Airflow in Python

timedelta

  • Si può usare anche pendulum.duration
  • duration(hours=6)
  • duration(minutes=30)
from pendulum import duration

@dag(
  dag_id="example_dag"
  schedule=duration(days=2)
)
Introduzione ad Apache Airflow in Python

Applicare gli schedule

  • Schedule definito nel Dag
  • Usa il parametro schedule:
@dag(
  dag_id="example_dag",
  schedule="0 12 * * *"
)
@dag(
  dag_id="example_dag",
  schedule="@daily"     
)
Introduzione ad Apache Airflow in Python

Problemi di schedule

Quando pianifichi un Dag, Airflow:

  • Richiede che passi un intervallo completo oltre la start date
  • Pianifica il task a start_date + schedule
'start_date': datetime(2026, 2, 25, tz="UTC")
'schedule': @daily

La prima esecuzione possibile del Dag è il 26 febbraio 2026

Introduzione ad Apache Airflow in Python

Ayo berlatih!

Introduzione ad Apache Airflow in Python

Preparing Video For Download...