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 <david@davidnind.com> Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com> Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
parent
00f72975ed
commit
08f24d0adf
2 changed files with 49 additions and 13 deletions
|
@ -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
|
||||
|
|
|
@ -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 });
|
||||
|
|
Loading…
Reference in a new issue