From 91a5b193781d4f696269ba0d4b4e3d2cc6c20002 Mon Sep 17 00:00:00 2001 From: Shannon Date: Wed, 5 Aug 2015 18:32:59 +0200 Subject: [PATCH] Fixes: U4-6891 Can't get to the "Rebuild" button when Lucene index folder is empty as well as how the polling works. --- .../developer/examinemgmt.controller.js | 4 +- .../ExamineManagementApiController.cs | 56 +++++++++++++++---- 2 files changed, 49 insertions(+), 11 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/views/dashboard/developer/examinemgmt.controller.js b/src/Umbraco.Web.UI.Client/src/views/dashboard/developer/examinemgmt.controller.js index 125f6bc7f4..e439d0e37f 100644 --- a/src/Umbraco.Web.UI.Client/src/views/dashboard/developer/examinemgmt.controller.js +++ b/src/Umbraco.Web.UI.Client/src/views/dashboard/developer/examinemgmt.controller.js @@ -10,7 +10,7 @@ function examineMgmtController($scope, umbRequestHelper, $log, $http, $q, $timeo 'Failed to check index processing') .then(function(data) { - if (data) { + if (data !== null && data !== "null") { //copy all resulting properties for (var k in data) { @@ -67,7 +67,9 @@ function examineMgmtController($scope, umbRequestHelper, $log, $http, $q, $timeo "Depending on how much content there is in your site this could take a while. " + "It is not recommended to rebuild an index during times of high website traffic " + "or when editors are editing content.")) { + indexer.isProcessing = true; + indexer.processingAttempts = 0; umbRequestHelper.resourcePromise( $http.post(umbRequestHelper.getApiUrl("examineMgmtBaseUrl", "PostRebuildIndex", { indexerName: indexer.name })), diff --git a/src/Umbraco.Web/WebServices/ExamineManagementApiController.cs b/src/Umbraco.Web/WebServices/ExamineManagementApiController.cs index b56c2b1dae..136e2a984a 100644 --- a/src/Umbraco.Web/WebServices/ExamineManagementApiController.cs +++ b/src/Umbraco.Web/WebServices/ExamineManagementApiController.cs @@ -164,12 +164,23 @@ namespace Umbraco.Web.WebServices var msg = ValidateLuceneIndexer(indexerName, out indexer); if (msg.IsSuccessStatusCode) { + //remove it in case there's a handler there alraedy + indexer.IndexOperationComplete -= Indexer_IndexOperationComplete; + //now add a single handler + indexer.IndexOperationComplete += Indexer_IndexOperationComplete; + + var cacheKey = "temp_indexing_op_" + indexer.Name; + //put temp val in cache which is used as a rudimentary way to know when the indexing is done + ApplicationContext.ApplicationCache.RuntimeCache.InsertCacheItem(cacheKey, () => "tempValue", TimeSpan.FromMinutes(5), isSliding: false); + try { indexer.RebuildIndex(); } catch (Exception ex) { + //ensure it's not listening + indexer.IndexOperationComplete -= Indexer_IndexOperationComplete; LogHelper.Error("An error occurred rebuilding index", ex); var response = Request.CreateResponse(HttpStatusCode.Conflict); response.Content = new StringContent(string.Format("The index could not be rebuilt at this time, most likely there is another thread currently writing to the index. Error: {0}", ex)); @@ -180,14 +191,26 @@ namespace Umbraco.Web.WebServices return msg; } + //static listener so it's not GC'd + private static void Indexer_IndexOperationComplete(object sender, EventArgs e) + { + var indexer = (LuceneIndexer) sender; + + //ensure it's not listening anymore + indexer.IndexOperationComplete -= Indexer_IndexOperationComplete; + + var cacheKey = "temp_indexing_op_" + indexer.Name; + ApplicationContext.Current.ApplicationCache.RuntimeCache.ClearCacheItem(cacheKey); + } + /// /// Check if the index has been rebuilt /// /// /// /// - /// This is kind of rudimentary since there's no way we can know that the index has rebuilt, we'll just check - /// if the index is locked based on Lucene apis + /// This is kind of rudimentary since there's no way we can know that the index has rebuilt, we + /// have a listener for the index op complete so we'll just check if that key is no longer there in the runtime cache /// public ExamineIndexerModel PostCheckRebuildIndex(string indexerName) { @@ -195,9 +218,11 @@ namespace Umbraco.Web.WebServices var msg = ValidateLuceneIndexer(indexerName, out indexer); if (msg.IsSuccessStatusCode) { - var isLocked = indexer.IsIndexLocked(); - return isLocked - ? null + var cacheKey = "temp_indexing_op_" + indexerName; + var found = ApplicationContext.ApplicationCache.RuntimeCache.GetCacheItem(cacheKey); + //if its still there then it's not done + return found != null + ? null : CreateModel(indexer); } throw new HttpResponseException(msg); @@ -239,13 +264,24 @@ namespace Umbraco.Web.WebServices } var luceneIndexer = indexer as LuceneIndexer; - if (luceneIndexer != null && luceneIndexer.IndexExists()) + if (luceneIndexer != null) { indexerModel.IsLuceneIndex = true; - indexerModel.DocumentCount = luceneIndexer.GetIndexDocumentCount(); - indexerModel.FieldCount = luceneIndexer.GetIndexFieldCount(); - indexerModel.IsOptimized = luceneIndexer.IsIndexOptimized(); - indexerModel.DeletionCount = luceneIndexer.GetDeletedDocumentsCount(); + + if (luceneIndexer.IndexExists()) + { + indexerModel.DocumentCount = luceneIndexer.GetIndexDocumentCount(); + indexerModel.FieldCount = luceneIndexer.GetIndexFieldCount(); + indexerModel.IsOptimized = luceneIndexer.IsIndexOptimized(); + indexerModel.DeletionCount = luceneIndexer.GetDeletedDocumentsCount(); + } + else + { + indexerModel.DocumentCount = 0; + indexerModel.FieldCount = 0; + indexerModel.IsOptimized = true; + indexerModel.DeletionCount = 0; + } } return indexerModel; }