Bugzilla – Attachment 133193 Details for
Bug 30410
Add a way for plugins to register background tasks
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 30410: Add a way for plugins to expose tasks they implement
Bug-30410-Add-a-way-for-plugins-to-expose-tasks-th.patch (text/plain), 3.71 KB, created by
Kyle M Hall (khall)
on 2022-04-11 16:57:53 UTC
(
hide
)
Description:
Bug 30410: Add a way for plugins to expose tasks they implement
Filename:
MIME Type:
Creator:
Kyle M Hall (khall)
Created:
2022-04-11 16:57:53 UTC
Size:
3.71 KB
patch
obsolete
>From 2d3bd5c792dae1013eecf86740df0db52d70aa8c Mon Sep 17 00:00:00 2001 >From: Tomas Cohen Arazi <tomascohen@theke.io> >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 <tomascohen@theke.io> > >Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com> >--- > 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.20.1
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 30410
:
132814
|
132815
|
133180
|
133181
|
133192
| 133193 |
133194
|
133195
|
133386