Пример #1
0
        public async Task ProcessContentItemsAsync()
        {
            // TODO: Lock over the filesystem

            var allIndexes = new Dictionary <string, int>();

            // Find the lowest task id to process
            int lastTaskId = int.MaxValue;

            foreach (var indexName in _indexManager.List())
            {
                var taskId = _indexingState.GetLastTaskId(indexName);
                lastTaskId = Math.Min(lastTaskId, taskId);
                allIndexes.Add(indexName, taskId);
            }

            if (!allIndexes.Any())
            {
                return;
            }

            IEnumerable <IndexingTask> batch;

            do
            {
                // Load the next batch of tasks
                batch = (await _indexTaskManager.GetIndexingTasksAsync(lastTaskId, BatchSize)).ToArray();

                if (!batch.Any())
                {
                    break;
                }

                foreach (var task in batch)
                {
                    foreach (var index in allIndexes)
                    {
                        // TODO: ignore if this index is not configured for the content type

                        if (index.Value < task.Id)
                        {
                            _indexManager.DeleteDocuments(index.Key, new string[] { task.ContentItemId });
                        }
                    }

                    if (task.Type == IndexingTaskTypes.Update)
                    {
                        var contentItem = await _contentManager.GetAsync(task.ContentItemId);

                        var context = new BuildIndexContext(new DocumentIndex(task.ContentItemId), contentItem, contentItem.ContentType);

                        // Update the document from the index if its lastIndexId is smaller than the current task id.
                        await _indexHandlers.InvokeAsync(x => x.BuildIndexAsync(context), Logger);

                        foreach (var index in allIndexes)
                        {
                            if (index.Value < task.Id)
                            {
                                _indexManager.StoreDocuments(index.Key, new DocumentIndex[] { context.DocumentIndex });
                            }
                        }
                    }
                }

                // Update task ids
                lastTaskId = batch.Last().Id;

                foreach (var index in allIndexes)
                {
                    if (index.Value < lastTaskId)
                    {
                        _indexingState.SetLastTaskId(index.Key, lastTaskId);
                    }
                }

                _indexingState.Update();
            } while (batch.Count() == BatchSize);
        }
Пример #2
0
        public async Task ProcessContentItemsAsync()
        {
            // TODO: Lock over the filesystem in case two instances get a command to rebuild the index concurrently.

            var allIndices = new Dictionary <string, int>();

            // Find the lowest task id to process
            int lastTaskId = int.MaxValue;

            foreach (var indexName in _indexManager.List())
            {
                var taskId = _indexingState.GetLastTaskId(indexName);
                lastTaskId = Math.Min(lastTaskId, taskId);
                allIndices.Add(indexName, taskId);
            }

            if (!allIndices.Any())
            {
                return;
            }

            IndexingTask[] batch;

            var shellContext = _shellHost.GetOrCreateShellContext(_shellSettings);

            do
            {
                // Create a scope for the content manager
                using (var scope = shellContext.EnterServiceScope())
                {
                    // Load the next batch of tasks
                    batch = (await _indexingTaskManager.GetIndexingTasksAsync(lastTaskId, BatchSize)).ToArray();

                    if (!batch.Any())
                    {
                        break;
                    }

                    foreach (var task in batch)
                    {
                        var contentManager = scope.ServiceProvider.GetRequiredService <IContentManager>();
                        var indexHandlers  = scope.ServiceProvider.GetServices <IContentItemIndexHandler>();

                        foreach (var index in allIndices)
                        {
                            // TODO: ignore if this index is not configured for the content type

                            if (index.Value < task.Id)
                            {
                                _indexManager.DeleteDocuments(index.Key, new string[] { task.ContentItemId });
                            }
                        }

                        if (task.Type == IndexingTaskTypes.Update)
                        {
                            var contentItem = await contentManager.GetAsync(task.ContentItemId);

                            var context = new BuildIndexContext(new DocumentIndex(task.ContentItemId), contentItem, contentItem.ContentType);

                            // Update the document from the index if its lastIndexId is smaller than the current task id.
                            await indexHandlers.InvokeAsync(x => x.BuildIndexAsync(context), Logger);

                            foreach (var index in allIndices)
                            {
                                if (index.Value < task.Id)
                                {
                                    _indexManager.StoreDocuments(index.Key, new DocumentIndex[] { context.DocumentIndex });
                                }
                            }
                        }
                    }


                    // Update task ids
                    lastTaskId = batch.Last().Id;

                    foreach (var index in allIndices)
                    {
                        if (index.Value < lastTaskId)
                        {
                            _indexingState.SetLastTaskId(index.Key, lastTaskId);
                        }
                    }

                    _indexingState.Update();
                }
            } while (batch.Length == BatchSize);
        }