Bug 34335 - Run Koha plugin method from CLI
Summary: Run Koha plugin method from CLI
Status: In Discussion
Alias: None
Product: Koha
Classification: Unclassified
Component: Plugin architecture (show other bugs)
Version: Main
Hardware: All All
: P5 - low enhancement (vote)
Assignee: David Cook
QA Contact: Testopia
URL:
Keywords:
: 26169 (view as bug list)
Depends on:
Blocks:
 
Reported: 2023-07-21 01:07 UTC by David Cook
Modified: 2023-10-04 10:39 UTC (History)
3 users (show)

See Also:
Change sponsored?: ---
Patch complexity: ---
Documentation contact:
Documentation submission:
Text to go in the release notes:
Version(s) released in:


Attachments
Bug 34335: Add a script to run Koha plugin methods from the CLI (2.58 KB, patch)
2023-07-21 01:27 UTC, David Cook
Details | Diff | Splinter Review
Bug 34335: Add a script to run Koha plugin methods from the CLI (2.76 KB, patch)
2023-07-21 01:38 UTC, David Cook
Details | Diff | Splinter Review
test plugin (1.31 KB, application/zip)
2023-07-21 01:38 UTC, David Cook
Details
Bug 34335: Add a script to run Koha plugin methods from the CLI (2.91 KB, patch)
2023-07-21 01:44 UTC, David Cook
Details | Diff | Splinter Review
Bug 34335: Add a script to run Koha plugin methods from the CLI (2.97 KB, patch)
2023-07-21 08:54 UTC, Paul Derscheid
Details | Diff | Splinter Review

Note You need to log in before you can comment on or make changes to this bug.
Description David Cook 2023-07-21 01:07:07 UTC
There are times where it would be nice to run a Koha plugin method from the CLI.

This can actually be achieved with very little code thanks to Koha::Plugins::Handler->run().
Comment 1 David Cook 2023-07-21 01:07:24 UTC
*** Bug 26169 has been marked as a duplicate of this bug. ***
Comment 2 David Cook 2023-07-21 01:27:58 UTC Comment hidden (obsolete)
Comment 3 David Cook 2023-07-21 01:28:41 UTC Comment hidden (obsolete)
Comment 4 David Cook 2023-07-21 01:38:22 UTC
Created attachment 153747 [details] [review]
Bug 34335: Add a script to run Koha plugin methods from the CLI

This change adds a script which allows you to run a method from
a Koha plugin.

Test plan:
0. Apply patch
1. Upload test plugin
2. Run the following:
perl misc/run_koha_plugin.pl --class Koha::Plugin::Prosentient::CustomMethodPlugin \
    --method awesome_method --params '{"msg": "this is awesome"}'
3. Note the following CLI messages:
This is a custom method that you're seeing on the CLI!
We got a message: this is awesome
Comment 5 David Cook 2023-07-21 01:38:59 UTC
Created attachment 153748 [details]
test plugin
Comment 6 David Cook 2023-07-21 01:44:41 UTC
Created attachment 153749 [details] [review]
Bug 34335: Add a script to run Koha plugin methods from the CLI

This change adds a script which allows you to run a method from
a Koha plugin.

Test plan:
0. Apply patch
1. Upload test plugin
2. Run the following:
perl misc/run_koha_plugin.pl --class Koha::Plugin::Prosentient::CustomMethodPlugin \
    --method awesome_method --params '{"msg": "this is awesome"}'
3. Note the following CLI messages:
This is a custom method that you're seeing on the CLI!
We got a message: this is awesome
Comment 7 David Cook 2023-07-21 06:27:58 UTC
Passing quoted values to koha-shell kohadev -c "" doesn't work so well... so might need to have alternate ways of providing the params
Comment 8 David Cook 2023-07-21 06:34:46 UTC
(In reply to David Cook from comment #7)
> Passing quoted values to koha-shell kohadev -c "" doesn't work so well... so
> might need to have alternate ways of providing the params

Actually, I figured it out. It just takes a bit of work...
Comment 9 Paul Derscheid 2023-07-21 08:48:45 UTC
This is indeed awesome.
Comment 10 Paul Derscheid 2023-07-21 08:54:23 UTC
Created attachment 153760 [details] [review]
Bug 34335: Add a script to run Koha plugin methods from the CLI

This change adds a script which allows you to run a method from
a Koha plugin.

Test plan:
0. Apply patch
1. Upload test plugin
2. Run the following:
perl misc/run_koha_plugin.pl --class Koha::Plugin::Prosentient::CustomMethodPlugin \
    --method awesome_method --params '{"msg": "this is awesome"}'
3. Note the following CLI messages:
This is a custom method that you're seeing on the CLI!
We got a message: this is awesome

Signed-off-by: Paul Derscheid <paul.derscheid@lmscloud.de>

Great stuff! Example works flawlessly. I will now try to break it ;)
Comment 11 Jonathan Druart 2023-07-21 09:01:44 UTC
Can you provide some use cases?
Comment 12 David Cook 2023-07-24 00:48:16 UTC
(In reply to Jonathan Druart from comment #11)
> Can you provide some use cases?

I'm working on a plugin where we schedule a cronjob to execute a plugin method every X minutes. (The "cronjob_nightly" hook isn't frequent/granular enough.)

Another use case would be allowing sysadmins to run tools manually on the backend. Could be useful for batch tasks, infrequent tasks, etc.  

It could be useful for integrations with other systems on the server where you want to avoid the overhead of a HTTP API. (This is something I considered in 2020.)

You could use it to configure a plugin in a more automation friendly way. So you could use Ansible to unzip a plugin to a plugin_dir directory, install it with "./misc/devel/install_plugins.pl", and then use this to configure it. 

--

Anyway, the first use case is the one I'm intentionally targeting. At the moment, I've included this script in the plugin itself.
Comment 13 Marcel de Rooy 2023-09-29 09:25:27 UTC
Why not just call something like:

perl -MKoha::Plugin::RMA::ItemHook::Adapter -e"Koha::Plugin::RMA::ItemHook::Adapter->new->install"

etc.

Do we really need a script for that?
Comment 14 David Cook 2023-10-04 00:24:04 UTC
(In reply to Marcel de Rooy from comment #13)
> Why not just call something like:
> 
> perl -MKoha::Plugin::RMA::ItemHook::Adapter
> -e"Koha::Plugin::RMA::ItemHook::Adapter->new->install"
> 
> etc.
> 
> Do we really need a script for that?

That's an overly simplistic example.

For a more complicated example, you'd need to write a full custom Perl script. 

This change means you don't need a custom script. You can just pass in configuration/data.
Comment 15 David Cook 2023-10-04 00:26:55 UTC
If folk don't want it in core Koha, that's fine.

I'm already using this pattern in existing plugins. I've found it useful and thought others would too.
Comment 16 Marcel de Rooy 2023-10-04 10:39:03 UTC
(In reply to David Cook from comment #15)
> If folk don't want it in core Koha, that's fine.
> 
> I'm already using this pattern in existing plugins. I've found it useful and
> thought others would too.

I do not have a strong objection either. Just asking for some feedback.