# HG changeset patch
# User Charlie Root
# Date 1559853440 14400
# Node ID dd93cb4b77adecd9c3246002226a137668312cea
# Parent d9dd9705e724dfc967944fe4887cf3d98588dd63
more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
diff -r d9dd9705e724 -r dd93cb4b77ad mq.php
--- a/mq.php Thu Jun 06 16:33:34 2019 -0400
+++ b/mq.php Thu Jun 06 16:37:20 2019 -0400
@@ -3,6 +3,8 @@
{
global $link;
$res = mysqli_query($link, $q);
+ include_once "dlog.php";
+ dl("query from $d: ".mysqli_num_rows( $res )." $q\n");
if ($res) {
return $res;
}
diff -r d9dd9705e724 -r dd93cb4b77ad php/read/getBookDetails.php
--- a/php/read/getBookDetails.php Thu Jun 06 16:33:34 2019 -0400
+++ b/php/read/getBookDetails.php Thu Jun 06 16:37:20 2019 -0400
@@ -103,7 +103,7 @@
else {
mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop
}
-
+ dl("gbd: ".strlen($output)." $proceed\n$output\n");
if($proceed == true)
{
include "aws_signed_request.php";
@@ -147,7 +147,7 @@
}
try {
- $xml=doAmazonRequest($Aserver[$locale]['ext'],$parameters,6,'d');
+ $xml=doAmazonRequest($Aserver[$locale]['ext'],$parameters,2,'d');
//use this xml to pull out the necessary information and save it
set_error_handler(function () {
global $out,$isbn;
@@ -288,6 +288,28 @@
}
catch (Exception $e) {
dl("gBD: dAR failed:".$e->getMessage()."\n");
+ if (isset($rows)) {
+ dl("gBD: returning stale info from DB: $timeDiff\n");
+ /* HST hack, copied from above */
+ $output .= "" . $isbn . "";
+ $output .= "
" . htmlspecialchars($rows["Title"]) . "";
+ $output .= "" . htmlspecialchars($rows["Author"]) . "";
+ $output .= "" . htmlspecialchars($rows["Binding"]) . "";
+ $output .= "" . htmlspecialchars($rows["DeweyDecimal"]) . "";
+ $output .= "" . htmlspecialchars($rows["ImageURL"]) . "";
+ $output .= "" . htmlspecialchars($rows["SalesRank"]) . "";
+ $output .= "" . htmlspecialchars($rows["PublicationDate"]) . "";
+ $output .= "" . htmlspecialchars($rows["Publisher"]) . "";
+ $output .= "" . htmlspecialchars($rows["Genre1"]) . "";
+ $output .= "" . htmlspecialchars($rows["Genre2"]) . "";
+ $output .= "" . htmlspecialchars($rows["Genre3"]) . "";
+ $output .= "Book";
+ }
+ else {
+ # Causes looping behaviour from client :-(
+ dl("gBD: no DB info either, bailing\n");
+ return;
+ }
}
}
$output .= "";
diff -r d9dd9705e724 -r dd93cb4b77ad xml/doAmazonRequest.php
--- a/xml/doAmazonRequest.php Thu Jun 06 16:33:34 2019 -0400
+++ b/xml/doAmazonRequest.php Thu Jun 06 16:37:20 2019 -0400
@@ -18,30 +18,37 @@
$url=aws_signed_request($ext,$parameters,$public_key,$private_key);
//dl("search: $ext $public_key $private_key\n$url\n".print_r($parameters,TRUE)."\n");
for ($i=1; $i<=$try; $i++) {
+ dl("dar: $requestIP $i\n");
$crl = curl_init();
$timeout = 5;
curl_setopt ($crl, CURLOPT_URL,$url);
curl_setopt ($crl, CURLOPT_ENCODING , "gzip");
curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
- $semaphore = new SyncSemaphore("Amazon");
- $gotit = $semaphore->lock(1000);
+ $semaphore = new SyncSemaphore("Amazon".$ext);
+ $gotit = $semaphore->lock(5000);
if (!$gotit) {
- dl("Got it: ".(int)$gotit." $requestIP $dbl\n");
- $gotit=$semaphore->lock(1000);
- dl("Got it: ".(int)$gotit." $requestIP $dbl"."a\n");
- }
- $output = curl_exec($crl);
- //dl("nai2: ".strlen($output)."\n");
-
- curl_close($crl);
- usleep(500000);
- if ($gotit) {
- $semaphore->unlock();
+ dl("No lock: $requestIP $dbl\n");
+ for ($j=1; $j<=$try+4; $j++) {
+ dl("dar: $requestIP $i $j\n");
+ $gotit=$semaphore->lock(1000);
+ if ($gotit) {
+ dl("Got it: $i $j $requestIP $dbl"."a\n");
+ break;
+ }
+ }
}
else {
- dl("W/o lock for $requestIP $dbl"."b\n");
+ dl("Got it: $i ! $requestIP $dbl"."a\n");
}
+ if (!$gotit) {
+ usleep(1000000);
+ continue;
+ }
+ usleep(1000000);
+ $output = curl_exec($crl);
+ dl("nai2: $gotit ".strlen($output)."\n");
+ curl_close($crl);
$mm=array();
$xml = new SimpleXMLElement($output);
if (preg_match("//",$output,$mm)) {
@@ -68,16 +75,27 @@
$parameters['Keywords']=urlencode($isbnMaybe);
}
dl("retrying with $isbnMaybe\n");
+ if ($gotit) {
+ $semaphore->unlock();
+ }
return doAmazonRequest($ext,$parameters,$try,$dbl);
}
}
if ($code=="") {
dl("error elt:\n$output\n");
}
+ if ($gotit) {
+ $semaphore->unlock();
+ }
throw new Exception($code);
}
}
else {
+ if ($gotit) {
+ $cnt=0;
+ $semaphore->unlock($cnt);
+ dl("unlocked: $i $cnt\n");
+ }
if ($wantXML) {
return $xml;
}
@@ -88,6 +106,9 @@
}
usleep(100000*$i); // Try to reduce throttling until we get a
// principled solution in place
+ if ($gotit) {
+ $semaphore->unlock();
+ }
}
throw new Exception("ThrottledRepeatedly");
}