From 08f24d0adfa88aa2648c8880a00d4d20c85e3099 Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Thu, 9 Dec 2021 14:49:32 +0100 Subject: [PATCH] Bug 29672: Increase performance of Koha::Plugins->call Make use of Koha::Cache::Memory::Lite to avoid hitting the database and creating plugins object for every call to Koha::Plugins->call Test plan: 1. Make sure plugins still work by executing `prove t/db_dependent/Koha/Plugins/Plugins.t` 2. Run the test script provided in the following patch to see how it affects performances Signed-off-by: David Nind Signed-off-by: Kyle M Hall Signed-off-by: Tomas Cohen Arazi --- Koha/Plugins.pm | 61 +++++++++++++++++++++------ t/db_dependent/Koha/Plugins/Plugins.t | 1 + 2 files changed, 49 insertions(+), 13 deletions(-) diff --git a/Koha/Plugins.pm b/Koha/Plugins.pm index f7b716638d..997d4a48b7 100644 --- a/Koha/Plugins.pm +++ b/Koha/Plugins.pm @@ -30,6 +30,7 @@ use Try::Tiny; use C4::Context; use C4::Output; +use Koha::Cache::Memory::Lite; use Koha::Exceptions::Plugin; use Koha::Plugins::Methods; @@ -74,25 +75,59 @@ updated by preceding plugins, provided that these plugins support that. sub call { my ($class, $method, @args) = @_; + return unless C4::Context->config('enable_plugins'); + my @responses; - if (C4::Context->config('enable_plugins')) { - my @plugins = $class->new({ enable_plugins => 1 })->GetPlugins({ method => $method }); - @plugins = grep { $_->can($method) } @plugins; - # TODO: Remove warn when after_hold_create is removed from the codebase - warn "after_hold_create is deprecated and will be removed soon. Contact the following plugin's authors: " . join( ', ', map {$_->{metadata}->{name}} @plugins) - if $method eq 'after_hold_create' and @plugins; - foreach my $plugin (@plugins) { - my $response = eval { $plugin->$method(@args) }; - if ($@) { - warn sprintf("Plugin error (%s): %s", $plugin->get_metadata->{name}, $@); + my @plugins = $class->get_enabled_plugins(); + @plugins = grep { $_->can($method) } @plugins; + + # TODO: Remove warn when after_hold_create is removed from the codebase + warn "after_hold_create is deprecated and will be removed soon. Contact the following plugin's authors: " . join( ', ', map {$_->{metadata}->{name}} @plugins) + if $method eq 'after_hold_create' and @plugins; + + foreach my $plugin (@plugins) { + my $response = eval { $plugin->$method(@args) }; + if ($@) { + warn sprintf("Plugin error (%s): %s", $plugin->get_metadata->{name}, $@); + next; + } + + push @responses, $response; + } + + return @responses; +} + +sub get_enabled_plugins { + my ($class) = @_; + + return unless C4::Context->config('enable_plugins'); + + my $cache_key = 'enabled_plugins'; + my $enabled_plugins = Koha::Cache::Memory::Lite->get_from_cache($cache_key); + unless ($enabled_plugins) { + $enabled_plugins = []; + my $rs = Koha::Database->schema->resultset('PluginData'); + $rs = $rs->search({ plugin_key => '__ENABLED__', plugin_value => 1 }); + my @plugin_classes = $rs->get_column('plugin_class')->all(); + foreach my $plugin_class (@plugin_classes) { + unless (can_load(modules => { $plugin_class => undef }, nocache => 1)) { + warn "Failed to load $plugin_class: $Module::Load::Conditional::ERROR"; next; } - push @responses, $response; - } + my $plugin = eval { $plugin_class->new() }; + if ($@ || !$plugin) { + warn "Failed to instantiate plugin $plugin_class: $@"; + next; + } + push @$enabled_plugins, $plugin; + } + Koha::Cache::Memory::Lite->set_in_cache($cache_key, $enabled_plugins); } - return @responses; + + return @$enabled_plugins; } =head2 GetPlugins diff --git a/t/db_dependent/Koha/Plugins/Plugins.t b/t/db_dependent/Koha/Plugins/Plugins.t index 0360a8ab87..7d9166da9a 100755 --- a/t/db_dependent/Koha/Plugins/Plugins.t +++ b/t/db_dependent/Koha/Plugins/Plugins.t @@ -55,6 +55,7 @@ subtest 'call() tests' => sub { $schema->storage->txn_begin; # Temporarily remove any installed plugins data Koha::Plugins::Methods->delete; + $schema->resultset('PluginData')->delete(); t::lib::Mocks::mock_config('enable_plugins', 1); my $plugins = Koha::Plugins->new({ enable_plugins => 1 }); -- 2.39.5