From dc20949f6598b6f2404cb5d66bc9dbf134170e0a Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 27 Feb 2019 10:34:42 -0300 Subject: [PATCH] Bug 22417: Add the ability to cancel a job Signed-off-by: Tomas Cohen Arazi Signed-off-by: David Cook Signed-off-by: Marcel de Rooy Signed-off-by: Kyle M Hall Signed-off-by: Jonathan Druart --- Koha/BackgroundJob.pm | 6 ++++++ Koha/BackgroundJob/BatchUpdateBiblio.pm | 19 ++++++++++++------- admin/background_jobs.pl | 8 ++++++++ .../prog/en/modules/admin/background_jobs.tt | 11 ++++++++--- 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/Koha/BackgroundJob.pm b/Koha/BackgroundJob.pm index 6eab98c6b5..bc22b19390 100644 --- a/Koha/BackgroundJob.pm +++ b/Koha/BackgroundJob.pm @@ -3,6 +3,8 @@ package Koha::BackgroundJob; use Modern::Perl; use JSON qw( encode_json decode_json ); use Carp qw( croak ); +use Net::RabbitFoot; + use C4::Context; use Koha::DateUtils qw( dt_from_string ); use Koha::BackgroundJobs; @@ -81,6 +83,10 @@ sub report { return $data_dump->{report}; } +sub cancel { + my ( $self ) = @_; + $self->status('cancelled')->store; +} sub _type { return 'BackgroundJob'; diff --git a/Koha/BackgroundJob/BatchUpdateBiblio.pm b/Koha/BackgroundJob/BatchUpdateBiblio.pm index 4e205bd187..990b34902a 100644 --- a/Koha/BackgroundJob/BatchUpdateBiblio.pm +++ b/Koha/BackgroundJob/BatchUpdateBiblio.pm @@ -14,10 +14,13 @@ sub process { my $job_type = $args->{job_type}; - return unless exists $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; $job->started_on(dt_from_string) ->progress($job_progress) @@ -29,14 +32,16 @@ sub process { my @record_ids = @{ $args->{record_ids} }; my $report = { - total_records => 0, + total_records => scalar @record_ids, total_success => 0, }; my @messages; my $dbh = C4::Context->dbh; $dbh->{RaiseError} = 1; 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; # Modify the biblio @@ -69,9 +74,9 @@ sub process { $job_data->{report} = $report; $job->ended_on(dt_from_string) - ->status('finished') - ->data(encode_json $job_data) - ->store; + ->data(encode_json $job_data); + $job->status('finished') if $job->status ne 'cancelled'; + $job->store; $channel->ack(); # FIXME Is that ok even on failure? } diff --git a/admin/background_jobs.pl b/admin/background_jobs.pl index 2b4ca03dbf..498891c80a 100755 --- a/admin/background_jobs.pl +++ b/admin/background_jobs.pl @@ -51,9 +51,17 @@ if ( $op eq 'view' ) { } else { $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' ) { my $jobs = Koha::BackgroundJobs->search({}, { order_by => { -desc => 'enqueued_on' }}); $template->param( jobs => $jobs, ); diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/background_jobs.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/background_jobs.tt index 098b7a3239..8009711e92 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/background_jobs.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/background_jobs.tt @@ -52,7 +52,9 @@ [% ELSE %]
- [% report.total_success | html %] / [% report.total_records | html %] records have successfully been modified. Some errors occurred. New batch record modification + [% 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 %] + New batch record modification
[% END %] [% END %] @@ -137,8 +139,11 @@ [% job.started_on| html %] [% job.ended_on| html %] - Delete - Replay + View + [% IF job.status == 'new' || job.status == 'started' %] + Cancel + [% END %] + Replay [% END %] -- 2.39.5