diff 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
line wrap: on
line diff
--- a/php/read/getBookDetails.php	Sun Dec 30 06:59:13 2018 -0500
+++ b/php/read/getBookDetails.php	Sun Dec 30 07:00:09 2018 -0500
@@ -69,6 +69,7 @@
 	if ( mysqli_num_rows( $res ) > 0 )
 	{
 		$rows=mysqli_fetch_array($res, MYSQLI_ASSOC);
+		mysqli_close($link);	//do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop
 		$timestamp=$rows['Timestamp'];
 		//echo $timestamp;
 		date_default_timezone_set('UTC');
@@ -97,7 +98,9 @@
 			$output .=  "<ProductGroup>Book</ProductGroup>";
 		}
 	}
-	mysqli_close($link);	//do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop
+	else {
+	  mysqli_close($link);	//do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop
+	}
 	include "../../../private/db.php";
 	
 	if($proceed == true)
@@ -207,17 +210,17 @@
   	        $gotit=0;
 		$requestIP = $_SERVER['REMOTE_ADDR'];
 		$semaphore = new SyncSemaphore("Amazon");
-		$gotit = $semaphore->lock(4000);
+		$gotit = $semaphore->lock(1000);
 		if (!$gotit) {
 		  file_put_contents('/var/ywww/debug/phpDebug',
            	  "Got it: ".(int)$gotit." $requestIP 2\n",FILE_APPEND);
-		  $gotit=$semaphore->lock(5000);
+		  $gotit=$semaphore->lock(1000);
 		  file_put_contents('/var/ywww/debug/phpDebug',
 		    "Got it: ".(int)$gotit." $requestIP 2a\n",FILE_APPEND);
 		}
 		$out = curl_exec($crl);
 		curl_close($crl);
-		usleep(1000000);
+		usleep(500000);
 		if ($gotit) {
 		  $semaphore->unlock();
 		}
@@ -226,13 +229,16 @@
 				    "W/o lock for $requestIP 2b\n",FILE_APPEND);
 		}
 		//echo $out;
-		
-		if (preg_match("<Error>",$out)) {
+		$mm=array();
+		if (preg_match("<Error>",$out,$mm)) {
 		  $xml = new SimpleXMLElement($out);	
 		  $resName=$xml->getName();
 		  $code=$xml->Error->Code;
+		  if (!$code) {
+		    $code=$xml->Items->Request->Errors->Error->Code;
+		  }
 		  $requestIP = $_SERVER['REMOTE_ADDR'];
-		  file_put_contents('/var/ywww/debug/phpDebug',"Losing gBD: $resName, $code, $requestIP\n",FILE_APPEND);
+		  file_put_contents('/var/ywww/debug/phpDebug',"Losing gBD: ".$mm[0].", $resName, $code, $requestIP\n",FILE_APPEND);
 		  if ($code=='RequestThrottled') {
 		    usleep(200000); // Try to reduce throttling until we get a 
 		    // principled solution in place
@@ -278,6 +284,16 @@
 		  $pubDate = $xml->Items->Item->ItemAttributes->PublicationDate;
 		  $publisher = $xml->Items->Item->ItemAttributes->Publisher;
 		  restore_error_handler();
+		  if ($publisher and strlen($publisher)>30) {
+		    $publisher=substr($publisher,0,30);
+		  }
+		  if ($title and strlen($title)>100) {
+		    $title=substr($title,0,100);
+		  }
+		  $publisher=mysqli_real_escape_string($link,$publisher);
+		  $title=mysqli_real_escape_string($link,$title);
+		  if (strlen($pubDate)==4) { $pubDate=$pubDate."-01-01";}
+		  if (strlen($pubDate)==7) { $pubDate=$pubDate."-01";}
 		  /*echo $title;
 		   echo $author;
 		   echo $binding;
@@ -316,6 +332,10 @@
 		
 		    foreach ($genArr as $key => $value) {
 		      //echo "$key => $value";
+		      if ($key>2047) {
+			//HST added
+			break;
+		      }
 		      $queryG = "CALL b_addBrowseNode($key,\"$value\")";	//add the name value pair for genre to new table	
 		      //echo $queryG;
 		      $resG = mysqli_query($link, $queryG);
@@ -335,7 +355,7 @@
 		      $loop++;
 		    }
 		  }
-		
+		  
 		  if($salesRank == "")
 		    $salesRank = "null";
 		
@@ -346,7 +366,15 @@
 		  $queryInsert = "CALL b_addNewBook(\"$isbn\",\"$title\", \"$author\",\"$binding\",\"$imageURL\", $dewey, $salesRank,\"$pubDate\",\"$publisher\",$g1,$g2,$g3,$loc)";	
 		  //echo $queryInsert;
 		
-		  $resG = mysqli_query($link, $queryInsert) or exit( mysqli_error( $link ));
+		  $res = mysqli_query($link, $queryInsert);
+				if (!$res) {
+				  $err=mysqli_error( $link );
+				  mysqli_close($link);
+				  file_put_contents('/var/ywww/debug/phpDebug',
+					"anb failed: $err, $pubDate, $g2, $publisher, $title\n",
+					FILE_APPEND);
+				  exit($err);
+				}
 		  mysqli_close($link);	//do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop