From 7fd48a66804741a22482331582cea21da392dde1 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 20 Feb 2020 12:23:07 +0100 Subject: [PATCH] Bug 22417: Handle errors Signed-off-by: Tomas Cohen Arazi Signed-off-by: David Cook Signed-off-by: Marcel de Rooy --- Koha/BackgroundJob.pm | 38 +++++++++++-------- .../tools/batch_record_modification.tt | 2 + tools/batch_record_modification.pl | 32 ++++++++++------ 3 files changed, 46 insertions(+), 26 deletions(-) diff --git a/Koha/BackgroundJob.pm b/Koha/BackgroundJob.pm index 252091489e..a8d1a219b0 100644 --- a/Koha/BackgroundJob.pm +++ b/Koha/BackgroundJob.pm @@ -27,21 +27,29 @@ sub enqueue { my $borrowernumber = C4::Context->userenv->{number}; # FIXME Handle non GUI calls my $json_args = encode_json $job_args; - $self->set({ - status => 'new', - type => $job_type, - size => $job_size, - data => $json_args, - enqueued_on => dt_from_string, - borrowernumber => $borrowernumber, - })->store; - - my $job_id = $self->id; - $job_args->{job_id} = $job_id; - $json_args = encode_json $job_args, - - my $conn = $self->connect; - $conn->send({destination => $job_type, body => $json_args}); + my $job_id; + $self->_result->result_source->schema->txn_do( + sub { + $self->set( + { + status => 'new', + type => $job_type, + size => $job_size, + data => $json_args, + enqueued_on => dt_from_string, + borrowernumber => $borrowernumber, + } + )->store; + + $job_id = $self->id; + $job_args->{job_id} = $job_id; + $json_args = encode_json $job_args; + + my $conn = $self->connect; + $conn->send_with_receipt( { destination => $job_type, body => $json_args } ) + or Koha::Exception->throw('Job has not been enqueued'); + } + ); return $job_id; } diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batch_record_modification.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batch_record_modification.tt index dc1d04c649..1c09747848 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batch_record_modification.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batch_record_modification.tt @@ -46,6 +46,8 @@ Authority record [% message.authid | html %] has not been modified. An error occurred on modifying it. [% ELSIF message.code == 'authority_modified' %] Bibliographic record [% message.authid | html %] has successfully been modified. + [% ELSIF message.code == 'cannot_enqueue_job' %] + Cannot enqueue this job. [% END %] [% IF message.error %] (The error was: [% message.error | html %]. See the Koha logfile for more information). diff --git a/tools/batch_record_modification.pl b/tools/batch_record_modification.pl index 23d54de053..5b645993ec 100755 --- a/tools/batch_record_modification.pl +++ b/tools/batch_record_modification.pl @@ -23,6 +23,7 @@ use Modern::Perl; use CGI; use List::MoreUtils qw( uniq ); use JSON qw( encode_json ); +use Try::Tiny; use C4::Auth qw( get_template_and_user ); use C4::Output qw( output_html_with_http_headers ); @@ -150,17 +151,26 @@ if ( $op eq 'form' ) { # We want to modify selected records! my @record_ids = $input->multi_param('record_id'); - my $job_id = Koha::BackgroundJob::BatchUpdateBiblio->new->enqueue( - { - mmtid => $mmtid, - record_type => $recordtype, - record_ids => \@record_ids, - } - ); - $template->param( - view => 'enqueued', - job_id => $job_id, - ); + try { + my $job_id = Koha::BackgroundJob::BatchUpdateBiblio->new->enqueue( + { + mmtid => $mmtid, + record_type => $recordtype, + record_ids => \@record_ids, + } + ); + $template->param( + view => 'enqueued', + job_id => $job_id, + ); + } catch { + push @messages, { + type => 'error', + code => 'cannot_enqueue_job', + error => $_, + }; + $template->param( view => 'errors' ); + }; } $template->param( -- 2.20.1