Skip to content

Instantly share code, notes, and snippets.

@Nihhaar
Last active September 16, 2021 12:52
Show Gist options
  • Save Nihhaar/b84ec0e02720fdcaf60b169166887a75 to your computer and use it in GitHub Desktop.
Save Nihhaar/b84ec0e02720fdcaf60b169166887a75 to your computer and use it in GitHub Desktop.
DAG factory to load dags from other directories and show load errors on the Airflow UI
import os
import sys
import traceback
from airflow.models import DagBag
from airflow.utils.session import provide_session
from airflow.dag_processing.processor import DagFileProcessor
DAG_DIRS = ['<path/to/dir1>', '<path/to/dir2>', ...]
@provide_session
def build_dags(session=None):
for dir in DAG_DIRS:
# Load DAGs from directory
dag_bag = DagBag(dir, include_examples=False, include_smart_sensor=False)
if dag_bag:
for dag_id, dag in dag_bag.dags.items():
globals()[dag_id] = dag
# Show errors on the Airflow UI
DagFileProcessor.update_import_errors(session, dag_bag)
if __name__ == '__main__':
build_dags()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment