-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindexer
executable file
·400 lines (296 loc) · 7.98 KB
/
indexer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#!/usr/bin/perl -w
use FindBin;
use lib "$FindBin::Bin/../perl_lib";
######################################################################
#
#
######################################################################
=pod
=for Pod2Wiki
=head1 NAME
B<indexer> - Indexing daemon for EPrints
=head1 SYNOPSIS
B<indexer> start [B<options>]
B<indexer> stop
B<indexer> status
B<indexer> install
B<indexer> uninstall
B<indexer> debug
B<indexer> --help
=head1 DESCRIPTION
This daemon runs in the background and creates index files for all eprints repositories.
Messages and errors are logged to /opt/eprints3/var/indexer.log unless you change the log options. If it appears to be having problems try raising the log level and examining the log.
Once every 24 hours, the indexer rolls the logs (up to logfile.5) and then starts again. See --rollcount for ways to customise this.
=over 8
=back
=head1 OPTIONS
=over 8
=item B<--help>
Print a brief help message and exit.
=item B<--man>
Print the full manual page and then exit.
=item B<--quiet>
Be vewwy vewwy quiet. This option will suppress all output unless an error occurs.
=item B<--force>
Start up, even if the PID file exists (implying another copy is running). This
is useful for starting after a crash, but be carefully not to run to copies at
once as BAD THINGS will happen.
=item B<--verbose>
Explain in detail what is going on.
May be repeated for greater effect.
=item B<--clear>
Clear broken event queue items (items that are "inprogress" or "failed") before commencing.
=item B<--retry>
Retry broken event queue items (items that are "inprogress" or "failed") before commencing.
=item B<--logfile> I<filename>
Log to I<filename> rather than default indexer log.
=item B<--loglevel> I<level>
Set the level of detail to log. Level may be 0-6.
=item B<--rollcount> I<number>
Set the number of once-through logs that should be kept. If set to zero then indexer will never roll the logs but rather just keep writing to the main log.
=item B<--respawn> I<seconds>
Respawn the indexer every I<seconds> (rolls the log files).
=item B<--notdaemon>
Do not become a daemon, remain attached to the current terminal.
Log goes to STDERR instead of the log file.
Does not create a .pid file.
=item B<--once>
Only clear the current queue of things needing indexing then exit.
=item B<--version>
Output version information and exit.
=back
=head1 Making into a service
This has only been tested under redhat linux. It make work on other OS's, but not promise.
To make the indexer into a service which starts and stops on reboots etc. like httpd and mysqld do the following (as root):
ln -s /opt/eprints3/bin/epindexer /etc/init.d/epindexer
chkconfig --add epindexer
chkconfig epindexer on
The epindexer script runs as root, changes user to "eprints" (or whatever uid your eprints install runs as) and then calls indexer.
=cut
use EPrints;
use POSIX;
use strict;
use Getopt::Long;
use Pod::Usage;
my $version = 0;
my $verbose = 0;
my $quiet = 0;
my $help = 0;
my $man = 0;
my $force = 0;
my $logfile;
my $loglevel = 2;
my $rollcount = 5;
my $notdaemon = 0;
my $once = 0;
my $opt_clear;
my $opt_retry;
my $opt_respawn = 86400;
Getopt::Long::Configure("permute");
GetOptions(
'help|?' => \$help,
'man' => \$man,
'version' => \$version,
'verbose+' => \$verbose,
'silent' => \$quiet,
'force' => \$force,
'quiet' => \$quiet,
'notdaemon' => \$notdaemon,
'once' => \$once,
'rollcount=s' => \$rollcount,
'logfile=s' => \$logfile,
'loglevel=s' => \$loglevel,
'clear' => \$opt_clear,
'retry' => \$opt_retry,
'respawn=s' => \$opt_respawn,
) || pod2usage( 2 );
EPrints::Utils::cmd_version( "indexer" ) if $version;
pod2usage( 1 ) if $help;
pod2usage( -exitstatus => 0, -verbose => 2 ) if $man;
pod2usage( 2 ) if( scalar @ARGV != 1 );
our $noise = 1;
$noise = 0 if( $quiet );
$noise = 1+$verbose if( $verbose );
my $p = {
loglevel => $loglevel+0,
rollcount => $rollcount+0,
respawn => $opt_respawn+0,
daemon => !$notdaemon,
noise => $noise,
once => $once,
logfile => EPrints::Index::logfile(),
pidfile => EPrints::Index::pidfile(),
tickfile => EPrints::Index::tickfile(),
};
if( $ARGV[0] eq "debug" )
{
$once = 1;
$notdaemon = 1;
$loglevel = 5;
$ARGV[0] = "start";
}
$logfile ||= EPrints::Index::logfile();
$logfile = undef if $logfile eq "-" or $notdaemon;
my $daemon = EPrints::Index::Daemon->new(
loglevel => $loglevel+0,
respawn => $opt_respawn+0,
logfile => $logfile,
noise => $noise,
once => $once,
);
if( $ARGV[0] eq "status" )
{
status();
}
elsif( $ARGV[0] eq "start" )
{
if( $force )
{
$daemon->remove_pid;
}
if( $opt_clear || $opt_retry )
{
my $eprints = EPrints->new;
print "Processing stale events\n" if $verbose;
foreach my $repoid ($eprints->repository_ids())
{
my $repo = $eprints->repository( $repoid );
my $result = $repo->dataset( "event_queue" )->search(
filters => [
{ meta_fields => [qw( status )], value => "inprogress failed", match => "IN", merge => "ANY" },
]);
$result->map(sub {
my( undef, undef, $event ) = @_;
if( $opt_clear )
{
$event->remove();
}
else
{
$event->set_value( "status", "waiting" );
$event->commit;
}
});
print "".($opt_clear ? "Removed " : "Retrying ").$result->count()." stale events\n" if $verbose;
}
}
start();
}
elsif( $ARGV[0] eq "stop" )
{
stop();
}
elsif( $ARGV[0] eq "install" && $^O eq 'MSWin32' )
{
install();
}
elsif( $ARGV[0] eq "uninstall" && $^O eq 'MSWin32' )
{
uninstall();
}
elsif( $daemon->can( "run_".$ARGV[0] ) )
{
my $f = "run_".shift(@ARGV);
$daemon->$f( @ARGV );
}
else
{
pod2usage( 2 );
}
sub status
{
if( !$daemon->is_running )
{
print "Indexer is not running\n";
}
elsif( $daemon->has_stalled() )
{
print "Indexer appears to have stalled. It may need restarting.\n";
}
else
{
my $last_tick = $daemon->get_last_tick();
my $pid = $daemon->get_pid;
my $interval = $daemon->get_interval;
if( $last_tick > $interval )
{
print "Indexer is running with PID $pid but the next index is overdue by ".($last_tick-$interval)." seconds.\n";
}
else
{
print "Indexer is running with PID $pid. Next index in ".($interval-$last_tick)." seconds.\n";
}
}
}
sub stop
{
if( !$daemon->is_running )
{
exit 1;
}
if( !$daemon->stop_daemon )
{
my $pid = $daemon->get_pid();
print STDERR <<END;
The indexer process with process ID $pid failed to stop. You may
need to stop it (and any child processes) by hand.
END
exit 1;
}
exit 0;
}
sub start
{
if( $daemon->is_running )
{
my $pid = $daemon->get_pid();
my $pidfile = $daemon->get_pidfile();
print <<END;
EPrints indexer appears to be running with process ID $pid.
It may have crashed.
To check if the process is still running (on a linux system)
use:
ps auwx | grep indexer
Options to "ps" vary on other systems. You may also try:
ps -ef | grep indexer
If indexer is not already running you may either:
* delete the PID file: $pidfile
* run indexer with the --force option
END
exit 1;
}
elsif( $notdaemon )
{
$daemon->run_index();
}
else
{
$daemon->start_daemon();
}
}
sub install
{
$daemon->create_service();
}
sub uninstall
{
$daemon->delete_service();
}
####################################################################################
=head1 COPYRIGHT
=for COPYRIGHT BEGIN
Copyright 2000-2011 University of Southampton.
=for COPYRIGHT END
=for LICENSE BEGIN
This file is part of EPrints L<http://www.eprints.org/>.
EPrints is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
EPrints is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.
You should have received a copy of the GNU General Public License
along with EPrints. If not, see L<http://www.gnu.org/licenses/>.
=for LICENSE END