global search MDL-25009 indexing failures in each module are now handled so that...
[moodle.git] / search / add.php
index f15a349..5f04782 100644 (file)
@@ -61,9 +61,7 @@
     }
     $dbcontrol = new IndexDBControl();
     $addition_count = 0;
-    $startindextime = time();
-
-    $indexdate = @$CFG->search_indexer_run_date;
+    $mainstartindextime = time();
 
     mtrace('Starting index update (additions)...');
     mtrace('Index size before: '.$CFG->search_index_size."\n");
 /// append virtual modules onto array
 
         foreach ($mods as $mod) {
+
+            $indexdate = 0;
+            $indexdatestring = 'search_indexer_run_date_'.$mod->name;
+            $startrundate = time();
+            if (isset($CFG->$indexdatestring)) {
+                $indexdate = $CFG->$indexdatestring;
+            }
+
             //build include file and function names
             $class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
             $db_names_function = $mod->name.'_db_names';
                         // foreach document, add it to the index and database table
                         foreach ($additions as $add) {
                             ++$addition_count;
+                            // try the addDocument() so possible dml_write_exception don't block other modules running.
+                            // also we can list all the new documents that are failing.
+                            try {
+                                // object to insert into db
+                                $dbid = $dbcontrol->addDocument($add);
+
+                                // synchronise db with index
+                                $add->addField(Zend_Search_Lucene_Field::Keyword('dbid', $dbid));
 
-                            // object to insert into db
-                            $dbid = $dbcontrol->addDocument($add);
+                                $index->addDocument($add);
 
-                            // synchronise db with index
-                            $add->addField(Zend_Search_Lucene_Field::Keyword('dbid', $dbid));
+                                mtrace("  Add: $add->title (database id = $add->dbid, moodle instance id = $add->docid)");
+                            }
 
-                            mtrace("  Add: $add->title (database id = $add->dbid, moodle instance id = $add->docid)");
+                            catch (dml_write_exception $e) {
+                                mtrace(" Add: FAILED adding '$add->title' ,  moodle instance id = $add->docid , Error: $e->error ");
+                                mtrace($e);
+                            }
 
-                            $index->addDocument($add);
                         }
                     }
                     else{
                         mtrace("No types to add.\n");
                     }
+
+                    //commit changes
+                    $index->commit();
+
+                    //update index date
+                    set_config($indexdatestring, $startrundate);
+
                     mtrace("Finished $mod->name.\n");
                 }
             }
 
 /// update index date and size
 
-    set_config('search_indexer_run_date', $startindextime);
+    set_config('search_indexer_run_date', $mainstartindextime);
     set_config('search_index_size', (int)$CFG->search_index_size + (int)$addition_count);
 
 /// print some additional info