// stop message publisher public async Task <IComponentHostServiceResponse <IMessagePublisherSettings> > StopMessagePublisherAsync() { var response = new ComponentHostServiceResponse <IMessagePublisherSettings>(); using (_logger.BeginScope("{Correlation}", HostInstanceId)) { if (MessagePublisher is null) { response.Set(MemberState.Error, "MessagePublisher not configured"); } else { try { await MessagePublisher.StopAsync(CancellationToken.None); response.Result = _messagePublisherSettings; } catch (Exception e) { _logger.LogError(e, "{Message} {@ObjectProperties}", "Failed to Stop MessagePublisher", Component); HostState = HostState.Errored; response.Set(MemberState.Error, "MessagePublisher failed to Stop"); } } } return(response); }
// start component public async Task <IComponentHostServiceResponse <IComponentSettings> > StartComponentAsync() { var response = new ComponentHostServiceResponse <IComponentSettings>(); using (_logger.BeginScope("{Correlation}", HostInstanceId)) { if (Component is null) { response.Set(MemberState.Error, "Component not configured"); } else { try { await Component.StartAsync(_cancellationTokenSource.Token); response.Result = _componentSettings; } catch (Exception e) { _logger.LogError(e, "{Message} {@ObjectProperties}", "Failed to Start Component", _componentSettings); HostState = HostState.Errored; response.Set(MemberState.Error, "Component failed to Start"); } } } return(response); }
public async Task <IComponentHostServiceResponse> ApplyConfigurationAsync() { var response = new ComponentHostServiceResponse(); using (_logger.BeginScope("{Correlation}", HostInstanceId)) { // if we've already started swallow the request // and inform caller that we're already running // to re-configure services the host must be stopped -> configured -> started if (HostState == HostState.Started) { response.State = MemberState.Error; response.Message = "Services running - request Stop before changing configuration"; } else { try { LoadRequestedAssemblies(); await ConfigureServicesCollectionAsync(); } catch (Exception e) { string message = "Configuration failed"; _logger.LogError(e, "{Message} {@ObjectProperties}", message, this); response.Set(MemberState.Error, message); HostState = HostState.Errored; } } } return(response); }
// stops component and message publisher public async Task <IComponentHostServiceResponse> StopAsync() { var response = new ComponentHostServiceResponse(); using (_logger.BeginScope("{Correlation}", HostInstanceId)) { if (HostState == HostState.Started) { try { // component is not null // stop component first then message publisher // this way we publish any remaining messages if (!(Component is null)) { await Component.StopAsync(CancellationToken.None); } // message publisher is not null // stop message publisher after component // this way if message publishers have // any items in queue they can push them through // in their stop method if (!(MessagePublisher is null)) { await MessagePublisher.StopAsync(CancellationToken.None); } HostState = HostState.Stopped; } catch (Exception e) { string message = "Failed to Stop members"; _logger.LogError(e, "{Message} {@ObjectProperties}", message, ComponentConfiguration); HostState = HostState.Errored; response.Set(MemberState.Error, message); } } else if (HostState != HostState.Stopped) { response.Set(MemberState.Error, $"Cannont Stop host when in {HostState.ToString()} state"); } _logger.LogInformation("{Message} {@ObjectProperties}", $"Stop request processed -- response: {response.State}", response); } return(response); }
// starts all hosted component and message publisher public async Task <IComponentHostServiceResponse> StartAsync() { var response = new ComponentHostServiceResponse(); using (_logger.BeginScope("{Correlation}", HostInstanceId)) { if (HostState == HostState.Started) { return(response); } else { try { // message publisher is not null // first start message publisher if there is one // so that we are immediately publishing when component starts if (!(MessagePublisher is null)) { await MessagePublisher.StartAsync(_cancellationTokenSource.Token); } // component is not null if (!(Component is null)) { await Component.StartAsync(_cancellationTokenSource.Token); } HostState = HostState.Started; } catch (Exception e) { _logger.LogError(e, "{Message} {@ObjectProperties}", "Failed to Start members", ComponentConfiguration); HostState = HostState.Errored; response.Set(MemberState.Error, "Failed to start members"); } } } return(response); }