protected override async Task ExecuteAsync(CancellationToken stoppingToken) { _logger.LogInformation("Starting route network event consumer worker at: {time}", DateTimeOffset.Now); try { if (String.IsNullOrEmpty(_kafkaSetting.RouteNetworkEventTopic)) { throw new ApplicationException("RouteNetworkEventTopic Kafka app setting must be set!"); } var toposConfig = _eventDispatcher.Config("route_network_event_" + Guid.NewGuid(), c => { var kafkaConfig = c.UseKafka(_kafkaSetting.Server); if (_kafkaSetting.CertificateFilename != null) { kafkaConfig.WithCertificate(_kafkaSetting.CertificateFilename); } }) .Logging(l => l.UseSerilog()) .Positions(p => p.StoreInMemory(_positionsStorage)) .Topics(t => t.Subscribe(_kafkaSetting.RouteNetworkEventTopic)); _eventDispatcher.OnEvent.Subscribe(_routeNetworkEventHandler); _kafkaConsumer = toposConfig.Start(); var inMemRouteNetworkState = (InMemRouteNetworkState)_routeNetworkState; // Wait for load mode to create an initial version/state _logger.LogInformation("Starting route network events load mode..."); bool loadFinish = false; if (loadFinish) { _logger.LogError("NOTICE THAT WAITING FOR ROUTE EVENTS LOADING IS DISABLED!!!"); } while (!stoppingToken.IsCancellationRequested && !loadFinish) { _logger.LogDebug("Waiting for load mode to finish creating initial state..."); _logger.LogInformation($"{inMemRouteNetworkState.NumberOfObjectsLoaded} route network Kafka events processed."); DateTime waitStartTimestamp = DateTime.UtcNow; await Task.Delay(5000, stoppingToken); TimeSpan timespan = waitStartTimestamp - inMemRouteNetworkState.LastEventRecievedTimestamp; if (timespan.TotalSeconds > 20) { loadFinish = true; } } ((InMemRouteNetworkState)_routeNetworkState).FinishLoadMode(); _logger.LogInformation("Loading of initial route network state finished."); // Dehydrate projections LogMenUsage(); _logger.LogInformation("Start dehydrate in-memory projections..."); _eventStore.DehydrateProjections(); _logger.LogInformation("Finish dehydrating in-memory projections."); LogMenUsage(); // We are now ready to serve the public if the loaded objects are bigger than 0 if (inMemRouteNetworkState.NumberOfObjectsLoaded > 0) { File.Create("/tmp/healthy"); _logger.LogInformation("Healhty file written writen to tmp."); } else { throw new ApplicationException("Recieved no route network elements from Kafka topic."); } // Start conversion new ConversionRunner(_loggerFactory, _eventStore, _geoDatabaseSetting, _commandDispatcher, _queryDispatcher).Run(); } catch (Exception ex) { _logger.LogError(ex, ex.Message); } await Task.CompletedTask; }