-
Notifications
You must be signed in to change notification settings - Fork 15.3k
Move DAG bundle config into config, not db #44924
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e3d334f
Move DAG bundle config into config, not db
jedcunningham 4fc4f02
Review comments and fix some tests
jedcunningham 5363828
Update erd
jedcunningham afbf181
Fix docs build
jedcunningham c77d57e
typo
jedcunningham 58dc4d1
Add refresh_interval to docstring
jedcunningham abfee68
Merge branch 'main' into bundles_from_config
jedcunningham ff68c78
Merge branch 'main' into bundles_from_config
jedcunningham aa23b88
rename column to active
jedcunningham b05cee5
Fix compat tests
jedcunningham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,96 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you 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 | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from airflow.configuration import conf | ||
from airflow.exceptions import AirflowConfigException | ||
from airflow.models.dagbundle import DagBundleModel | ||
from airflow.utils.log.logging_mixin import LoggingMixin | ||
from airflow.utils.module_loading import import_string | ||
from airflow.utils.session import NEW_SESSION, provide_session | ||
|
||
if TYPE_CHECKING: | ||
from sqlalchemy.orm import Session | ||
|
||
from airflow.dag_processing.bundles.base import BaseDagBundle | ||
|
||
|
||
class DagBundlesManager(LoggingMixin): | ||
"""Manager for DAG bundles.""" | ||
|
||
@property | ||
def bundle_configs(self) -> dict[str, dict]: | ||
"""Get all DAG bundle configurations.""" | ||
configured_bundles = conf.getsection("dag_bundles") | ||
|
||
if not configured_bundles: | ||
return {} | ||
|
||
# If dags_folder is empty string, we remove it. This allows the default dags_folder bundle to be disabled. | ||
if not configured_bundles["dags_folder"]: | ||
jedcunningham marked this conversation as resolved.
Show resolved
Hide resolved
|
||
del configured_bundles["dags_folder"] | ||
|
||
dict_bundles: dict[str, dict] = {} | ||
for key in configured_bundles.keys(): | ||
config = conf.getjson("dag_bundles", key) | ||
jedcunningham marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if not isinstance(config, dict): | ||
raise AirflowConfigException(f"Bundle config for {key} is not a dict: {config}") | ||
dict_bundles[key] = config | ||
|
||
return dict_bundles | ||
|
||
@provide_session | ||
def sync_bundles_to_db(self, *, session: Session = NEW_SESSION) -> None: | ||
known_bundles = {b.name: b for b in session.query(DagBundleModel).all()} | ||
|
||
for name in self.bundle_configs.keys(): | ||
if bundle := known_bundles.get(name): | ||
jedcunningham marked this conversation as resolved.
Show resolved
Hide resolved
|
||
bundle.active = True | ||
else: | ||
session.add(DagBundleModel(name=name)) | ||
self.log.info("Added new DAG bundle %s to the database", name) | ||
|
||
for name, bundle in known_bundles.items(): | ||
if name not in self.bundle_configs: | ||
bundle.active = False | ||
self.log.warning("DAG bundle %s is no longer found in config and has been disabled", name) | ||
|
||
def get_all_dag_bundles(self) -> list[BaseDagBundle]: | ||
""" | ||
Get all DAG bundles. | ||
|
||
:param session: A database session. | ||
|
||
:return: list of DAG bundles. | ||
""" | ||
return [self.get_bundle(name, version=None) for name in self.bundle_configs.keys()] | ||
|
||
def get_bundle(self, name: str, version: str | None = None) -> BaseDagBundle: | ||
""" | ||
Get a DAG bundle by name. | ||
|
||
:param name: The name of the DAG bundle. | ||
:param version: The version of the DAG bundle you need (optional). If not provided, ``tracking_ref`` will be used instead. | ||
|
||
:return: The DAG bundle. | ||
""" | ||
# TODO: proper validation of the bundle configuration so we have better error messages | ||
bundle_config = self.bundle_configs[name] | ||
bundle_class = import_string(bundle_config["classpath"]) | ||
return bundle_class(name=name, version=version, **bundle_config["kwargs"]) |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1 +1 @@ | ||
ccb8ef5583b2a6b3ee3ab4212139c112b92953675655010a6775fffb4945b206 | ||
ba10504bc54d15b2faca37ae9db172848a498e471bbf332e031715f728158ff8 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.