Bug 29672: Increase performance of Koha::Plugins->call
authorJulian Maurice <julian.maurice@biblibre.com>
Thu, 9 Dec 2021 13:49:32 +0000 (14:49 +0100)
committerTomas Cohen Arazi <tomascohen@theke.io>
Thu, 3 Nov 2022 14:28:11 +0000 (11:28 -0300)
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 <david@davidnind.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Koha/Plugins.pm
t/db_dependent/Koha/Plugins/Plugins.t

index f7b7166..997d4a4 100644 (file)
@@ -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
index 0360a8a..7d9166d 100755 (executable)
@@ -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 });