From ebae23153dd06ccd2b37d7b206e4573aac656a7d Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Thu, 31 Mar 2022 17:33:21 +0200 Subject: [PATCH] Bug 30410: Add a way for plugins to expose tasks they implement This patch implements a mechanism for plugins to tell Koha they implement background jobs, by returning a mapping ``` task => class ``` * _task_ is a string that will be used when queueing new tasks, to let the workers know which class to instantiate to launch the job (a.k.a. process). * _class_ is a string for a class name. For this to work, plugins need to include the 'namespace' attribute in their metadata. If they don't, they will be skipped when listing the valid _background jobs_. After this, high-level methods for letting plugins (easily) enqueue their tasks will be provided. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/db_dependent/Koha/Plugins/BackgroundJob.t => SUCCESS: Tests pass! 3. Sign off :-D Signed-off-by: Tomas Cohen Arazi Signed-off-by: Kyle M Hall Signed-off-by: Fridolin Somers --- Koha/BackgroundJob.pm | 68 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/Koha/BackgroundJob.pm b/Koha/BackgroundJob.pm index 9d05f172bf..23d1136d6a 100644 --- a/Koha/BackgroundJob.pm +++ b/Koha/BackgroundJob.pm @@ -25,6 +25,7 @@ use Try::Tiny qw( catch try ); use C4::Context; use Koha::DateUtils qw( dt_from_string ); use Koha::Exceptions; +use Koha::Plugins; use base qw( Koha::Object ); @@ -250,9 +251,31 @@ sub _derived_class { =head3 type_to_class_mapping + my $mapping = Koha::BackgrounJob->new->type_to_class_mapping; + +Returns the available types to class mappings. + =cut sub type_to_class_mapping { + my ($self) = @_; + + my $plugins_mapping = $self->plugin_type_to_class; + + return ($plugins_mapping) + ? { %{ $self->core_type_to_class }, %{ $self->plugin_type_to_class } } + : $self->core_type_to_class; +} + +=head3 core_type_to_class + + my $mappings = Koha::BackgrounJob->new->core_type_to_class + +Returns the core background jobs types to class mappings. + +=cut + +sub core_type_to_class { return { batch_authority_record_deletion => 'Koha::BackgroundJob::BatchDeleteAuthority', batch_authority_record_modification => 'Koha::BackgroundJob::BatchUpdateAuthority', @@ -264,6 +287,51 @@ sub type_to_class_mapping { }; } +=head3 plugin_type_to_class + + my $mappings = Koha::BackgroundJob->new->plugin_type_to_class + +Returns the plugin-refined background jobs types to class mappings. + +=cut + +sub plugin_type_to_class { + my ($self) = @_; + + unless ( exists $self->{_plugin_mapping} ) { + my @plugins = Koha::Plugins->new()->GetPlugins( { method => 'background_tasks', } ); + + foreach my $plugin (@plugins) { + + my $tasks = $plugin->background_tasks; + my $metadata = $plugin->get_metadata; + + unless ( $metadata->{namespace} ) { + Koha::Logger->get->warn( +"The plugin includes the 'background_tasks' method, but doesn't provide the required 'namespace' method (" + . $plugin->{class} + . ')' ); + next; + } + + my $namespace = $metadata->{namespace}; + + foreach my $type ( keys %{$tasks} ) { + my $class = $tasks->{$type}; + + # skip if conditions not met + next unless $type and $class; + + my $key = "plugin_$namespace" . "_$type"; + + $self->{_plugin_mapping}->{$key} = $tasks->{$type}; + } + } + } + + return $self->{_plugin_mapping}; +} + =head3 _type =cut -- 2.39.5