Hi
We have a project where we build the index of a repository used for news items manually. The rebuilding code is run when a item of the type "news item" is saved. To avoid building both instances of the index at the same time, we have used a check to avoid this which is as follows:
public static void RebuildIndex(string repoName, string indexName, string build)
{
if (!string.IsNullOrEmpty(indexName) && !indexName.ToLower().EndsWith(".index"))
{
indexName = string.Format("{0}.index", indexName);
}
// Get the current IIndexService from the ServiceLocator
Task.Factory.StartNew(() =>
{
var indexService = ServiceLocator.Current.GetInstance<IIndexService>();
// Build the specified index using the IIndexService
var index = indexService.LoadIndex(repoName, indexName);
var currentInstance = index.GetInstance();
foreach (var instanceName in index.Instances.Keys)
{
if (instanceName == currentInstance.Name)
continue;
indexService.BuildIndex(repoName, indexName, instanceName, build);
}
var startTime = DateTime.Now;
while (true)
{
var tasks = indexService.GetActiveBuildTasks(index);
if (!tasks.Any() || DateTime.Now.Subtract(startTime).TotalMinutes >= 45)
break;
Task.WaitAll(new[] { Task.Delay(5000) });
}
RebuildIndexCurrentInstance(repoName, indexName, build);
});
}
Is this check OK, to stop both indexes from being locked at the same time, even if its only for miliseconds?