From a10fb3c75d071149e4672e26e9135e7cd7ab17d4 Mon Sep 17 00:00:00 2001 From: Fridolin Somers Date: Wed, 9 Jun 2021 09:24:54 +0200 Subject: [PATCH] Revert "Bug 28158: Remove backgroundjob from batchMod" This reverts commit ade7914e5927ecb21aa4e8abb80aad894d597765. --- .../prog/en/modules/tools/batchMod-edit.tt | 2 +- tools/batchMod.pl | 111 ++++++++++++++++-- 2 files changed, 103 insertions(+), 10 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batchMod-edit.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batchMod-edit.tt index a9188ed437..0ed9ed2ab4 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batchMod-edit.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batchMod-edit.tt @@ -36,7 +36,7 @@ $(document).ready(function(){ } }); $("#mainformsubmit").on("click",function(){ - //return submitBackgroundJob(this.form); + return submitBackgroundJob(this.form); }); $('a[name="field_regex"]').click(function() { var id = $(this).attr('id'); diff --git a/tools/batchMod.pl b/tools/batchMod.pl index b5dba8aa8f..53181f5b6e 100755 --- a/tools/batchMod.pl +++ b/tools/batchMod.pl @@ -55,6 +55,7 @@ my $op = $input->param('op'); my $del = $input->param('del'); my $del_records = $input->param('del_records'); my $completedJobID = $input->param('completedJobID'); +my $runinbackground = $input->param('runinbackground'); my $src = $input->param('src'); my $use_default_values = $input->param('use_default_values'); my $exclude_from_local_holds_priority = $input->param('exclude_from_local_holds_priority'); @@ -149,9 +150,24 @@ if ($op eq "action") { $template->param( "job_completed" => 1 ); } + # Setting the job as done + my $job = C4::BackgroundJob->fetch($sessionID, $completedJobID); + + # Calling the template + add_saved_job_results_to_template($template, $completedJobID, $items_display_hashref); + } else { # While the job is getting done + # Job size is the number of items we have to process + my $job_size = scalar(@itemnumbers); + my $job = undef; + + # If we asked for background processing + if ($runinbackground) { + $job = put_in_background($job_size); + } + #initializing values for updates my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField( "items.itemnumber" ); if ($values_to_modify){ @@ -187,6 +203,7 @@ if ($op eq "action") { my $i = 1; my $extra_headers = {}; foreach my $itemnumber (@itemnumbers) { + $job->progress($i) if $runinbackground; my $item = Koha::Items->find($itemnumber); next unless $item @@ -233,6 +250,8 @@ if ($op eq "action") { $item->exclude_from_local_holds_priority($exclude_from_local_holds_priority)->store; $modified_holds_priority = 1; } + $extra_headers->{exclude_from_local_holds_priority} = {name => 'Exclude from local holds priority', items => {}} unless defined $extra_headers->{exclude_from_local_holds_priority}; + $extra_headers->{exclude_from_local_holds_priority}->{items}->{$item->itemnumber} = $ynhash->{'av'.$item->exclude_from_local_holds_priority}; } my $modified = 0; if ( $values_to_modify || $values_to_blank ) { @@ -301,8 +320,17 @@ if ($op eq "action") { push @$upd_biblionumbers, $itemdata->{'biblionumber'}; } } - $modified_items++ if $modified || $modified_holds_priority; - $modified_fields += $modified + $modified_holds_priority; + if ($runinbackground) { + $modified_items++ if $modified || $modified_holds_priority; + $modified_fields += $modified + $modified_holds_priority; + $job->set( + { + modified_items => $modified_items, + modified_fields => $modified_fields, + extra_headers => $extra_headers, + } + ); + } } $i++; } @@ -330,13 +358,6 @@ if ($op eq "action") { $indexer->index_records( $upd_biblionumbers, 'specialUpdate', "biblioserver", undef ) if @$upd_biblionumbers; $indexer->index_records( $del_biblionumbers, 'recordDelete', "biblioserver", undef ) if @$del_biblionumbers; } - - # Calling the template - $template->param( - modified_items => $modified_items, - modified_fields => $modified_fields, - ); - } # #------------------------------------------------------------------------------- @@ -768,3 +789,75 @@ sub find_value { } return($indicator,$result); } + +# ---------------------------- +# Background functions + + +sub add_results_to_template { + my $template = shift; + my $results = shift; + $template->param(map { $_ => $results->{$_} } keys %{ $results }); +} + +sub add_saved_job_results_to_template { + my $template = shift; + my $completedJobID = shift; + my $items_display_hashref= shift; + my $job = C4::BackgroundJob->fetch($sessionID, $completedJobID); + my $results = $job->results(); + add_results_to_template($template, $results); + + my $fields = $job->get("modified_fields"); + my $items = $job->get("modified_items"); + my $extra_headers = $job->get("extra_headers"); + + foreach my $header (keys %{$extra_headers}) { + push @{$items_display_hashref->{item_header_loop}}, {header_value => $extra_headers->{$header}->{name}}; + foreach my $row (@{$items_display_hashref->{item_loop}}) { + push @{$row->{item_value}}, {field => $extra_headers->{$header}->{items}->{$row->{itemnumber}}}; + } + } + + $template->param( + modified_items => $items, + modified_fields => $fields, + ); +} + +sub put_in_background { + my $job_size = shift; + + my $job = C4::BackgroundJob->new($sessionID, "test", '/cgi-bin/koha/tools/batchMod.pl', $job_size); + my $jobID = $job->id(); + + # fork off + if (my $pid = fork) { + # parent + # return job ID as JSON + + # prevent parent exiting from + # destroying the kid's database handle + # FIXME: according to DBI doc, this may not work for Oracle + $dbh->{InactiveDestroy} = 1; + + my $reply = CGI->new(""); + print $reply->header(-type => 'text/html'); + print '{"jobID":"' . $jobID . '"}'; + exit 0; + } elsif (defined $pid) { + # child + # close STDOUT to signal to Apache that + # we're now running in the background + close STDOUT; + close STDERR; + } else { + # fork failed, so exit immediately + warn "fork failed while attempting to run tools/batchMod.pl as a background job"; + exit 0; + } + return $job; +} + + + -- 2.39.5