-
Notifications
You must be signed in to change notification settings - Fork 1
/
nlp.pl
executable file
·169 lines (136 loc) · 5.12 KB
/
nlp.pl
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
#!/usr/bin/perl
use strict;
use warnings;
use POSIX;
use RExtractor::Tools;
use RExtractor::Document;
use RExtractor::Strategy;
$SIG{INT} = \&_signalHandler;
$SIG{KILL} = \&_signalHandler;
$SIG{TERM} = \&_signalHandler;
_daemonize();
# Open log file
my $LOG = undef;
open($LOG, ">>./servers/logs/nlp.log");
# Terminate if there is another running nlp server
my $pid = RExtractor::Tools::readFile("./servers/pids/nlp.pid");
if ($pid and kill(0, $pid)) {
RExtractor::Tools::error($LOG, "Another NLP server ($pid) is running. Terminating...\n");
exit(1);
}
# Create own PID-file
if (!RExtractor::Tools::writeFile("./servers/pids/nlp.pid", $$)) {
RExtractor::Tools::error($LOG, "Couldn't create a PID file. Terminating...");
exit(1);
}
RExtractor::Tools::info($LOG, "NLP server ($$) started.");
# Process document
while (42) {
# Obtain document
my $document = _getDocument();
# If there is no document, sleep for 5 secs
if (!defined($document)) {
system("touch servers/pids/nlp.pid");
sleep(5);
next;
}
RExtractor::Tools::info($LOG, "NLP processing of the document $document->{id} started.");
# Obtain strategy id
my $strategy_id = RExtractor::Tools::getDocumentStrategy($document->{id});
# Load strategy
my $Strategy = new RExtractor::Strategy();
if (!$Strategy->loadFile("./strategies/$strategy_id.xml")) {
RExtractor::Tools::error($LOG, "Couldn't load strategy from './strategies/$strategy_id.xml'.");
RExtractor::Tools::setDocumentStatus($document->{id}, "410 Couldn't load strategy.");
RExtractor::Tools::deleteFile("./data/converted/$document->{id}.lock");
next;
}
# Check, if Strategy configuration contains all needed attributes
if (!$Strategy->check("nlp")) {
RExtractor::Tools::error($LOG, "Strategy is incorrect or incomplete. ($document->{id}).");
RExtractor::Tools::setDocumentStatus($document->{id}, "410 Couldn't load strategy.");
RExtractor::Tools::deleteFile("./data/converted/$document->{id}.lock");
next;
}
RExtractor::Tools::info($LOG, "Applying conversion strategy '$strategy_id'.");
## Load file
my $Document = new RExtractor::Document();
if (!$Document->load($document->{filename})) {
system("rm -rf ./servers/tmp/nlp/$document->{id}");
RExtractor::Tools::error($LOG, "Couldn't load document ($document->{id}).");
RExtractor::Tools::setDocumentStatus($document->{id}, "410 Error occured during loading document.");
RExtractor::Tools::deleteFile("./data/converted/$document->{id}.lock");
next;
}
eval("use $Strategy->{nlp}{package}");
my $NLP = eval("new $Strategy->{nlp}{package}");
if (!$NLP->process($Strategy, $Document)) {
system("rm -rf ./servers/tmp/nlp/$document->{id}");
RExtractor::Tools::error($LOG, "Error occured while treex processing of the document ($document->{id}).");
RExtractor::Tools::error($LOG, `cat /tmp/treex.log`);
RExtractor::Tools::setDocumentStatus($document->{id}, "410 Error occured during document processing in treex.");
RExtractor::Tools::deleteFile("./data/converted/$document->{id}.lock");
next;
}
## Everything OK, log and unlock document
system("rm -rf ./servers/tmp/nlp/$document->{id}");
RExtractor::Tools::info($LOG, "NLP of the document $document->{id} finished.");
RExtractor::Tools::setDocumentStatus($document->{id}, "420 Document processed successfully.");
RExtractor::Tools::deleteFile("./data/converted/$document->{id}.lock");
system("touch servers/pids/nlp.pid");
}
##
## Obtaining document for nlp.
##
## Document must not have a lock file, it should have
## own log file and last message in the log should be
## 320 Document converted successfully.
##
sub _getDocument {
foreach my $file (split(/\n/, `find ./data/converted/ -type f -name '*.xml'`)) {
# Extract ID
my $id = $file;
$id =~ s/(?:.*\/)?([^\/]+)\.xml/$1/;
# Check lock
if (-f "./data/converted/$id.lock") {
next;
}
# Check log
if (not(-f "./data/logs/$id.log")) {
next;
}
# Check last message in log
my $status = RExtractor::Tools::getDocumentStatus($id);
if ($status !~ /^320/) {
next;
}
##
## At this point we have a new document for processing
##
# Create lock file
RExtractor::Tools::writeFile("./data/converted/$id.lock", "nlp");
# Log
RExtractor::Tools::setDocumentStatus($id, "400 Language processing started.");
# Return document id
return {filename => $file, id => $id};
}
return undef;
}
sub _daemonize {
fork and exit;
POSIX::setsid();
fork and exit;
umask 0;
close STDIN;
close STDOUT;
close STDERR;
}
sub _signalHandler {
RExtractor::Tools::info($LOG, "Received terminating signal. Preparing for termination...");
# Remove PID
RExtractor::Tools::deleteFile("./servers/pids/nlp.pid");
# Close log
RExtractor::Tools::info($LOG, "Terminating NOW!");
close($LOG);
exit(0);
}