- my ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored) = BatchRevertBibRecords($import_batch_id);
- $template->param(did_revert => 1);
- $template->param(num_deleted => $num_deleted);
- $template->param(num_items_deleted => $num_items_deleted);
- $template->param(num_errors => $num_errors);
- $template->param(num_reverted => $num_reverted);
- $template->param(num_ignored => $num_ignored);
+
+ $dbh->{AutoCommit} = 0;
+ my $job = undef;
+ my $callback = sub {};
+ if ($runinbackground) {
+ $job = put_in_background($import_batch_id);
+ $callback = progress_callback($job, $dbh);
+ }
+ my ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored) =
+ BatchRevertBibRecords($import_batch_id, 50, $callback);
+ $dbh->commit();
+
+ my $results = {
+ did_revert => 1,
+ num_deleted => $num_deleted,
+ num_items_deleted => $num_items_deleted,
+ num_errors => $num_errors,
+ num_reverted => $num_reverted,
+ num_ignored => $num_ignored,
+ };
+ if ($runinbackground) {
+ $job->finish($results);
+ } else {
+ add_results_to_template($template, $results);
+ }
+}
+
+sub put_in_background {
+ my $import_batch_id = shift;
+
+ my $batch = GetImportBatch($import_batch_id);
+ my $job = C4::BackgroundJob->new($sessionID, $batch->{'file_name'}, $ENV{'SCRIPT_NAME'}, $batch->{'num_biblios'});
+ 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 $ENV{'SCRIPT_NAME'} as a background job";
+ exit 0;
+ }
+ return $job;
+}
+
+sub progress_callback {
+ my $job = shift;
+ my $dbh = shift;
+ return sub {
+ my $progress = shift;
+ $job->progress($progress);
+ $dbh->commit();
+ }
+}
+
+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 $job = C4::BackgroundJob->fetch($sessionID, $completedJobID);
+ my $results = $job->results();
+ add_results_to_template($template, $results);