From b546b99162d8f58b4c389bab6a8ef54abe301793 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 Content-Type: text/plain; charset=utf-8 Signed-off-by: Tomas Cohen Arazi Signed-off-by: David Cook Signed-off-by: Marcel de Rooy --- Koha/BackgroundJob.pm | 38 +++++++++++++--------- .../en/modules/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 5aa2cd67a8..caf4d300f5 100644 --- a/Koha/BackgroundJob.pm +++ b/Koha/BackgroundJob.pm @@ -29,21 +29,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.11.0