Browse Source

Fixed memory leaks

Stephen Ficklin 10 years ago
parent
commit
441eed840e

+ 1 - 1
tripal_contact/api/tripal_contact.api.inc

@@ -128,7 +128,7 @@ function tripal_autocomplete_contact($text) {
 
   $sql = "SELECT * FROM {contact} WHERE lower(name) like lower(:name) ";
   $args = array();
-  $args[':name'] = $name . '%';
+  $args[':name'] = $text . '%';
   $sql .= "ORDER BY name ";
   $sql .= "LIMIT 25 OFFSET 0 ";
   $results = chado_query($sql, $args);

+ 40 - 26
tripal_core/api/tripal_core.chado_nodes.api.inc

@@ -908,17 +908,15 @@ function chado_node_sync_records($base_table, $max_sync = FALSE,
   try {
     $percent = 0;
     foreach ($results as $record) {
-
-      //print "\nLoading $base_table " . ($i + 1) . " of $count ($base_table_id=" . $record->{$base_table_id} . ")...";
-
-      // update the job status every 1% features
+      // Update the job status every 1% features.
       if ($job_id and $i % $interval == 0) {
         $percent = sprintf("%.2f", (($i + 1) / $count) * 100);
         print "Syncing $base_table " . ($i + 1) . " of $count (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes.\r";
         tripal_set_job_progress($job_id, intval(($i/$count)*100));
       }
 
-      // Check if it is in the chado linking table (ie: check to see if it is already linked to a node)
+      // Check if the record is already in the chado linking table
+      // (ie: check to see if it is already linked to a node).
       $result = db_select($linking_table, 'lnk')
         ->fields('lnk',array('nid'))
         ->condition($base_table_id, $record->{$base_table_id}, '=')
@@ -926,7 +924,7 @@ function chado_node_sync_records($base_table, $max_sync = FALSE,
         ->fetchObject();
 
       if (empty($result)) {
-        // Create generic new node
+        // Create generic new node.
         $new_node = new stdClass();
         $new_node->type = $node_type;
         $new_node->uid = $user->uid;
@@ -948,8 +946,9 @@ function chado_node_sync_records($base_table, $max_sync = FALSE,
 
         if (!form_get_errors()) {
           $node = node_submit($new_node);
+          // If there are memory leaks on the node_save it is probably
+          // caused by the hook_node_insert() function.
           node_save($node);
-          //print " Node Created (nid=".$node->nid.")";
         }
         else {
           throw new Exception(t("Failed to insert $base_table: %title", array('%title' => $new_node->title)));
@@ -1001,12 +1000,23 @@ function chado_cleanup_orphaned_nodes($table, $nentries = 25000, $linking_table,
     $count = $temp->count;
   }
 
-  $m = ceil($count / $nentries);
-  for ($i = 0; $i < $m; $i++) {
-    $offset = ($nentries * $i);
-    chado_cleanup_orphaned_nodes_part($table, $job_id, $nentries, $offset, $linking_table, $node_type);
+  $transaction = db_transaction();
+  print "\nNOTE: Syncing is performed using a database transaction. \n" .
+    "If the sync fails or is terminated prematurely then the entire set of \n" .
+    "synced items is rolled back and will not be found in the database\n\n";
+  try {
+    $m = ceil($count / $nentries);
+    for ($i = 0; $i < $m; $i++) {
+      $offset = ($nentries * $i);
+      chado_cleanup_orphaned_nodes_part($table, $job_id, $nentries, $offset, $linking_table, $node_type);
+    }
+  }
+  catch (Exception $e) {
+    $transaction->rollback();
+    print "\n"; // make sure we start errors on new line
+    watchdog_exception('trp-fsync', $e);
+    print "FAILED: Rolling back database changes...\n";
   }
-
   return '';
 }
 
@@ -1023,7 +1033,9 @@ function chado_cleanup_orphaned_nodes($table, $nentries = 25000, $linking_table,
  *
  * @ingroup tripal_chado_node_api
  */
-function chado_cleanup_orphaned_nodes_part($table, $job_id = NULL, $nentries, $offset, $linking_table, $node_type) {
+function chado_cleanup_orphaned_nodes_part($table, $job_id = NULL, $nentries,
+    $offset, $linking_table, $node_type) {
+
   $count = 0;
 
   // Retrieve all of the entries in the linker table for a given node type
@@ -1126,19 +1138,21 @@ function chado_cleanup_orphaned_nodes_part($table, $job_id = NULL, $nentries, $o
 
       // check to see if the node has a corresponding entry
       // in the $linking_table table. If not then delete the node.
-      $csql = "SELECT * FROM {" . $linking_table . "} WHERE nid = :nid ";
-      $results = db_query($csql, array(':nid' => $node->nid));
-      $link = $results->fetchObject();
-      if (!$link) {
-        if (node_access('delete', $node)) {
-          $deleted++;
-          //print "Node missing in $linking_table table.... DELETING node $node->nid\n";
-          node_delete($node->nid);
-        }
-        else {
-          print "\nNode missing in $linking_table table.... but cannot delete due to improper permissions (node $node->nid)\n";
-        }
-      }
+       $csql = "SELECT * FROM {" . $linking_table . "} WHERE nid = :nid ";
+       $results = db_query($csql, array(':nid' => $node->nid));
+       $link = $results->fetchObject();
+       if (!$link) {
+         // Checking node_access creates a memory leak. Commenting out for now
+         // assuming that this code can only be run by a site administrator
+         // anyway.
+//         if (node_access('delete', $node)) {
+           $deleted++;
+           node_delete($node->nid);
+//         }
+//         else {
+//           print "\nNode missing in $linking_table table.... but cannot delete due to improper permissions (node $node->nid)\n";
+//         }
+       }
       $i++;
     }
     $percent = sprintf("%.2f", ($i / $count) * 100);

+ 2 - 4
tripal_feature/includes/tripal_feature.chado_node.inc

@@ -599,13 +599,13 @@ function chado_feature_delete($node) {
 
   $feature_id  = chado_get_id_from_nid('feature', $node->nid);
 
-  // if we don't have a library id for this node then this isn't a node of
+  // If we don't have a feature id for this node then this isn't a node of
   // type chado_library or the entry in the chado_library table was lost.
   if (!$feature_id) {
     return;
   }
 
-  // remove the drupal content
+  // Remove the drupal content.
   $sql_del = "DELETE FROM {chado_feature} WHERE nid = :nid AND vid = :vid";
   db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
   $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
@@ -793,7 +793,6 @@ function tripal_feature_node_presave($node) {
  * @ingroup tripal_feature
  */
 function tripal_feature_node_insert($node) {
-
   // set the URL path after inserting.  We do it here because we do not
   // know the feature_id in the presave
   switch ($node->type) {
@@ -810,7 +809,6 @@ function tripal_feature_node_insert($node) {
 
       // Now get the title.
       $node->title = chado_get_node_title($node);
-
       break;
   }
 }