public async Task AppendDocumentAsync(EssayDocument request) { _logger.LogInformation("appending document: " + new { TermCount = request.Terms.Count }); var client = _setup.GetClient(); var result = await client.IndexAsync(request); if (!result.IsValid) { _logger.LogError("Document indexing operation is reported as invalid."); } }
public async Task RunAsync() { _logger.LogInformation("Indexing started."); _logger.LogInformation("Insuring index"); await _indexer.ReCreateIndexAsync(); var noMoreThanFewParallelRequests = new SemaphoreSlim(MaxParallelRequests, MaxParallelRequests); var allTasks = new ConcurrentDictionary <Guid, Task>(); foreach (var combination in GetRandomThemeCombinations().Take(RequiredCount)) { var termList = combination.Select(i => i.IdentifierName).ToList(); _logger.LogTrace($"Parallel op semaphore count: {noMoreThanFewParallelRequests.CurrentCount}"); await noMoreThanFewParallelRequests.WaitAsync(); var opId = Guid.NewGuid(); var task = new Task(async() => { try { // Assuming we need an external id. var externalDocId = Guid.NewGuid(); var generated = await _boolshitEssayGenerator.GenerateByTermsAsync(termList); var indexRequest = new EssayDocument(externalDocId, generated.Title, generated.RawBody, termList); await _indexer.AppendDocumentAsync(indexRequest); } catch (Exception e) { _logger.LogError("Error seeding essay index.", e); } finally { noMoreThanFewParallelRequests.Release(); allTasks.TryRemove(opId, out Task unused); } }); allTasks.TryAdd(opId, task); task.Start(); } List <Task> leftTasks; while ((leftTasks = allTasks.Values.ToList()).Count > 0) { await Task.WhenAll(leftTasks); } }