private async Task CallElasticsearch() { var host = new Uri("http://" + Host() + ":9200"); var settings = new ConnectionSettings(host).DefaultIndex("elastic-net-example"); var elastic = new ElasticClient(settings); await elastic.ClusterHealthAsync(new ClusterHealthRequest()); await elastic.ClusterStateAsync(new ClusterStateRequest()); }
protected override async Task <bool> PerformCheck(ElasticsearchContainer container, CancellationToken cancellationToken) { var elastic = new ElasticClient(new Uri(container.GetUrl())); var health = await elastic.ClusterHealthAsync( ch => ch .WaitForStatus(WaitForStatus.Yellow) .Level(Level.Cluster) .ErrorTrace(true), cancellationToken); Logger?.LogDebug(health.DebugInformation); return(health.IsValid); }
private static List <Func <object> > ClusterCommandsAsync(ElasticClient elastic) { return(new List <Func <object> > { () => elastic.ClusterAllocationExplainAsync(new ClusterAllocationExplainRequest()), () => elastic.ClusterGetSettingsAsync(new ClusterGetSettingsRequest()), () => elastic.ClusterHealthAsync(new ClusterHealthRequest()), () => elastic.ClusterPendingTasksAsync(new ClusterPendingTasksRequest()), () => elastic.ClusterPutSettingsAsync(new ClusterPutSettingsRequest()), () => elastic.ClusterRerouteAsync(new ClusterRerouteRequest()), () => elastic.ClusterStateAsync(new ClusterStateRequest()), () => elastic.ClusterStatsAsync(new ClusterStatsRequest()), }); }
public async Task <bool> Wait(ElasticsearchContainer container, CancellationToken cancellationToken = default) { if (container == null) { new ArgumentNullException(nameof(container)); } var elastic = new ElasticClient(new Uri(container.GetUrl())); var health = await elastic.ClusterHealthAsync(ch => ch .WaitForStatus(WaitForStatus.Yellow) .Level(Level.Cluster) .ErrorTrace(true)); _logger?.LogDebug(health.DebugInformation); return(health.IsValid); }
// returns a wait time so that certain actions cause a longer delay before polling again public async Task <int> Run() { string messageId = null; // useful to have in scope here so we can use it in exception catching _logger.LogDebug("App.Run"); var processQueue = _configuration["rsmq:queueName"]; // Check Elastic Search status try { _logger.LogDebug("checking Elastic Search status"); var response = await _elastic.ClusterHealthAsync( new ClusterHealthRequest { WaitForStatus = WaitForStatus.Yellow }); if (response.TimedOut || !response.IsValid) { throw new ApplicationException(); } } catch (Exception e) { _logger.LogWarning("Elastic Search server doesn't seem to be available"); _logger.LogError(e.Message); return(_configuration.GetValue <int>("intervalMs")); // wait before re-polling } // Ensure the Message Queue API is up, and the queue we want exists if (!await QueueReady(processQueue)) { return(_configuration.GetValue <int>("intervalMs")); // wait before re-polling } // now continue with our work _logger.LogDebug("Checking for messages..."); try { var message = await _redis.ReceiveMessage(processQueue); if (!string.IsNullOrWhiteSpace(message.Id)) { messageId = message.Id; _logger.LogInformation($"Message received: {message.Id}"); // process it // TODO if we support multiple file types // we'll need a way to DI the correct processor // but for now just CSV so it's ok var ext = Path.GetExtension(message.Message); if (Array.FindIndex( _configuration["fileExtensions"].Split(), x => x == ext) < 0) { _logger.LogInformation($"unsupported file type detected: {message.Message}"); Utils.MoveFile(PathTypes.Queued, PathTypes.BadType, message.Message, _logger); } else { // TODO really processors should work on streams // so the ES bit is in App, and not recreated by every processor? // but no time for that yet await _fileProcessor.Process(message.Message); } } else { // no message right now return(_configuration.GetValue <int>("intervalMs")); // wait before re-polling } } catch (Exception e) { _logger.LogError(e.Message); } // try and delete the message (if any) regardless of the outcome above try { if (!string.IsNullOrWhiteSpace(messageId)) { await _redis.DeleteMessage(processQueue, messageId); } } catch (Exception e) { _logger.LogError(e.Message); } return(0); //re-poll immediately after actual work }