Mercurial > hg > ywww
comparison php/read/getBookDetails.php @ 23:d606320ec331
post-5.7-upgrade efforts to reduce dropped connections, db insertion fails
author | Charlie Root |
---|---|
date | Sun, 30 Dec 2018 07:00:09 -0500 |
parents | 46382face560 |
children | 828895488948 |
comparison
equal
deleted
inserted
replaced
22:69c37b58d091 | 23:d606320ec331 |
---|---|
67 $res = mysqli_query($link, $query) or exit( mysqli_error( $link )); | 67 $res = mysqli_query($link, $query) or exit( mysqli_error( $link )); |
68 | 68 |
69 if ( mysqli_num_rows( $res ) > 0 ) | 69 if ( mysqli_num_rows( $res ) > 0 ) |
70 { | 70 { |
71 $rows=mysqli_fetch_array($res, MYSQLI_ASSOC); | 71 $rows=mysqli_fetch_array($res, MYSQLI_ASSOC); |
72 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop | |
72 $timestamp=$rows['Timestamp']; | 73 $timestamp=$rows['Timestamp']; |
73 //echo $timestamp; | 74 //echo $timestamp; |
74 date_default_timezone_set('UTC'); | 75 date_default_timezone_set('UTC'); |
75 $dbStamp = strtotime($timestamp); | 76 $dbStamp = strtotime($timestamp); |
76 //echo $dbStamp; | 77 //echo $dbStamp; |
95 $output .= "<Genre2>" . htmlspecialchars($rows["Genre2"]) . "</Genre2>"; | 96 $output .= "<Genre2>" . htmlspecialchars($rows["Genre2"]) . "</Genre2>"; |
96 $output .= "<Genre3>" . htmlspecialchars($rows["Genre3"]) . "</Genre3>"; | 97 $output .= "<Genre3>" . htmlspecialchars($rows["Genre3"]) . "</Genre3>"; |
97 $output .= "<ProductGroup>Book</ProductGroup>"; | 98 $output .= "<ProductGroup>Book</ProductGroup>"; |
98 } | 99 } |
99 } | 100 } |
100 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop | 101 else { |
102 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop | |
103 } | |
101 include "../../../private/db.php"; | 104 include "../../../private/db.php"; |
102 | 105 |
103 if($proceed == true) | 106 if($proceed == true) |
104 { | 107 { |
105 include "aws_signed_request.php"; | 108 include "aws_signed_request.php"; |
205 curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); | 208 curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); |
206 curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); | 209 curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); |
207 $gotit=0; | 210 $gotit=0; |
208 $requestIP = $_SERVER['REMOTE_ADDR']; | 211 $requestIP = $_SERVER['REMOTE_ADDR']; |
209 $semaphore = new SyncSemaphore("Amazon"); | 212 $semaphore = new SyncSemaphore("Amazon"); |
210 $gotit = $semaphore->lock(4000); | 213 $gotit = $semaphore->lock(1000); |
211 if (!$gotit) { | 214 if (!$gotit) { |
212 file_put_contents('/var/ywww/debug/phpDebug', | 215 file_put_contents('/var/ywww/debug/phpDebug', |
213 "Got it: ".(int)$gotit." $requestIP 2\n",FILE_APPEND); | 216 "Got it: ".(int)$gotit." $requestIP 2\n",FILE_APPEND); |
214 $gotit=$semaphore->lock(5000); | 217 $gotit=$semaphore->lock(1000); |
215 file_put_contents('/var/ywww/debug/phpDebug', | 218 file_put_contents('/var/ywww/debug/phpDebug', |
216 "Got it: ".(int)$gotit." $requestIP 2a\n",FILE_APPEND); | 219 "Got it: ".(int)$gotit." $requestIP 2a\n",FILE_APPEND); |
217 } | 220 } |
218 $out = curl_exec($crl); | 221 $out = curl_exec($crl); |
219 curl_close($crl); | 222 curl_close($crl); |
220 usleep(1000000); | 223 usleep(500000); |
221 if ($gotit) { | 224 if ($gotit) { |
222 $semaphore->unlock(); | 225 $semaphore->unlock(); |
223 } | 226 } |
224 else { | 227 else { |
225 file_put_contents('/var/ywww/debug/phpDebug', | 228 file_put_contents('/var/ywww/debug/phpDebug', |
226 "W/o lock for $requestIP 2b\n",FILE_APPEND); | 229 "W/o lock for $requestIP 2b\n",FILE_APPEND); |
227 } | 230 } |
228 //echo $out; | 231 //echo $out; |
229 | 232 $mm=array(); |
230 if (preg_match("<Error>",$out)) { | 233 if (preg_match("<Error>",$out,$mm)) { |
231 $xml = new SimpleXMLElement($out); | 234 $xml = new SimpleXMLElement($out); |
232 $resName=$xml->getName(); | 235 $resName=$xml->getName(); |
233 $code=$xml->Error->Code; | 236 $code=$xml->Error->Code; |
237 if (!$code) { | |
238 $code=$xml->Items->Request->Errors->Error->Code; | |
239 } | |
234 $requestIP = $_SERVER['REMOTE_ADDR']; | 240 $requestIP = $_SERVER['REMOTE_ADDR']; |
235 file_put_contents('/var/ywww/debug/phpDebug',"Losing gBD: $resName, $code, $requestIP\n",FILE_APPEND); | 241 file_put_contents('/var/ywww/debug/phpDebug',"Losing gBD: ".$mm[0].", $resName, $code, $requestIP\n",FILE_APPEND); |
236 if ($code=='RequestThrottled') { | 242 if ($code=='RequestThrottled') { |
237 usleep(200000); // Try to reduce throttling until we get a | 243 usleep(200000); // Try to reduce throttling until we get a |
238 // principled solution in place | 244 // principled solution in place |
239 } | 245 } |
240 else { | 246 else { |
276 $imageURL = $xml->Items->Item->MediumImage->URL; | 282 $imageURL = $xml->Items->Item->MediumImage->URL; |
277 $salesRank = $xml->Items->Item->SalesRank; | 283 $salesRank = $xml->Items->Item->SalesRank; |
278 $pubDate = $xml->Items->Item->ItemAttributes->PublicationDate; | 284 $pubDate = $xml->Items->Item->ItemAttributes->PublicationDate; |
279 $publisher = $xml->Items->Item->ItemAttributes->Publisher; | 285 $publisher = $xml->Items->Item->ItemAttributes->Publisher; |
280 restore_error_handler(); | 286 restore_error_handler(); |
287 if ($publisher and strlen($publisher)>30) { | |
288 $publisher=substr($publisher,0,30); | |
289 } | |
290 if ($title and strlen($title)>100) { | |
291 $title=substr($title,0,100); | |
292 } | |
293 $publisher=mysqli_real_escape_string($link,$publisher); | |
294 $title=mysqli_real_escape_string($link,$title); | |
295 if (strlen($pubDate)==4) { $pubDate=$pubDate."-01-01";} | |
296 if (strlen($pubDate)==7) { $pubDate=$pubDate."-01";} | |
281 /*echo $title; | 297 /*echo $title; |
282 echo $author; | 298 echo $author; |
283 echo $binding; | 299 echo $binding; |
284 echo $dewey; | 300 echo $dewey; |
285 echo $imageURL; | 301 echo $imageURL; |
314 $g3 = "null"; | 330 $g3 = "null"; |
315 $loop = 1; | 331 $loop = 1; |
316 | 332 |
317 foreach ($genArr as $key => $value) { | 333 foreach ($genArr as $key => $value) { |
318 //echo "$key => $value"; | 334 //echo "$key => $value"; |
335 if ($key>2047) { | |
336 //HST added | |
337 break; | |
338 } | |
319 $queryG = "CALL b_addBrowseNode($key,\"$value\")"; //add the name value pair for genre to new table | 339 $queryG = "CALL b_addBrowseNode($key,\"$value\")"; //add the name value pair for genre to new table |
320 //echo $queryG; | 340 //echo $queryG; |
321 $resG = mysqli_query($link, $queryG); | 341 $resG = mysqli_query($link, $queryG); |
322 | 342 |
323 switch ($loop) { | 343 switch ($loop) { |
333 } | 353 } |
334 | 354 |
335 $loop++; | 355 $loop++; |
336 } | 356 } |
337 } | 357 } |
338 | 358 |
339 if($salesRank == "") | 359 if($salesRank == "") |
340 $salesRank = "null"; | 360 $salesRank = "null"; |
341 | 361 |
342 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop | 362 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop |
343 include "../../../private/db.php"; | 363 include "../../../private/db.php"; |
344 | 364 |
345 $title = strtr($title, '"', "'"); | 365 $title = strtr($title, '"', "'"); |
346 $queryInsert = "CALL b_addNewBook(\"$isbn\",\"$title\", \"$author\",\"$binding\",\"$imageURL\", $dewey, $salesRank,\"$pubDate\",\"$publisher\",$g1,$g2,$g3,$loc)"; | 366 $queryInsert = "CALL b_addNewBook(\"$isbn\",\"$title\", \"$author\",\"$binding\",\"$imageURL\", $dewey, $salesRank,\"$pubDate\",\"$publisher\",$g1,$g2,$g3,$loc)"; |
347 //echo $queryInsert; | 367 //echo $queryInsert; |
348 | 368 |
349 $resG = mysqli_query($link, $queryInsert) or exit( mysqli_error( $link )); | 369 $res = mysqli_query($link, $queryInsert); |
370 if (!$res) { | |
371 $err=mysqli_error( $link ); | |
372 mysqli_close($link); | |
373 file_put_contents('/var/ywww/debug/phpDebug', | |
374 "anb failed: $err, $pubDate, $g2, $publisher, $title\n", | |
375 FILE_APPEND); | |
376 exit($err); | |
377 } | |
350 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop | 378 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop |
351 | 379 |
352 | 380 |
353 $output .= "<ISBN>" . htmlspecialchars($isbn) . "</ISBN>"; | 381 $output .= "<ISBN>" . htmlspecialchars($isbn) . "</ISBN>"; |
354 $output .= "<Title>" . htmlspecialchars($title) . "</Title>"; | 382 $output .= "<Title>" . htmlspecialchars($title) . "</Title>"; |