changeset 53:dd93cb4b77ad

more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
author Charlie Root
date Thu, 06 Jun 2019 16:37:20 -0400
parents d9dd9705e724
children a04fc91bbd95
files mq.php php/read/getBookDetails.php xml/doAmazonRequest.php
diffstat 3 files changed, 61 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- 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;
   }
--- 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>" . $isbn . "</ISBN>";
+		$output .=  "<Title>" . htmlspecialchars($rows["Title"]) . "</Title>";
+		$output .=  "<Author>" . htmlspecialchars($rows["Author"]) . "</Author>";
+		$output .=  "<Binding>" . htmlspecialchars($rows["Binding"]) . "</Binding>";
+		$output .=  "<Dewey>" . htmlspecialchars($rows["DeweyDecimal"]) . "</Dewey>";
+		$output .=  "<ImageURL>" . htmlspecialchars($rows["ImageURL"]) . "</ImageURL>";
+		$output .=  "<SalesRank>" . htmlspecialchars($rows["SalesRank"]) . "</SalesRank>";
+		$output .=  "<PublicationDate>" . htmlspecialchars($rows["PublicationDate"]) . "</PublicationDate>";
+		$output .=  "<Publisher>" . htmlspecialchars($rows["Publisher"]) . "</Publisher>";
+		$output .=  "<Genre1>" . htmlspecialchars($rows["Genre1"]) . "</Genre1>";
+		$output .=  "<Genre2>" . htmlspecialchars($rows["Genre2"]) . "</Genre2>";
+		$output .=  "<Genre3>" . htmlspecialchars($rows["Genre3"]) . "</Genre3>";
+		$output .=  "<ProductGroup>Book</ProductGroup>";
+	      }
+	      else {
+		# Causes looping behaviour from client :-(
+		dl("gBD:  no DB info either, bailing\n");
+		return;
+	      }
 	    }
 	  }
 	$output .=  "</BookDetails>";
--- 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("/<Error>/",$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");
 }