Skip to content
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

Fix missing on_load trigger for folder-based plugins #15208

Merged
merged 1 commit into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions airflow/plugins_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,23 @@ def is_valid_plugin(plugin_obj):
return False


def register_plugin(plugin_instance):
"""
Start plugin load and register it after success initialization

:param plugin_instance: subclass of AirflowPlugin
"""
global plugins # pylint: disable=global-statement
plugin_instance.on_load()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to check if on_load is present since we already verify the plugin is a subclass of AirflowPlugin.

plugins.append(plugin_instance)


def load_entrypoint_plugins():
"""
Load and register plugins AirflowPlugin subclasses from the entrypoints.
The entry_point group should be 'airflow.plugins'.
"""
global import_errors # pylint: disable=global-statement
global plugins # pylint: disable=global-statement

log.debug("Loading plugins from entrypoints")

Expand All @@ -202,10 +212,8 @@ def load_entrypoint_plugins():
continue

plugin_instance = plugin_class()
if callable(getattr(plugin_instance, 'on_load', None)):
plugin_instance.on_load()
plugin_instance.source = EntryPointSource(entry_point, dist)
plugins.append(plugin_instance)
plugin_instance.source = EntryPointSource(entry_point, dist)
register_plugin(plugin_instance)
except Exception as e: # pylint: disable=broad-except
log.exception("Failed to import plugin %s", entry_point.name)
import_errors[entry_point.module] = str(e)
Expand All @@ -214,11 +222,9 @@ def load_entrypoint_plugins():
def load_plugins_from_plugin_directory():
"""Load and register Airflow Plugins from plugins directory"""
global import_errors # pylint: disable=global-statement
global plugins # pylint: disable=global-statement
log.debug("Loading plugins from directory: %s", settings.PLUGINS_FOLDER)

for file_path in find_path_from_directory(settings.PLUGINS_FOLDER, ".airflowignore"):

if not os.path.isfile(file_path):
continue
mod_name, file_ext = os.path.splitext(os.path.split(file_path)[-1])
Expand All @@ -236,8 +242,7 @@ def load_plugins_from_plugin_directory():
for mod_attr_value in (m for m in mod.__dict__.values() if is_valid_plugin(m)):
plugin_instance = mod_attr_value()
plugin_instance.source = PluginsDirectorySource(file_path)
plugins.append(plugin_instance)

register_plugin(plugin_instance)
except Exception as e: # pylint: disable=broad-except
log.exception(e)
log.error('Failed to import plugin %s', file_path)
Expand Down
7 changes: 7 additions & 0 deletions tests/plugins/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,10 @@ class MockPluginB(AirflowPlugin):

class MockPluginC(AirflowPlugin):
name = 'plugin-c'


class AirflowTestOnLoadPlugin(AirflowPlugin):
name = 'preload'

def on_load(self, *args, **kwargs):
self.name = 'postload'
47 changes: 47 additions & 0 deletions tests/plugins/test_plugins_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,32 @@
# under the License.
import importlib
import logging
import os
import sys
import tempfile
from unittest import mock

import pytest

from airflow.hooks.base import BaseHook
from airflow.plugins_manager import AirflowPlugin
from airflow.www import app as application
from tests.test_utils.config import conf_vars
from tests.test_utils.mock_plugins import mock_plugin_manager

py39 = sys.version_info >= (3, 9)
importlib_metadata = 'importlib.metadata' if py39 else 'importlib_metadata'

ON_LOAD_EXCEPTION_PLUGIN = """
from airflow.plugins_manager import AirflowPlugin

class AirflowTestOnLoadExceptionPlugin(AirflowPlugin):
name = 'preload'

def on_load(self, *args, **kwargs):
raise Exception("oops")
"""


class TestPluginsRBAC:
@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -146,6 +159,40 @@ class TestPropertyHook(BaseHook):
assert caplog.records[-1].levelname == 'DEBUG'
assert caplog.records[-1].msg == 'Loading %d plugin(s) took %.2f seconds'

def test_loads_filesystem_plugins(self, caplog):
from airflow import plugins_manager

with mock.patch('airflow.plugins_manager.plugins', []):
plugins_manager.load_plugins_from_plugin_directory()

assert 5 == len(plugins_manager.plugins)
for plugin in plugins_manager.plugins:
if 'AirflowTestOnLoadPlugin' not in str(plugin):
continue
assert 'postload' == plugin.name
break
else:
pytest.fail("Wasn't able to find a registered `AirflowTestOnLoadPlugin`")

assert caplog.record_tuples == []

def test_loads_filesystem_plugins_exception(self, caplog):
from airflow import plugins_manager

with mock.patch('airflow.plugins_manager.plugins', []):
with tempfile.TemporaryDirectory() as tmpdir:
with open(os.path.join(tmpdir, 'testplugin.py'), "w") as f:
f.write(ON_LOAD_EXCEPTION_PLUGIN)

with conf_vars({('core', 'plugins_folder'): tmpdir}):
plugins_manager.load_plugins_from_plugin_directory()

assert plugins_manager.plugins == []

received_logs = caplog.text
assert 'Failed to import plugin' in received_logs
assert 'testplugin.py' in received_logs

def test_should_warning_about_incompatible_plugins(self, caplog):
class AirflowAdminViewsPlugin(AirflowPlugin):
name = "test_admin_views_plugin"
Expand Down