lib/NewsClipper/Server/MySQL.pm untested

filter: Statementstmt 0.0% 0 / 147 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 240 CC 22 SCAR 34.0
CC
22
Cov
0%
_Talk_To_Server
90.0
Get_Handler
20.0
_Get_Compatible_Working_Handler_Version
20.0
CRAP 29.9
1 # -*- mode: Perl; -*-
2 package NewsClipper::Server::MySQL;
3  
4 # This package implements the fast MySQL-based interface to the handler
5 # server.
6  
7 use strict;
8 use Carp;
9 use DBI;
10 use DBD::mysql;
11  
12 use vars qw( $VERSION @ISA );
13 @ISA = qw( NewsClipper::Server );
14  
15 $VERSION = 0.10;
16  
17 use NewsClipper::Globals;
18  
19 # Caches used to avoid unnecessary processing of handlers
20 my %downloadedCode;
21 my %handler_type;
22  
23 # Avoid multiple warnings about the server being down
24 my $already_warned_server_down = 0;
25  
26 # The database handle;
27 my $dbh = undef;
28  
29 # ------------------------------------------------------------------------------
30  
31 sub new
32 {
33 my $proto = shift;
34  
35 # We take the ref if "new" was called on an object, and the class ref
36 # otherwise.
37 my $class = ref($proto) || $proto;
38  
39 # Create an "object"
40 my $self = {};
41  
42 # Make the object a member of the class
43 bless ($self, $class);
44  
45 return $self;
46 }
47  
48 # ------------------------------------------------------------------------------
49  
50 # Connect to the database, storing the DB connection in $dbh for the
51 # duration of the run. Returns 1 if successful, 0 if not.
52  
53 sub _Connect_To_DB
54 {
55 my $self = shift;
56  
57 return $dbh if defined $dbh;
58  
59 dprint "Attempting to connect to handler database.";
60  
61 my $code =<<" EOF";
62 return
63 DBI->connect('DBI:mysql:handlers:$self->{handler_server}','webuser');
64 EOF
65  
66 $dbh = $self->_Talk_To_Server($code);
67  
68 if (defined $dbh)
69 {
70 dprint "Database connect succeeded.";
71 return(1);
72 }
73 else
74 {
75 dprint "Database connect failed.";
76 return(0);
77 }
78 }
79  
80 # ------------------------------------------------------------------------------
81  
82 # Disconnect from the database.
83  
84 sub _Disconnect_From_DB
85 {
86 if (defined $dbh)
87 {
88 $dbh->disconnect();
89 dprint "Disconnected from database";
90 }
91 }
92  
93 # ------------------------------------------------------------------------------
94  
95 # Evals code that talks to the server, taking the socket timeout and socket
96 # retries into account. The code should result in a nonzero, defined value if
97 # successful. Returns 2 values. The first value is 1 or 0 depending on whether
98 # the connect succeeded, and the second value is the result of eval'ing the
99 # code.
100  
101 sub _Talk_To_Server
102 {
103 my $self = shift;
104 my $code = shift;
105  
106 # Connect to the database if we haven't already. (We connect on demand.)
107 # Also avoid recursion when _Connect_To_DB calls this function.
108 {
109 my $calling_function = @{[caller(1)]}[3];
110  
111 if (!defined $dbh &&
112 $calling_function ne 'NewsClipper::Server::MySQL::_Connect_To_DB')
113 {
114 unless ($self->_Connect_To_DB())
115 {
116 return (0,undef);
117 }
118 }
119 }
120  
121 local $SIG{ALRM} = sub { die "database timeout" };
122  
123 my $numTriesLeft = $config{socket_tries};
124 my $errors = '';
125 my $completed_successfully = 0;
126 my $return_value;
127  
128 do
129 {
130 local $SIG{__WARN__} = sub { $errors .= $_[0] unless $errors eq $_[0] };
131  
132 # Wrap in an eval to trap die's in the DBI code
133 alarm($config{socket_timeout});
134 $return_value = eval $code;
135 alarm(0);
136  
137 $completed_successfully = 1 if $@ eq '' && $errors eq '';
138  
139 $numTriesLeft--;
140 } until ($numTriesLeft == 0 || $completed_successfully);
141  
142 if ($completed_successfully)
143 {
144 return(1,$return_value);
145 }
146 else
147 {
148 if ($@ =~ /database timeout/)
149 {
150 dprint "Could not access database: Connection timed out";
151 lprint "Could not access database: Connection timed out";
152 }
153 elsif ($errors ne '')
154 {
155 dprint "Could not access database: $errors";
156 lprint "Could not access database: $errors";
157 }
158 elsif (defined $dbh)
159 {
160 dprint "Could not access database: $DBI::errstr";
161 lprint "Could not access database: $DBI::errstr";
162 }
163 else
164 {
165 dprint "Could not access database. An unknown error occurred";
166 lprint "Could not access database. An unknown error occurred";
167 }
168  
169 return(0,undef);
170 }
171 }
172  
173 # ------------------------------------------------------------------------------
174  
175 # Download the handler type from the remote server, caching it locally in
176 # %handler_type. The first return value is the status of the request, and the
177 # second is the handler type. The status is 0 if the server can't be
178 # contacted, or the returned data can't be parsed.
179  
180 sub Get_Handler_Type
181 {
182 my $self = shift;
183 my $handler_name = shift;
184 my $ncversion = shift;
185  
186 dprint "Downloading handler type information.\n";
187  
188 if (exists $handler_type{$handler_name})
189 {
190 dprint "Reusing cached handler type information " .
191 "($handler_name is $handler_type{$handler_name})";
192 return (1,$handler_type{$handler_name});
193 }
194  
195 my $table = _Get_Table($ncversion);
196  
197 my $query = qq{ SELECT Type FROM $table WHERE Name like '$handler_name'
198 and Status like 'Working'
199 ORDER BY Version DESC };
200  
201 my $talk_code =<<" EOF";
202 return scalar \$dbh->selectrow_array(q{$query});
203 EOF
204  
205 my ($succeeded,$type) = $self->_Talk_To_Server($talk_code);
206  
207 return (0,undef) unless $succeeded;
208  
209 $handler_type{$handler_name} = $type;
210 return (1,$type);
211 }
212  
213 # ------------------------------------------------------------------------------
214  
215 # This function downloads a new handler from the handler database. The first
216 # argument is the name of the handler. The second argument is the version
217 # number of the current handler. You should call Get_New_Handler_Version before
218 # calling this function.
219  
220 # This function returns two values:
221 # - an error code: (okay, not found, failed: error message)
222 # - the handler (if the error code is okay)
223  
224 sub Get_Handler
225 {
226 my $self = shift;
227 my $handler_name = shift;
228 my $version = shift;
229 my $ncversion = shift;
230  
231 dprint "Downloading code for handler \"$handler_name\"";
232  
233 if (defined $downloadedCode{$handler_name})
234 {
235 dprint "Reusing already downloaded code.";
236 return ('okay',$downloadedCode{$handler_name});
237 }
238  
239 my $table = _Get_Table($ncversion);
240  
241 my $query = qq{ SELECT Code FROM $table WHERE Name like '$handler_name'
242 and Status like 'Working'
243 ORDER BY Version DESC };
244  
245 my $talk_code =<<" EOF";
246 return scalar \$dbh->selectrow_array(q{$query});
247 EOF
248  
249 my ($succeeded,$code) = $self->_Talk_To_Server($talk_code);
250  
251 # If either the download failed, or the thing we got back doesn't look like
252 # a handler...
253 unless ($succeeded)
254 {
255 my $error_message = reformat dequote <<" EOF";
256 failed: Couldn't download handler $handler_name. Maybe the server is
257 down. Try again in a while, and send email to bugreport\@newsclipper.com
258 if the problem persists. Check the run log for additional information.
259 EOF
260  
261 return ($error_message,undef);
262 }
263  
264 return ('not found',undef) if $succeeded && !defined $code;
265  
266 $downloadedCode{$handler_name} = $code;
267 return ('okay',$code);
268 }
269  
270 # ------------------------------------------------------------------------------
271  
272 # Computes the most recent version number for a working handler.
273 # Returns a status value and the version. The status can be one of 'okay',
274 # 'failed', or 'not found'.
275  
276 sub _Get_Latest_Working_Handler_Version
277 {
278 my $self = shift;
279 my $handler_name = shift;
280 my $ncversion = shift;
281  
282 my $table = _Get_Table($ncversion);
283  
284 my $query = qq{ SELECT Version FROM $table WHERE Name like '$handler_name'
285 and Status like 'Working' ORDER BY Version DESC };
286  
287 my $talk_code =<<" EOF";
288 return scalar \$dbh->selectrow_array(q{$query});
289 EOF
290  
291 my ($succeeded,$version) = $self->_Talk_To_Server($talk_code);
292  
293 if ($succeeded)
294 {
295 if (defined $version)
296 {
297 return ('okay',$version);
298 }
299 else
300 {
301 return ('not found',undef);
302 }
303 }
304 else
305 {
306 lprint reformat dequote <<" EOF";
307 ERROR: Couldn't get handler version information from server. Please
308 send email to bugreport\@newsclipper.com describing this message.
309 EOF
310  
311 return ('failed',undef);
312 }
313 }
314  
315 # ------------------------------------------------------------------------------
316  
317 sub _Get_Table
318 {
319 my $version = shift;
320  
321 my $table = $version;
322 $table =~ s/\./_/g;
323  
324 return $table;
325 }
326  
327 # ------------------------------------------------------------------------------
328  
329 # Computes the most recent guaranteed-compatible version number for a working
330 # handler. Returns a status value and the version. The status can be one of
331 # 'okay', 'no update', 'failed', or 'not found'. The parameter $version
332 # should be defined.
333  
334 sub _Get_Compatible_Working_Handler_Version
335 {
336 my $self = shift;
337 my $handler_name = shift;
338 my $ncversion = shift;
339 my $version = shift;
340  
341 my $table = _Get_Table($ncversion);
342  
343 # Truncate to two decimal places, and increment the hundredths place so we
344 # can query for < $version
345 my $lower_version = sprintf("%0.2f",int($version * 100)/100);
346 my $upper_version = sprintf("%0.2f",int($version * 100)/100 + .01);
347  
348 my $query = qq{ SELECT Version FROM $table WHERE Name like '$handler_name'
349 and Status like 'Working'
350 and Version < $upper_version and Version >= $lower_version
351 ORDER BY Version DESC };
352  
353 my $talk_code =<<" EOF";
354 return scalar \$dbh->selectrow_array(q{$query});
355 EOF
356  
357 my ($succeeded,$new_version) = $self->_Talk_To_Server($talk_code);
358  
359 if ($succeeded)
360 {
361 if (defined $new_version)
362 {
363 return ('okay',$new_version);
364 }
365 # We have to differentiate between 'not found' and 'no update'
366 else
367 {
368 my $query = qq{ SELECT Name FROM $table WHERE Name like '$handler_name' };
369  
370 my $talk_code =<<" EOF";
371 return scalar \$dbh->selectrow_array(q{$query});
372 EOF
373  
374 my ($succeeded2,$name) = $self->_Talk_To_Server($talk_code);
375  
376 if (defined $name)
377 {
378 return ('no update',undef);
379 }
380 else
381 {
382 return ('not found',undef);
383 }
384 }
385 }
386 else
387 {
388 return ('failed',undef);
389 }
390 }
391  
392 # ------------------------------------------------------------------------------
393  
394 END
395 {
396 _Disconnect_From_DB();
397 }
398  
399 1;