-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changes to support Airflow TaskFlow API
- Loading branch information
Showing
7 changed files
with
685 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
105 changes: 105 additions & 0 deletions
105
observatory-platform/observatory/platform/refactor/sensors.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# Copyright 2020, 2021 Curtin University | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# Author: Tuan Chien, Keegan Smith, Jamie Diprose | ||
|
||
from __future__ import annotations | ||
|
||
from datetime import timedelta | ||
from functools import partial | ||
from typing import Callable, List, Optional | ||
|
||
import pendulum | ||
from airflow.models import DagRun | ||
from airflow.sensors.external_task import ExternalTaskSensor | ||
from airflow.utils.db import provide_session | ||
from sqlalchemy.orm.scoping import scoped_session | ||
|
||
|
||
class DagCompleteSensor(ExternalTaskSensor): | ||
""" | ||
A sensor that awaits the completion of an external dag by default. Wait functionality can be customised by | ||
providing a different execution_date_fn. | ||
The sensor checks for completion of a dag with "external_dag_id" on the logical date returned by the | ||
execution_date_fn. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
task_id: str, | ||
external_dag_id: str, | ||
mode: str = "reschedule", | ||
poke_interval: int = 1200, # Check if dag run is ready every 20 minutes | ||
timeout: int = int(timedelta(days=1).total_seconds()), # Sensor will fail after 1 day of waiting | ||
check_existence: bool = True, | ||
execution_date_fn: Optional[Callable] = None, | ||
**kwargs, | ||
): | ||
""" | ||
:param task_id: the id of the sensor task to create | ||
:param external_dag_id: the id of the external dag to check | ||
:param mode: The mode of the scheduler. Can be reschedule or poke. | ||
:param poke_interval: how often to check if the external dag run is complete | ||
:param timeout: how long to check before the sensor fails | ||
:param check_existence: whether to check that the provided dag_id exists | ||
:param execution_date_fn: a function that returns the logical date(s) of the external DAG runs to query for, | ||
since you need a logical date and a DAG ID to find a particular DAG run to wait for. | ||
""" | ||
|
||
if execution_date_fn is None: | ||
execution_date_fn = partial(get_logical_dates, external_dag_id) | ||
|
||
super().__init__( | ||
task_id=task_id, | ||
external_dag_id=external_dag_id, | ||
mode=mode, | ||
poke_interval=poke_interval, | ||
timeout=timeout, | ||
check_existence=check_existence, | ||
execution_date_fn=execution_date_fn, | ||
**kwargs, | ||
) | ||
|
||
|
||
@provide_session | ||
def get_logical_dates( | ||
external_dag_id: str, logical_date: pendulum.DateTime, session: scoped_session = None, **context | ||
) -> List[pendulum.DateTime]: | ||
"""Get the logical dates for a given external dag that fall between and returns its data_interval_start (logical date) | ||
:param external_dag_id: the DAG ID of the external DAG we are waiting for. | ||
:param logical_date: the logic date of the waiting DAG. | ||
:param session: the SQL Alchemy session. | ||
:param context: the Airflow context. | ||
:return: the last logical date of the external DAG that falls before the data interval end of the waiting DAG. | ||
""" | ||
|
||
data_interval_end = context["data_interval_end"] | ||
dag_runs = ( | ||
session.query(DagRun) | ||
.filter( | ||
DagRun.dag_id == external_dag_id, | ||
DagRun.data_interval_end <= data_interval_end, | ||
) | ||
.all() | ||
) | ||
dates = [d.logical_date for d in dag_runs] | ||
dates.sort(reverse=True) | ||
|
||
# If more than 1 date return first date | ||
if len(dates) >= 2: | ||
dates = [dates[0]] | ||
|
||
return dates |
77 changes: 77 additions & 0 deletions
77
observatory-platform/observatory/platform/refactor/tasks.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Copyright 2020-2023 Curtin University | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
from __future__ import annotations | ||
|
||
import logging | ||
from typing import List, Optional | ||
|
||
import airflow | ||
from airflow.decorators import task | ||
from airflow.exceptions import AirflowNotFoundException | ||
from airflow.hooks.base import BaseHook | ||
from airflow.models import Variable | ||
|
||
|
||
@task | ||
def check_dependencies(airflow_vars: Optional[List[str]] = None, airflow_conns: Optional[List[str]] = None, **context): | ||
"""Checks if the given Airflow Variables and Connections exist. | ||
:param airflow_vars: the Airflow Variables to check exist. | ||
:param airflow_conns: the Airflow Connections to check exist. | ||
:return: None. | ||
""" | ||
|
||
vars_valid = True | ||
conns_valid = True | ||
if airflow_vars: | ||
vars_valid = check_variables(*airflow_vars) | ||
if airflow_conns: | ||
conns_valid = check_connections(*airflow_conns) | ||
|
||
if not vars_valid or not conns_valid: | ||
raise AirflowNotFoundException("Required variables or connections are missing") | ||
|
||
|
||
def check_variables(*variables): | ||
"""Checks whether all given airflow variables exist. | ||
:param variables: name of airflow variable | ||
:return: True if all variables are valid | ||
""" | ||
is_valid = True | ||
for name in variables: | ||
try: | ||
Variable.get(name) | ||
except AirflowNotFoundException: | ||
logging.error(f"Airflow variable '{name}' not set.") | ||
is_valid = False | ||
return is_valid | ||
|
||
|
||
def check_connections(*connections): | ||
"""Checks whether all given airflow connections exist. | ||
:param connections: name of airflow connection | ||
:return: True if all connections are valid | ||
""" | ||
is_valid = True | ||
for name in connections: | ||
try: | ||
BaseHook.get_connection(name) | ||
except airflow.exceptions.AirflowNotFoundException: | ||
logging.error(f"Airflow connection '{name}' not set.") | ||
is_valid = False | ||
return is_valid |
Oops, something went wrong.