public async Task ShutdownAsync() { foreach (var serverId in _ServerIds) { await _ServiceDiscovery.DeregisterService(serverId); } await _ServerInstance.ShutdownAsync(); ServerRunning = false; }
static void Shutdown() { if (_serviceDiscovery == null) { return; } if (!ShuttingDown) { ShuttingDown = true; foreach (var kv in servicesDict) { _serviceDiscovery.DeregisterService(kv.Value); } } }
internal async Task <bool> Start(HostControl hostControl) { _hostControl = hostControl; _log.Info("Starting Hosted Microservice..."); int retries = MaxRetries; while (retries > 0) { var childKernel = new ChildKernel(_kernel); var configuration = childKernel.Get <HostedServiceConfiguration>(); var startup = childKernel.Get <Startup>(); var deferredRegistration = await _discovery.RegisterServiceDeferred(); if (configuration.RestApiPort > 0) { var apiUri = $"http://{configuration.RestApiHostname}:{configuration.RestApiPort}"; try { _app = WebApp.Start(apiUri, startup.Configuration); _log.Info($"Started API at {apiUri}"); _registration = await _discovery.RegisterService(deferredRegistration); MonitorServiceRegistration(_cancellationTokenSource.Token); _inServiceWorkers = childKernel.GetAll <IInServiceWorker>().ToArray(); if (_inServiceWorkers.Any()) { _inServiceWorkers.ToList().ForEach(worker => worker.Start(_cancellationTokenSource.Token)); _log.Info($"Startd {_inServiceWorkers.Count()} in service workers"); } return(true); } catch (Exception ex) { _log.Warn($"Failed to start the API host on {apiUri}: {ex.Message}"); while (ex.InnerException != null) { _log.Debug(ex.Message); ex = ex.InnerException; } if (_app != null) { _app.Dispose(); _app = null; } _log.Info($"Retrying...({MaxRetries - retries}/{MaxRetries})"); configuration.RestApiPort = 0; retries--; } } else { retries = 0; } await Task.Delay(RetryPeriodInSeconds * 1000, _cancellationTokenSource.Token); } _discovery.DeregisterService(_registration); _log.Fatal($"Could not start API due to one or more fatal errors"); return(false); }