Skip to content

Commit

Permalink
Add background_tasks implementation example
Browse files Browse the repository at this point in the history
Signed-off-by: Tomas Cohen Arazi <[email protected]>
  • Loading branch information
tomascohen committed Oct 13, 2023
1 parent da5b15b commit 30d1446
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 4 deletions.
39 changes: 35 additions & 4 deletions Koha/Plugin/Com/ByWaterSolutions/KitchenSink.pm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ use MARC::Record;
use Mojo::JSON qw(decode_json);;
use URI::Escape qw(uri_unescape);

use Koha::Plugin::Com::ByWaterSolutions::KitchenSink::Greeter;

## Here we set our plugin version
our $VERSION = "{VERSION}";
our $MINIMUM_VERSION = "{MINIMUM_VERSION}";
Expand All @@ -40,6 +42,7 @@ our $metadata = {
description => 'This plugin implements every available feature '
. 'of the plugin system and is meant '
. 'to be documentation and a starting point for writing your own plugins!',
namespace => 'kitchensink',
};

## This is the minimum code required for a plugin's 'new' method
Expand Down Expand Up @@ -85,13 +88,15 @@ sub tool {

my $cgi = $self->{'cgi'};

unless ( $cgi->param('submitted') ) {
$self->tool_step1();
if ( $cgi->param('submitted') ) {
$self->tool_step2();
}
elsif ( $cgi->param('greet') ) {
$self->schedule_greets();
}
else {
$self->tool_step2();
$self->tool_step1();
}

}

## The existiance of a 'to_marc' subroutine means the plugin is capable
Expand Down Expand Up @@ -507,6 +512,20 @@ sub tool_step2 {
$self->output_html( $template->output() );
}

sub schedule_greets {
my ($self) = @_;

my $cgi = $self->{cgi};
my $count = $cgi->param('count');

Koha::Plugin::Com::ByWaterSolutions::KitchenSink::Greeter->new->enqueue( { size => $count } );

my $template = $self->get_template( { file => 'greets_scheduled.tt' } );
$template->param( count => $count );

$self->output_html( $template->output() );
}

## API methods
# If your plugin implements API routes, then the 'api_routes' method needs
# to be implemented, returning valid OpenAPI 2.0 paths serialized as a hashref.
Expand Down Expand Up @@ -645,4 +664,16 @@ sub intranet_catalog_biblio_tab {
return @tabs;
}

=head3 background_tasks
Plugin hook used to register new background_job types
=cut

sub background_tasks {
return {
greeter => 'Koha::Plugin::Com::ByWaterSolutions::KitchenSink::Greeter'
};
}

1;
98 changes: 98 additions & 0 deletions Koha/Plugin/Com/ByWaterSolutions/KitchenSink/Greeter.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package Koha::Plugin::Com::ByWaterSolutions::KitchenSink::Greeter;

# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.

use Modern::Perl;

use base 'Koha::BackgroundJob';

=head1 NAME
KitchenSink::Greeter - Background task for greeting in the logs
This is a subclass of Koha::BackgroundJob.
=head1 API
=head2 Class methods
=head3 job_type
Define the job type of this job: greeter
=cut

sub job_type {
return 'plugin_kitchensink_greeter';
}

=head3 process
Process the modification.
=cut

sub process {
my ( $self, $args ) = @_;

$self->start;

my @messages;
my $report = {
total_greets => $self->size,
total_success => 0,
};

foreach my $step ( 1 .. $self->size ) {

warn "Greeting: Hola! ($step)";

push @messages,
{
type => 'success',
code => 'greeted',
};

$report->{total_success}++;

$self->step;
}

my $data = $self->decoded_data;
$data->{messages} = \@messages;
$data->{report} = $report;

$self->finish($data);
}

=head3 enqueue
Enqueue the new job
=cut

sub enqueue {
my ( $self, $args ) = @_;

$self->SUPER::enqueue(
{
job_size => $args->{size} // 5,
job_args => {},
}
);
}

1;
15 changes: 15 additions & 0 deletions Koha/Plugin/Com/ByWaterSolutions/KitchenSink/greets_scheduled.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[% INCLUDE 'doc-head-open.inc' %]
<title>Koha: Kitchen Sink Plugin: Example background job scheduling</title>
[% INCLUDE 'doc-head-close.inc' %]
</head>
<body>
[% INCLUDE 'header.inc' %]
[% INCLUDE 'cat-search.inc' %]

<div id="breadcrumbs"><a href="/cgi-bin/koha/mainpage.pl">Home</a> &rsaquo; <a href="/cgi-bin/koha/plugins/plugins-home.pl">Plugins</a> &rsaquo; Kitchen Sink &rsaquo; Example Tool</div>

<div id="doc3">

<p>Hello! You scheduled [% count | html %] greetings!</p>

[% INCLUDE 'intranet-bottom.inc' %]
12 changes: 12 additions & 0 deletions Koha/Plugin/Com/ByWaterSolutions/KitchenSink/tool-step1.tt
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,16 @@
<input name="submitted" type="submit" value="Click me" />
</form>

<br/>

<form method="post">
<input type="hidden" name="class" value="[% CLASS %]"/>
<input type="hidden" name="method" value="[% METHOD %]"/>

<br/>
<input type="number" name="count" value="6" />

<input name="greet" type="submit" value="Schedule greetings" />
</form>

[% INCLUDE 'intranet-bottom.inc' %]

0 comments on commit 30d1446

Please sign in to comment.