Bug 17376 - rebuild_zebra.pl in daemon mode no database access kills the process
[koha-ffzg.git] / misc / migration_tools / rebuild_zebra.pl
index fb06aab..b32f8ba 100755 (executable)
@@ -99,7 +99,8 @@ if (not $result or $want_help) {
 }
 
 if ( $as_xml ) {
-    warn "Warning: You passed -x which is already the default and is now deprecated·\n";
+    warn "Warning: You passed -x which is already the default and is now deprecated\n";
+    undef $as_xml; # Should not be used later
 }
 
 if( not defined $run_as_root and $run_user eq 'root') {
@@ -110,7 +111,7 @@ if( not defined $run_as_root and $run_user eq 'root') {
 }
 
 if ( $as_usmarc and $nosanitize ) {
-    my $msg = "Cannot specify both -no_xml and -nosanitize\n";
+    my $msg = "Cannot specify both -noxml and -nosanitize\n";
     $msg   .= "Please do '$0 --help' to see usage.\n";
     die $msg;
 }
@@ -127,14 +128,6 @@ if ($process_zebraqueue and $do_not_clear_zebraqueue) {
     die $msg;
 }
 
-if ($reset) {
-    $noshadow = 1;
-}
-
-if ($noshadow) {
-    $noshadow = ' -n ';
-}
-
 if ($daemon_mode) {
     # incompatible flags handled above: help, reset, and do_not_clear_zebraqueue
     if ($skip_export or $keep_export or $skip_index or
@@ -182,7 +175,6 @@ my $kohadir = C4::Context->config('intranetdir');
 my $bib_index_mode  = C4::Context->config('zebra_bib_index_mode')  // 'dom';
 my $auth_index_mode = C4::Context->config('zebra_auth_index_mode') // 'dom';
 
-my $dbh = C4::Context->dbh;
 my ($biblionumbertagfield,$biblionumbertagsubfield) = &GetMarcFromKohaField("biblio.biblionumber","");
 my ($biblioitemnumbertagfield,$biblioitemnumbertagsubfield) = &GetMarcFromKohaField("biblioitems.biblioitemnumber","");
 
@@ -234,6 +226,7 @@ if ( $verbose_logging ) {
 }
 
 my $tester = XML::LibXML->new();
+my $dbh;
 
 # The main work is done here by calling do_one_pass().  We have added locking
 # avoid race conditions between full rebuilds and incremental updates either from
@@ -250,7 +243,13 @@ if ($daemon_mode) {
     while (1) {
         # For incremental updates, skip the update if the updates are locked
         if (_flock($LockFH, LOCK_EX|LOCK_NB)) {
-            do_one_pass() if ( zebraqueue_not_empty() );
+            eval {
+                $dbh = C4::Context->dbh;
+                do_one_pass() if ( zebraqueue_not_empty() );
+            };
+            if ($@ && $verbose_logging) {
+                warn "Warning : $@\n";
+            }
             _flock($LockFH, LOCK_UN);
         }
         sleep $daemon_sleep;
@@ -259,6 +258,7 @@ if ($daemon_mode) {
     # all one-off invocations
     my $lock_mode = ($wait_for_lock) ? LOCK_EX : LOCK_EX|LOCK_NB;
     if (_flock($LockFH, $lock_mode)) {
+        $dbh = C4::Context->dbh;
         do_one_pass();
         _flock($LockFH, LOCK_UN);
     } else {
@@ -790,7 +790,8 @@ sub fix_unimarc_100 {
     my $marc = shift;
 
     my $string;
-    if ( length($marc->subfield( 100, "a" )) == 36 ) {
+    my $length_100a = length($marc->subfield( 100, "a" ));
+    if (  $length_100a and $length_100a == 36 ) {
         $string = $marc->subfield( 100, "a" );
         my $f100 = $marc->field(100);
         $marc->delete_field($f100);
@@ -801,7 +802,8 @@ sub fix_unimarc_100 {
         $string = sprintf( "%-*s", 35, $string );
     }
     substr( $string, 22, 6, "frey50" );
-    unless ( length($marc->subfield( 100, "a" )) == 36 ) {
+    $length_100a = length($marc->subfield( 100, "a" ));
+    unless ( $length_100a and $length_100a == 36 ) {
         $marc->delete_field($marc->field(100));
         $marc->insert_grouped_field(MARC::Field->new( 100, "", "", "a" => $string ));
     }
@@ -815,10 +817,15 @@ sub do_indexing {
     my $zebra_config  = C4::Context->zebraconfig($zebra_server)->{'config'};
     my $zebra_db_dir  = C4::Context->zebraconfig($zebra_server)->{'directory'};
 
+    $noshadow //= '';
+
+    if ($noshadow or $reset_index) {
+        $noshadow = '-n';
+    }
+
     system("zebraidx -c $zebra_config $zebraidx_log_opt -g $record_format -d $zebra_db_name init") if $reset_index;
     system("zebraidx -c $zebra_config $zebraidx_log_opt $noshadow -g $record_format -d $zebra_db_name $op $record_dir");
     system("zebraidx -c $zebra_config $zebraidx_log_opt -g $record_format -d $zebra_db_name commit") unless $noshadow;
-
 }
 
 sub _flock {