Skip to content

Instantly share code, notes, and snippets.

@LucasMMota
Created August 24, 2021 23:15
Show Gist options
  • Save LucasMMota/cce2c8cf1058a0bb35eb85b6ad898544 to your computer and use it in GitHub Desktop.
Save LucasMMota/cce2c8cf1058a0bb35eb85b6ad898544 to your computer and use it in GitHub Desktop.
Example for testing SLA in Airflow
from datetime import datetime, timedelta
from airflow.models import DAG
from airflow.operators.bash_operator import BashOperator
dag = DAG(
dag_id="my_dag_to_test_sla",
catchup=False,
start_date=datetime(2021, 8, 28, 0, 0, 0),
# If schedule_interval=None, SLA won't be evaluated for this DAG!
schedule_interval="*/5 * * * *",
)
task_1 = BashOperator(
task_id='task_1',
bash_command='sleep 150',
dag=dag,
)
task_2 = BashOperator(
dag=dag,
task_id='task_2',
bash_command='echo 1',
# If the task finishes (or starts) after 120 seconds,
# it will register an SLA miss
sla=timedelta(seconds=120)
)
# The task `task_2` will start after 150 seconds
# so Airflow will register an SLA miss
task_1 >> task_2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment