View | Details | Raw Unified | Return to bug 11078
Collapse All | Expand All

(-)a/misc/migration_tools/rebuild_zebra.pl (-12 / +56 lines)
Lines 44-49 my $where; Link Here
44
my $offset;
44
my $offset;
45
my $run_as_root;
45
my $run_as_root;
46
my $run_user = (getpwuid($<))[0];
46
my $run_user = (getpwuid($<))[0];
47
my $wait_for_lock = 0;
48
my $use_flock;
47
49
48
my $verbose_logging = 0;
50
my $verbose_logging = 0;
49
my $zebraidx_log_opt = " -v none,fatal,warn ";
51
my $zebraidx_log_opt = " -v none,fatal,warn ";
Lines 65-75 my $result = GetOptions( Link Here
65
    'x'             => \$as_xml,
67
    'x'             => \$as_xml,
66
    'y'             => \$do_not_clear_zebraqueue,
68
    'y'             => \$do_not_clear_zebraqueue,
67
    'z'             => \$process_zebraqueue,
69
    'z'             => \$process_zebraqueue,
68
    'where:s'        => \$where,
70
    'where:s'       => \$where,
69
    'length:i'        => \$length,
71
    'length:i'      => \$length,
70
    'offset:i'      => \$offset,
72
    'offset:i'      => \$offset,
71
    'v+'             => \$verbose_logging,
73
    'v+'            => \$verbose_logging,
72
    'run-as-root'    => \$run_as_root,
74
    'run-as-root'   => \$run_as_root,
75
    'wait-for-lock' => \$wait_for_lock,
73
);
76
);
74
77
75
if (not $result or $want_help) {
78
if (not $result or $want_help) {
Lines 156-166 my ($biblioitemnumbertagfield,$biblioitemnumbertagsubfield) = &GetMarcFromKohaFi Link Here
156
159
157
# Protect again simultaneous update of the zebra index by using a lock file.
160
# Protect again simultaneous update of the zebra index by using a lock file.
158
# Create our own lock directory if its missing.  This shouild be created
161
# Create our own lock directory if its missing.  This shouild be created
159
# by koha-zebra-ctl.sh.
162
# by koha-zebra-ctl.sh or at system installation.  If the desired directory
163
# does not exist and cannot be created, we fall back on /tmp - which will
164
# always work.
160
165
161
my $lockdir = C4::Context->config("zebra_lockdir") // "/var/lock";
166
my $lockdir = C4::Context->config("zebra_lockdir") // "/var/lock";
162
$lockdir .= "/rebuild";
167
$lockdir .= "/rebuild";
163
mkpath($lockdir, 0, oct(755)) unless (-d $lockdir);
168
unless (-d $lockdir) {
169
    eval { mkpath($lockdir, 0, oct(755)) };
170
    $lockdir = "/tmp" if ($@);
171
}
164
my $lockfile = $lockdir . "/rebuild..LCK";
172
my $lockfile = $lockdir . "/rebuild..LCK";
165
173
166
if ( $verbose_logging ) {
174
if ( $verbose_logging ) {
Lines 187-207 my $tester = XML::LibXML->new(); Link Here
187
# record being overwritten by a rebuild if the update is applied after the export
195
# record being overwritten by a rebuild if the update is applied after the export
188
# by the rebuild and before the rebuild finishes (more likely to effect large
196
# by the rebuild and before the rebuild finishes (more likely to effect large
189
# catalogs).
197
# catalogs).
198
#
199
# We have chosen to exit immediately by default if we cannot obtain the lock
200
# to prevent the potential for a infinite backlog from cron invocations, but an
201
# option (wait-for-lock) is provided to let the program wait for the lock.
202
# See http://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=11078 for details.
190
open my $LockFH, q{>}, $lockfile or die "$lockfile: $!";
203
open my $LockFH, q{>}, $lockfile or die "$lockfile: $!";
191
if ($daemon_mode) {
204
if ($daemon_mode) {
192
    while (1) {
205
    while (1) {
193
        # For incremental updates, skip the update if the updates are locked
206
        # For incremental updates, skip the update if the updates are locked
194
        if (flock($LockFH, LOCK_EX|LOCK_NB)) {
207
        if (_flock($LockFH, LOCK_EX|LOCK_NB)) {
195
            do_one_pass() if ( zebraqueue_not_empty() );
208
            do_one_pass() if ( zebraqueue_not_empty() );
196
            flock($LockFH, LOCK_UN);
209
            _flock($LockFH, LOCK_UN);
197
        }
210
        }
198
        sleep $daemon_sleep;
211
        sleep $daemon_sleep;
199
    }
212
    }
200
} else {
213
} else {
201
    # all one-off invocations, wait for the lock to free
214
    # all one-off invocations
202
    flock($LockFH, LOCK_EX);
215
    my $lock_mode = ($wait_for_lock) ? LOCK_EX : LOCK_EX|LOCK_NB;
203
    do_one_pass();
216
    if (_flock($LockFH, $lock_mode)) {
204
    flock($LockFH, LOCK_UN);
217
        do_one_pass();
218
        _flock($LockFH, LOCK_UN);
219
    } else {
220
        # Can't die() here because we have files to dlean up.
221
        print "Aborting rebuild.  Unable to flock $lockfile: $!\n";
222
    }
205
}
223
}
206
224
207
225
Lines 755-760 sub do_indexing { Link Here
755
773
756
}
774
}
757
775
776
sub _flock {
777
# test if flock is present; if so, use it; if not, return true
778
# op refers to the official flock operations incl LOCK_EX, LOCK_UN, etc.
779
# combining LOCK_EX with LOCK_NB returns immediately
780
    my ($fh, $op)= @_;
781
    if( !defined($use_flock) ) {
782
        #check if flock is present; if not, you will have a fatal error
783
        my $i=eval { flock($fh, $op) };
784
        #assuming that $fh and $op are fine(..), an undef i means no flock
785
        $use_flock= defined($i)? 1: 0;
786
        print "Warning: flock could not be used!\n" if $verbose_logging && !$use_flock;
787
        return 1 if !$use_flock;
788
        return $i;
789
    }
790
    else {
791
        return 1 if !$use_flock;
792
        return flock($fh, $op);
793
    }
794
}
795
758
sub print_usage {
796
sub print_usage {
759
    print <<_USAGE_;
797
    print <<_USAGE_;
760
$0: reindex MARC bibs and/or authorities in Zebra.
798
$0: reindex MARC bibs and/or authorities in Zebra.
Lines 843-848 Parameters: Link Here
843
881
844
    --run-as-root           explicitily allow script to run as 'root' user
882
    --run-as-root           explicitily allow script to run as 'root' user
845
883
884
    --wait-for-lock         when not running in daemon mode, the default
885
                            behavior is to abort a rebuild if the rebuild
886
                            lock is busy.  This option will cause the program
887
                            to wait for the lock to free and then continue
888
                            processing the rebuild request,
889
846
    --help or -h            show this message.
890
    --help or -h            show this message.
847
_USAGE_
891
_USAGE_
848
}
892
}
(-)a/t/db_dependent/zebra_config.pl (-1 / +1 lines)
Lines 28-33 if ($indexing_mode eq 'dom') { Link Here
28
make_path("$destination/var/lock/zebradb");
28
make_path("$destination/var/lock/zebradb");
29
make_path("$destination/var/lock/zebradb/biblios");
29
make_path("$destination/var/lock/zebradb/biblios");
30
make_path("$destination/var/lock/zebradb/authorities");
30
make_path("$destination/var/lock/zebradb/authorities");
31
make_path("$destination/var/lock/zebradb/rebuild");
31
make_path("$destination/var/lib/zebradb");
32
make_path("$destination/var/lib/zebradb");
32
make_path("$destination/var/lib/zebradb/biblios");
33
make_path("$destination/var/lib/zebradb/biblios");
33
make_path("$destination/var/lib/zebradb/biblios/key");
34
make_path("$destination/var/lib/zebradb/biblios/key");
34
- 

Return to bug 11078