]> git.koha-community.org Git - koha.git/blob - Koha/Indexer/Daemon.pm
Bug 15471 [QA Followup] - Revert use of raw method which is no longer used do to...
[koha.git] / Koha / Indexer / Daemon.pm
1 # This file is part of Koha.
2 #
3 # Copyright (C) 2013 Tamil s.a.r.l.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 package Koha::Indexer::Daemon;
19
20 use Moose;
21
22 use Modern::Perl;
23 use utf8;
24 use AnyEvent;
25 use Koha::Indexer::Indexing;
26 use C4::Context;
27
28 with 'MooseX::Getopt';
29
30
31 has name => ( is => 'rw', isa => 'Str' );
32
33 has directory => ( is => 'rw', isa => 'Str' );
34
35 has timeout => (
36     is      => 'rw',
37     isa     => 'Int',
38     default => 60,
39 );
40
41 has verbose => ( is => 'rw', isa => 'Bool', default => 0 );
42
43
44 sub BUILD {
45     my $self = shift;
46
47     say "Starting Koha Indexer Daemon";
48
49     $self->name( C4::Context->config('database') );
50
51     my $idle = AnyEvent->timer(
52         after    => $self->timeout,
53         interval => $self->timeout,
54         cb       => sub { $self->index_zebraqueue(); }
55     );
56     AnyEvent->condvar->recv;
57 }
58
59
60 sub index_zebraqueue {
61     my $self = shift;
62
63     my $dbh = C4::Context->dbh();
64     my $sql = " SELECT COUNT(*), server
65                 FROM zebraqueue
66                 WHERE done = 0
67                 GROUP BY server ";
68     my $sth = $dbh->prepare($sql);
69     $sth->execute();
70     my %count = ( biblio => 0, authority => 0 );
71     while ( my ($count, $server) = $sth->fetchrow ) {
72         $server =~ s/server//g;
73         $count{$server} = $count;
74     }
75
76     say "[", $self->name, "] Index biblio (", $count{biblio}, ") authority (",
77         $count{authority}, ")";
78
79     for my $source (qw/biblio authority/) {
80         next unless $count{$source};
81         my $indexer = Koha::Indexer::Indexing->new(
82             source      => $source,
83             select      => 'queue',
84             blocking    => 1,
85             keep        => 1,
86             verbose     => $self->verbose,
87         );
88         $indexer->directory($self->directory) if $self->directory;
89         $indexer->run();
90     }
91 }
92
93 no Moose;
94 __PACKAGE__->meta->make_immutable;
95 1;
96
97 __END__
98 =pod
99
100 =head1 SYNOPSIS
101
102  # Index Koha queued biblio/authority records every minute.
103  # KOHA_CONF environment variable is used to find which Koha
104  # instance to use.
105  # Records are exported from Koha DB into files located in
106  # the current directory
107  my $daemon = Koha::Indexer::Daemon->new();
108
109  my $daemon = Koha::Indexer::Daemon->new(
110     timeout   => 20,
111     directory => '/home/koha/mylib/tmp',
112     verbose   => 1 );
113
114 =head1 Attributes
115
116 =over
117
118 =item directory($directory_name)
119
120 Location of the directory where to export biblio/authority records before
121 sending them to Zebra indexer.
122
123 =item timeout($seconds)
124
125 Number of seconds between indexing.
126
127 =item verbose(0|1)
128
129 Task verbosity.
130
131 =back
132
133 =cut