Skip to content

Commit

Permalink
Code formating for C# codebase (Azure#677)
Browse files Browse the repository at this point in the history
  • Loading branch information
philipktlin authored and varunpuranik committed Jan 4, 2019
1 parent 82ce72e commit 027a509
Show file tree
Hide file tree
Showing 761 changed files with 19,568 additions and 15,136 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ AppPackages/
# Others
*.[Cc]ache
ClientBin/
[Ss]tyle[Cc]op.*
~$*
*~
*.dbmdl
Expand Down
853 changes: 853 additions & 0 deletions Azure.IoT.Edge.DotSettings

Large diffs are not rendered by default.

117 changes: 9 additions & 108 deletions Microsoft.Azure.Devices.Edge.sln.DotSettings

Large diffs are not rendered by default.

245 changes: 134 additions & 111 deletions edge-agent/src/Microsoft.Azure.Devices.Edge.Agent.Core/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ namespace Microsoft.Azure.Devices.Edge.Agent.Core
public class Agent
{
const string StoreConfigKey = "CurrentConfig";
IEnvironment environment;
readonly IPlanner planner;
readonly IPlanRunner planRunner;
readonly IReporter reporter;
Expand All @@ -28,12 +27,18 @@ public class Agent
readonly AsyncLock reconcileLock = new AsyncLock();
readonly ISerde<DeploymentConfigInfo> deploymentConfigInfoSerde;
readonly IEncryptionProvider encryptionProvider;
IEnvironment environment;
DeploymentConfigInfo currentConfig;

public Agent(IConfigSource configSource, IEnvironmentProvider environmentProvider,
IPlanner planner, IPlanRunner planRunner, IReporter reporter,
public Agent(
IConfigSource configSource,
IEnvironmentProvider environmentProvider,
IPlanner planner,
IPlanRunner planRunner,
IReporter reporter,
IModuleIdentityLifecycleManager moduleIdentityLifecycleManager,
IEntityStore<string, string> configStore, DeploymentConfigInfo initialDeployedConfigInfo,
IEntityStore<string, string> configStore,
DeploymentConfigInfo initialDeployedConfigInfo,
ISerde<DeploymentConfigInfo> deploymentConfigInfoSerde,
IEncryptionProvider encryptionProvider)
{
Expand All @@ -51,9 +56,16 @@ public Agent(IConfigSource configSource, IEnvironmentProvider environmentProvide
Events.AgentCreated();
}

public static async Task<Agent> Create(IConfigSource configSource, IPlanner planner, IPlanRunner planRunner, IReporter reporter,
IModuleIdentityLifecycleManager moduleIdentityLifecycleManager, IEnvironmentProvider environmentProvider,
IEntityStore<string, string> configStore, ISerde<DeploymentConfigInfo> deploymentConfigInfoSerde, IEncryptionProvider encryptionProvider)
public static async Task<Agent> Create(
IConfigSource configSource,
IPlanner planner,
IPlanRunner planRunner,
IReporter reporter,
IModuleIdentityLifecycleManager moduleIdentityLifecycleManager,
IEnvironmentProvider environmentProvider,
IEntityStore<string, string> configStore,
ISerde<DeploymentConfigInfo> deploymentConfigInfoSerde,
IEncryptionProvider encryptionProvider)
{
Preconditions.CheckNotNull(deploymentConfigInfoSerde, nameof(deploymentConfigInfoSerde));
Preconditions.CheckNotNull(configStore, nameof(configStore));
Expand All @@ -62,85 +74,30 @@ public static async Task<Agent> Create(IConfigSource configSource, IPlanner plan
try
{
Option<string> deploymentConfigInfoJson = await Preconditions.CheckNotNull(configStore, nameof(configStore)).Get(StoreConfigKey);
await deploymentConfigInfoJson.ForEachAsync(async json =>
{
string decryptedJson = await encryptionProvider.DecryptAsync(json);
deploymentConfigInfo = Option.Some(deploymentConfigInfoSerde.Deserialize(decryptedJson));
});
await deploymentConfigInfoJson.ForEachAsync(
async json =>
{
string decryptedJson = await encryptionProvider.DecryptAsync(json);
deploymentConfigInfo = Option.Some(deploymentConfigInfoSerde.Deserialize(decryptedJson));
});
}
catch (Exception ex) when (!ex.IsFatal())
{
Events.ErrorDeserializingConfig(ex);
}
var agent = new Agent(configSource, environmentProvider, planner, planRunner, reporter, moduleIdentityLifecycleManager,
configStore, deploymentConfigInfo.GetOrElse(DeploymentConfigInfo.Empty), deploymentConfigInfoSerde, encryptionProvider);
return agent;
}

async Task<(ModuleSet current, Exception ex)> GetCurrentModuleSetAsync(CancellationToken token)
{
ModuleSet current = null;
Exception ex = null;

try
{
current = await this.environment.GetModulesAsync(token);
}
catch (Exception e) when (!e.IsFatal())
{
ex = e;
}

return (current, ex);
}

async Task<(DeploymentConfigInfo deploymentConfigInfo, Exception ex)> GetDeploymentConfigInfoAsync()
{
DeploymentConfigInfo deploymentConfigInfo = null;
Exception ex = null;

try
{
Events.GettingDeploymentConfigInfo();
deploymentConfigInfo = await this.configSource.GetDeploymentConfigInfoAsync();
Events.ObtainedDeploymentConfigInfo(deploymentConfigInfo);
}
catch (Exception e) when (!e.IsFatal())
{
ex = e;
}

return (deploymentConfigInfo, ex);
}

async Task<(ModuleSet current, DeploymentConfigInfo DeploymentConfigInfo, Exception ex)> GetReconcileData(CancellationToken token)
{
// we read the data from the config source and from the environment separately because
// when doing something like TaskEx.WhenAll(t1, t2) if either of them throws then we get
// nothing; so for example, if the environment is able to successfully retrieve the moduleset
// but there's a corrupt deployment in IoT Hub then we end up not being able to report the
// current state even though we have it

((ModuleSet current, Exception environmentException), (DeploymentConfigInfo deploymentConfigInfo, Exception configSourceException)) = await TaskEx.WhenAll(
this.GetCurrentModuleSetAsync(token), this.GetDeploymentConfigInfoAsync()
);

List<Exception> exceptions = new[]
{
environmentException,
configSourceException,
deploymentConfigInfo?.Exception.OrDefault()
}
.Where(e => e != null)
.ToList();

Exception exception = null;
if (exceptions.Any())
{
exception = exceptions.Count > 1 ? new AggregateException(exceptions) : exceptions[0];
}

return (current, deploymentConfigInfo, exception);
var agent = new Agent(
configSource,
environmentProvider,
planner,
planRunner,
reporter,
moduleIdentityLifecycleManager,
configStore,
deploymentConfigInfo.GetOrElse(DeploymentConfigInfo.Empty),
deploymentConfigInfoSerde,
encryptionProvider);
return agent;
}

public async Task ReconcileAsync(CancellationToken token)
Expand All @@ -165,7 +122,7 @@ public async Task ReconcileAsync(CancellationToken token)
// TODO - Update this logic to create identities only when needed, in the Command factory, instead of creating all the identities
// up front here. That will allow handling the case when only the state of the system has changed (say one module crashes), and
// no new identities need to be created. This will simplify the logic to allow EdgeAgent to work when offline.
// But that required ModuleSet.Diff to be updated to include modules updated by deployment, and modules updated by state change.
// But that required ModuleSet.Diff to be updated to include modules updated by deployment, and modules updated by state change.
IImmutableDictionary<string, IModuleIdentity> identities = await this.moduleIdentityLifecycleManager.GetModuleIdentitiesAsync(desiredModuleSet, current);
Plan plan = await this.planner.PlanAsync(desiredModuleSet, current, deploymentConfig.Runtime, identities);
if (!plan.IsEmpty)
Expand Down Expand Up @@ -217,20 +174,11 @@ public async Task ReconcileAsync(CancellationToken token)
break;
}
}

await this.reporter.ReportAsync(token, moduleSetToReport, await this.environment.GetRuntimeInfoAsync(), this.currentConfig.Version, status);
}
}

// This should be called only within the reconcile lock.
async Task UpdateCurrentConfig(DeploymentConfigInfo deploymentConfigInfo)
{
this.environment = this.environmentProvider.Create(deploymentConfigInfo.DeploymentConfig);
this.currentConfig = deploymentConfigInfo;

string encryptedConfig = await this.encryptionProvider.EncryptAsync(this.deploymentConfigInfoSerde.Serialize(deploymentConfigInfo));
await this.configStore.Put(StoreConfigKey, encryptedConfig);
}

public async Task HandleShutdown(CancellationToken token)
{
try
Expand All @@ -248,6 +196,95 @@ public async Task HandleShutdown(CancellationToken token)
}
}

internal async Task ReportShutdownAsync(CancellationToken token)
{
try
{
var status = new DeploymentStatus(DeploymentStatusCode.Unknown, "Agent is not running");
await this.reporter.ReportShutdown(status, token);
Events.ReportShutdown();
}
catch (Exception ex) when (!ex.IsFatal())
{
Events.ReportShutdownFailed(ex);
}
}

async Task<(ModuleSet current, Exception ex)> GetCurrentModuleSetAsync(CancellationToken token)
{
ModuleSet current = null;
Exception ex = null;

try
{
current = await this.environment.GetModulesAsync(token);
}
catch (Exception e) when (!e.IsFatal())
{
ex = e;
}

return (current, ex);
}

async Task<(DeploymentConfigInfo deploymentConfigInfo, Exception ex)> GetDeploymentConfigInfoAsync()
{
DeploymentConfigInfo deploymentConfigInfo = null;
Exception ex = null;

try
{
Events.GettingDeploymentConfigInfo();
deploymentConfigInfo = await this.configSource.GetDeploymentConfigInfoAsync();
Events.ObtainedDeploymentConfigInfo(deploymentConfigInfo);
}
catch (Exception e) when (!e.IsFatal())
{
ex = e;
}

return (deploymentConfigInfo, ex);
}

async Task<(ModuleSet current, DeploymentConfigInfo DeploymentConfigInfo, Exception ex)> GetReconcileData(CancellationToken token)
{
// we read the data from the config source and from the environment separately because
// when doing something like TaskEx.WhenAll(t1, t2) if either of them throws then we get
// nothing; so for example, if the environment is able to successfully retrieve the moduleset
// but there's a corrupt deployment in IoT Hub then we end up not being able to report the
// current state even though we have it
((ModuleSet current, Exception environmentException), (DeploymentConfigInfo deploymentConfigInfo, Exception configSourceException)) = await TaskEx.WhenAll(
this.GetCurrentModuleSetAsync(token),
this.GetDeploymentConfigInfoAsync());

List<Exception> exceptions = new[]
{
environmentException,
configSourceException,
deploymentConfigInfo?.Exception.OrDefault()
}
.Where(e => e != null)
.ToList();

Exception exception = null;
if (exceptions.Any())
{
exception = exceptions.Count > 1 ? new AggregateException(exceptions) : exceptions[0];
}

return (current, deploymentConfigInfo, exception);
}

// This should be called only within the reconcile lock.
async Task UpdateCurrentConfig(DeploymentConfigInfo deploymentConfigInfo)
{
this.environment = this.environmentProvider.Create(deploymentConfigInfo.DeploymentConfig);
this.currentConfig = deploymentConfigInfo;

string encryptedConfig = await this.encryptionProvider.EncryptAsync(this.deploymentConfigInfoSerde.Serialize(deploymentConfigInfo));
await this.configStore.Put(StoreConfigKey, encryptedConfig);
}

async Task ShutdownModules(CancellationToken token)
{
try
Expand All @@ -269,24 +306,10 @@ async Task ShutdownModules(CancellationToken token)
}
}

internal async Task ReportShutdownAsync(CancellationToken token)
{
try
{
var status = new DeploymentStatus(DeploymentStatusCode.Unknown, "Agent is not running");
await this.reporter.ReportShutdown(status, token);
Events.ReportShutdown();
}
catch (Exception ex) when (!ex.IsFatal())
{
Events.ReportShutdownFailed(ex);
}
}

static class Events
{
static readonly ILogger Log = Logger.Factory.CreateLogger<Agent>();
const int IdStart = AgentEventIds.Agent;
static readonly ILogger Log = Logger.Factory.CreateLogger<Agent>();

enum EventIds
{
Expand Down Expand Up @@ -366,11 +389,6 @@ public static void ObtainedDeploymentConfigInfo(DeploymentConfigInfo deploymentC
}
}

internal static void ErrorDeserializingConfig(Exception ex)
{
Log.LogWarning((int)EventIds.ErrorDeserializingConfig, ex, "There was an error deserializing stored deployment configuration information");
}

public static void InitiateShutdown()
{
Log.LogInformation((int)EventIds.InitiateShutdown, "Initiating shutdown cleanup.");
Expand All @@ -395,6 +413,11 @@ public static void ShutdownModulesFailed(Exception ex)
{
Log.LogWarning((int)EventIds.StopModulesFailed, ex, "Error while stopping all modules.");
}

internal static void ErrorDeserializingConfig(Exception ex)
{
Log.LogWarning((int)EventIds.ErrorDeserializingConfig, ex, "There was an error deserializing stored deployment configuration information");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ namespace Microsoft.Azure.Devices.Edge.Agent.Core
{
public struct AgentEventIds
{
const int EventIdStart = 100000;
public const int Agent = EventIdStart;
public const int FileConfigSource = EventIdStart + 100;
public const int TwinConfigSource = EventIdStart + 200;
Expand All @@ -21,5 +20,6 @@ public struct AgentEventIds
public const int OrderedRetryPlanRunner = EventIdStart + 1400;
public const int ModuleManagementHttpClient = EventIdStart + 1500;
public const int ModuleIdentityLifecycleManager = EventIdStart + 1600;
const int EventIdStart = 100000;
}
}
Loading

0 comments on commit 027a509

Please sign in to comment.