From a4b237d5ccb65865255a443f44015a9d2d02b5b6 Mon Sep 17 00:00:00 2001 From: Avani Patel <32693971+avanikp@users.noreply.github.com> Date: Wed, 31 Oct 2018 12:09:34 -0700 Subject: [PATCH] Save stats for all simulations when multiple sims are running (#307) --- SimulationAgent/Agent.cs | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/SimulationAgent/Agent.cs b/SimulationAgent/Agent.cs index 39f4c9e5..a07afaee 100644 --- a/SimulationAgent/Agent.cs +++ b/SimulationAgent/Agent.cs @@ -240,28 +240,30 @@ private async Task StopInactiveSimulationsAsync(IList activeSimulati private async Task SaveSimulationStatisticsAsync(IList simulations) { - foreach (var simulation in simulations) + DateTimeOffset now = DateTimeOffset.UtcNow; + TimeSpan duration = now - this.lastSaveStatisticsTime; + + // Save statistics for simulations at specified interval + if (duration.Seconds >= SAVE_STATS_INTERVAL_SECS) { - try + foreach (var simulation in simulations) { - if (this.simulationManagers.ContainsKey(simulation.Id)) + try { - DateTimeOffset now = DateTimeOffset.UtcNow; - TimeSpan duration = now - this.lastSaveStatisticsTime; - - // Save simulation statistics at specified interval - if (duration.Seconds >= SAVE_STATS_INTERVAL_SECS) + if (this.simulationManagers.ContainsKey(simulation.Id)) { - await this.simulationManagers[simulation.Id].SaveStatisticsAsync(); - - this.lastSaveStatisticsTime = now; + { + await this.simulationManagers[simulation.Id].SaveStatisticsAsync(); + } } } + catch (Exception e) + { + this.log.Error("Failed to save simulation statistics.", () => new { simulation.Id, e }); + } } - catch (Exception e) - { - this.log.Error("Failed to save simulation statistics.", () => new { simulation.Id, e }); - } + + this.lastSaveStatisticsTime = now; } }