Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
|
6 |
use IPC::Open3; |
7 |
use FindBin; |
8 |
|
9 |
while (1){ |
10 |
run(); |
11 |
sleep 5; |
12 |
} |
13 |
|
14 |
sub run { |
15 |
if ( _zebra_needs_indexing() ){ |
16 |
_run_zebra(); |
17 |
} |
18 |
} |
19 |
|
20 |
#NOTE: This could also be done using a separate script but this is more self-contained |
21 |
sub _zebra_needs_indexing { |
22 |
my $pid; |
23 |
if ( $pid = fork ){ |
24 |
waitpid( $pid, 0 ); |
25 |
my $child_exit_status = $? >> 8; |
26 |
if ($child_exit_status == 0){ |
27 |
return 1; |
28 |
} |
29 |
else { |
30 |
return; |
31 |
} |
32 |
} |
33 |
elsif ( ! defined $pid ){ |
34 |
warn "Failed to fork!"; |
35 |
exit 1; |
36 |
} |
37 |
elsif ( $pid == 0 ){ |
38 |
my $count = _get_count(); |
39 |
if ( $count && $count > 0 ){ |
40 |
exit 0; |
41 |
} |
42 |
else { |
43 |
exit 1; |
44 |
} |
45 |
} |
46 |
} |
47 |
|
48 |
sub _run_zebra { |
49 |
my $zebra_indexer = "$FindBin::Bin/migration_tools/rebuild_zebra.pl"; |
50 |
my @command = ($zebra_indexer,"-b","-a","-z"); |
51 |
|
52 |
my $pid = open3(my $child_in, '>&STDOUT', '>&STDERR', @command); |
53 |
waitpid($pid,0); |
54 |
} |
55 |
|
56 |
sub _get_count { |
57 |
my $count; |
58 |
my $dbh = _get_dbh(); |
59 |
if ($dbh){ |
60 |
my $sql = "SELECT COUNT(*) AS count FROM zebraqueue WHERE done = 0;"; |
61 |
my $sth = $dbh->prepare($sql); |
62 |
if ($sth){ |
63 |
$sth->execute(); |
64 |
$count = $sth->fetchrow_arrayref->[0]; |
65 |
} |
66 |
} |
67 |
return $count; |
68 |
} |
69 |
|
70 |
sub _get_dbh { |
71 |
my $conf = _get_db_config($ENV{KOHA_CONF}); |
72 |
my $dsn = "dbi:$conf->{db_driver}:database=$conf->{db_name};host=$conf->{db_host};port=$conf->{db_port}".($conf->{tls_options}? $conf->{tls_options} : ""); |
73 |
require DBI; |
74 |
my $dbh = DBI->connect("$dsn","$conf->{user}","$conf->{pass}"); |
75 |
return $dbh; |
76 |
} |
77 |
|
78 |
sub _get_db_config { |
79 |
my ($kohaconf_filename) = @_; |
80 |
my $db_config = {}; |
81 |
require XML::LibXML; |
82 |
my $doc = XML::LibXML->load_xml(location => $kohaconf_filename); |
83 |
my $root = $doc->documentElement; |
84 |
my @nodelist = $root->findnodes("./config"); |
85 |
my $config = $nodelist[0]; |
86 |
$db_config->{db_driver} = _get_value($config,"db_scheme"); |
87 |
$db_config->{db_name} = _get_value($config,"database"); |
88 |
$db_config->{db_host} = _get_value($config,"hostname"); |
89 |
$db_config->{db_port} = _get_value($config,"port") || ''; |
90 |
$db_config->{user} = _get_value($config,"user"); |
91 |
$db_config->{pass} = _get_value($config,"pass"); |
92 |
my $tls = _get_value($config,"tls"); |
93 |
if ($tls && $tls eq 'yes'){ |
94 |
my $ca = _get_value($config,"ca"); |
95 |
my $cert = _get_value($config,"cert"); |
96 |
my $key = _get_value($config,"key"); |
97 |
$db_config->{tls_options} = ";mysql_ssl=1;mysql_ssl_client_key=".$key.";mysql_ssl_client_cert=".$cert.";mysql_ssl_ca_file=".$ca; |
98 |
} |
99 |
return $db_config; |
100 |
} |
101 |
|
102 |
sub _get_value { |
103 |
my ($parent,$tag) = @_; |
104 |
my $node = $parent->findnodes("./$tag"); |
105 |
if ($node){ |
106 |
return $node->to_literal(); |
107 |
} |
108 |
} |