Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release/1.1] Prevent EdgeHub from starting without a valid configuration #5515

Merged
merged 9 commits into from
Oct 27, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ await this.currentConfig.Match(
{
Events.GettingConfig();
await pullTask;

this.currentConfig.Expect<InvalidOperationException>(() => throw new InvalidOperationException(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested text - Cannot start successfully as could not get configuration for EdgeHub.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same text that is used in later versions. Should we change those also?

"Could not obtain twin neither from local store nor from cloud. " +
"This happens when there is no upstream connection and this is the first EdgeHub startup, " +
"or there is no persistent store to save a previous twin configuration. " +
"EdgeHub cannot start without basic configuration stored in twin. Stopping now."));

return this.currentConfig;
});

Expand Down
16 changes: 13 additions & 3 deletions edge-hub/src/Microsoft.Azure.Devices.Edge.Hub.Service/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ static async Task<int> MainAsync(IConfigurationRoot configuration)
logger.LogInformation("Initializing configuration");
IConfigSource configSource = await container.Resolve<Task<IConfigSource>>();
ConfigUpdater configUpdater = await container.Resolve<Task<ConfigUpdater>>();
await configUpdater.Init(configSource);

var configDownloadTask = configUpdater.Init(configSource);

if (!Enum.TryParse(configuration.GetValue("AuthenticationMode", string.Empty), true, out AuthenticationMode authenticationMode)
|| authenticationMode != AuthenticationMode.Cloud)
Expand All @@ -107,8 +108,17 @@ static async Task<int> MainAsync(IConfigurationRoot configuration)
using (IProtocolHead protocolHead = await GetEdgeHubProtocolHeadAsync(logger, configuration, container, hosting))
using (var renewal = new CertificateRenewal(certificates, logger))
{
await protocolHead.StartAsync();
await Task.WhenAny(cts.Token.WhenCanceled(), renewal.Token.WhenCanceled());
try
{
await configDownloadTask;
await protocolHead.StartAsync();
await Task.WhenAny(cts.Token.WhenCanceled(), renewal.Token.WhenCanceled());
}
catch (Exception ex)
{
logger.LogError($"Error starting protocol heads: {ex.Message}");
}

logger.LogInformation("Stopping the protocol heads...");
await protocolHead.CloseAsync(CancellationToken.None);
logger.LogInformation("Protocol heads stopped.");
Expand Down