The idea is to have a way for plugins to define 'code' => 'class' mappings so the worker(s) can process tasks through plugin-provided classes.
Created attachment 132814 [details] [review] Bug 30410: Unit tests
Created attachment 132815 [details] [review] 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>
(In reply to Tomás Cohen Arazi from comment #2) > 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_. What's an example 'namespace' attribute? Is that used to prevent collisions with task names?
(In reply to David Cook from comment #3) > (In reply to Tomás Cohen Arazi from comment #2) > > 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_. > > What's an example 'namespace' attribute? Is that used to prevent collisions > with task names? There's a clear example in the patches. Thanks
(In reply to Tomás Cohen Arazi from comment #4) > There's a clear example in the patches. > Thanks My bad. I had already read the main patch but not the unit tests patch. I should've thought to look there, although I didn't think the example was that clear. I had to re-read it a few times. I suppose the plugin's "name" isn't usable since it's human-friendly and not machine friendly. A default method inherited from Koha::Plugins::Base that does something like this might be useful: sub namespace { my $namespace = lc(__PACKAGE__); $namespace =~ s/::/_/g return $namespace } I suppose free text and auto-generated both have their pros and cons...
(In reply to David Cook from comment #5) > (In reply to Tomás Cohen Arazi from comment #4) > > There's a clear example in the patches. > > Thanks > > I suppose the plugin's "name" isn't usable since it's human-friendly and not > machine friendly. > > I suppose free text and auto-generated both have their pros and cons... The main problem is how big the column needs to be. I suggest you go try and sign the patches. Thanks!
Created attachment 133180 [details] [review] Bug 30410: Unit tests Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Created attachment 133181 [details] [review] 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: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Nice clear code and gives us a great way to develop background tasks. All works as expected, Signing off
Created attachment 133192 [details] [review] Bug 30410: Unit tests Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Created attachment 133193 [details] [review] 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>
Created attachment 133194 [details] [review] Bug 30410: (QA follow-up) Pluralise new methods, fix minor issues with language Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Created attachment 133195 [details] [review] Bug 30410: (QA follow-up) Only fetch plugins mapping once
Please add to https://wiki.koha-community.org/wiki/Koha_Plugin_Hooks
Pushed to master for 22.05, thanks to everybody involved [U+1F984]
Created attachment 133386 [details] [review] Bug 30410: (follow-up) Instantiate object in unit test This patch updates the unit test for the BackgroundJob->_derived_class() private method to instantiate a proper object to work on instead of calling the type_to_class_mapping method directly as non-object method. Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Pushed to master : Bug 30410: (follow-up) Instantiate object in unit test
Do we have any examples of this one?
Wiki page updated
(In reply to Fridolin Somers from comment #19) > Wiki page updated Thanks, Frido, although I don't see any example plugins?
I had a look but I really dont understand how to implement the hook. A pool request on kitchen sink is much welcomed.
(In reply to Fridolin Somers from comment #21) > I had a look but I really dont understand how to implement the hook. > A pool request on kitchen sink is much welcomed. Ok. I'm on leave the next 4 days, but I'll hopefully look at creating a plugin next week (if I can figure out how to!)
(In reply to Tomás Cohen Arazi from comment #4) > There's a clear example in the patches. (In reply to Martin Renvoize from comment #9) > Nice clear code and gives us a great way to develop background tasks. After nearly a year, I still can't understand this code and have no idea how to develop a plugin to use this hook. I'm keen to try it out, so I'll keep trying to figure it out, but it would be great to have some assistance either here or on the Koha Plugin Hooks wiki page...