Bug 22417: Add the ability to cancel a job

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: David Cook <dcook@prosentient.com.au>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
This commit is contained in:
Jonathan Druart 2019-02-27 10:34:42 -03:00
parent 63cf7a0307
commit dc20949f65
4 changed files with 35 additions and 11 deletions

View file

@ -3,6 +3,8 @@ package Koha::BackgroundJob;
use Modern::Perl; use Modern::Perl;
use JSON qw( encode_json decode_json ); use JSON qw( encode_json decode_json );
use Carp qw( croak ); use Carp qw( croak );
use Net::RabbitFoot;
use C4::Context; use C4::Context;
use Koha::DateUtils qw( dt_from_string ); use Koha::DateUtils qw( dt_from_string );
use Koha::BackgroundJobs; use Koha::BackgroundJobs;
@ -81,6 +83,10 @@ sub report {
return $data_dump->{report}; return $data_dump->{report};
} }
sub cancel {
my ( $self ) = @_;
$self->status('cancelled')->store;
}
sub _type { sub _type {
return 'BackgroundJob'; return 'BackgroundJob';

View file

@ -14,10 +14,13 @@ sub process {
my $job_type = $args->{job_type}; my $job_type = $args->{job_type};
return unless exists $args->{job_id};
my $job = Koha::BackgroundJobs->find( $args->{job_id} ); my $job = Koha::BackgroundJobs->find( $args->{job_id} );
if ( !exists $args->{job_id} || !$job || $job->status eq 'cancelled' ) {
$channel->ack;
return;
}
my $job_progress = 0; my $job_progress = 0;
$job->started_on(dt_from_string) $job->started_on(dt_from_string)
->progress($job_progress) ->progress($job_progress)
@ -29,14 +32,16 @@ sub process {
my @record_ids = @{ $args->{record_ids} }; my @record_ids = @{ $args->{record_ids} };
my $report = { my $report = {
total_records => 0, total_records => scalar @record_ids,
total_success => 0, total_success => 0,
}; };
my @messages; my @messages;
my $dbh = C4::Context->dbh; my $dbh = C4::Context->dbh;
$dbh->{RaiseError} = 1; $dbh->{RaiseError} = 1;
RECORD_IDS: for my $biblionumber ( sort { $a <=> $b } @record_ids ) { RECORD_IDS: for my $biblionumber ( sort { $a <=> $b } @record_ids ) {
$report->{total_records}++;
last if $job->get_from_storage->status eq 'cancelled';
next unless $biblionumber; next unless $biblionumber;
# Modify the biblio # Modify the biblio
@ -69,9 +74,9 @@ sub process {
$job_data->{report} = $report; $job_data->{report} = $report;
$job->ended_on(dt_from_string) $job->ended_on(dt_from_string)
->status('finished') ->data(encode_json $job_data);
->data(encode_json $job_data) $job->status('finished') if $job->status ne 'cancelled';
->store; $job->store;
$channel->ack(); # FIXME Is that ok even on failure? $channel->ack(); # FIXME Is that ok even on failure?
} }

View file

@ -51,9 +51,17 @@ if ( $op eq 'view' ) {
} else { } else {
$op = 'list'; $op = 'list';
} }
} }
if ( $op eq 'cancel' ) {
my $id = $input->param('id');
if ( my $job = Koha::BackgroundJobs->find($id) ) { # FIXME Make sure logged in user can cancel this job
$job->cancel;
}
$op = 'list';
}
if ( $op eq 'list' ) { if ( $op eq 'list' ) {
my $jobs = Koha::BackgroundJobs->search({}, { order_by => { -desc => 'enqueued_on' }}); my $jobs = Koha::BackgroundJobs->search({}, { order_by => { -desc => 'enqueued_on' }});
$template->param( jobs => $jobs, ); $template->param( jobs => $jobs, );

View file

@ -52,7 +52,9 @@
</div> </div>
[% ELSE %] [% ELSE %]
<div class="dialog message"> <div class="dialog message">
[% report.total_success | html %] / [% report.total_records | html %] records have successfully been modified. Some errors occurred. <a href="/cgi-bin/koha/tools/batch_record_modification.pl" title="New batch record modification">New batch record modification</a> [% report.total_success | html %] / [% report.total_records | html %] records have successfully been modified. Some errors occurred.
[% IF job.status == 'cancelled' %]The job has been cancelled before it finished.[% END %]
<a href="/cgi-bin/koha/tools/batch_record_modification.pl" title="New batch record modification">New batch record modification</a>
</div> </div>
[% END %] [% END %]
[% END %] [% END %]
@ -137,7 +139,10 @@
<td>[% job.started_on| html %]</td> <td>[% job.started_on| html %]</td>
<td>[% job.ended_on| html %]</td> <td>[% job.ended_on| html %]</td>
<td class="actions"> <td class="actions">
<a class="btn btn-default btn-xs disabled" href="/cgi-bin/koha/admin/background_jobs.pl?op=delete_confirm&amp;id=[% job.id | html %]"><i class="fa fa-pencil"></i> Delete</a> <a class="btn btn-default btn-xs" href="/cgi-bin/koha/admin/background_jobs.pl?op=view&amp;id=[% job.id | html %]"><i class="fa fa-eye"></i> View</a>
[% IF job.status == 'new' || job.status == 'started' %]
<a class="btn btn-default btn-xs" href="/cgi-bin/koha/admin/background_jobs.pl?op=cancel&amp;id=[% job.id | html %]"><i class="fa fa-pencil"></i> Cancel</a>
[% END %]
<a class="btn btn-default btn-xs disabled" href="/cgi-bin/koha/admin/background_jobs.pl?op=replay&amp;id=[% job.id | html %]"><i class="fa fa-trash"></i> Replay</a> <a class="btn btn-default btn-xs disabled" href="/cgi-bin/koha/admin/background_jobs.pl?op=replay&amp;id=[% job.id | html %]"><i class="fa fa-trash"></i> Replay</a>
</td> </td>
</tr> </tr>