From 0e13f4f3f193824ce20ef128e7459124b5fba5a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damian=20B=C3=BCchel?= Date: Tue, 3 Aug 2021 14:56:58 +0200 Subject: [PATCH] SEBWIN-504: Attempt to improve server integration performance by using timer to send log events. --- SafeExamBrowser.Server/ServerProxy.cs | 118 +++++++++++++------------- 1 file changed, 60 insertions(+), 58 deletions(-) diff --git a/SafeExamBrowser.Server/ServerProxy.cs b/SafeExamBrowser.Server/ServerProxy.cs index 370007ed..cad618ed 100644 --- a/SafeExamBrowser.Server/ServerProxy.cs +++ b/SafeExamBrowser.Server/ServerProxy.cs @@ -13,7 +13,6 @@ using System.Linq; using System.Net.Http; using System.Net.Http.Headers; using System.Text; -using System.Threading; using System.Threading.Tasks; using System.Timers; using Newtonsoft.Json; @@ -36,7 +35,6 @@ namespace SafeExamBrowser.Server { private ApiVersion1 api; private AppConfig appConfig; - private CancellationTokenSource cancellationTokenSource; private FileSystem fileSystem; private string connectionToken; private int currentPowerSupplyValue; @@ -46,13 +44,13 @@ namespace SafeExamBrowser.Server private ConcurrentQueue instructionConfirmations; private ILogger logger; private ConcurrentQueue logContent; + private Timer logTimer; private Parser parser; private string oauth2Token; private int pingNumber; + private Timer pingTimer; private IPowerSupply powerSupply; private ServerSettings settings; - private Task task; - private Timer timer; private IWirelessAdapter wirelessAdapter; public event ProctoringConfigurationReceivedEventHandler ProctoringConfigurationReceived; @@ -67,15 +65,15 @@ namespace SafeExamBrowser.Server { this.api = new ApiVersion1(); this.appConfig = appConfig; - this.cancellationTokenSource = new CancellationTokenSource(); this.fileSystem = new FileSystem(appConfig, logger); this.httpClient = new HttpClient(); this.instructionConfirmations = new ConcurrentQueue(); this.logger = logger; this.logContent = new ConcurrentQueue(); + this.logTimer = new Timer(); this.parser = new Parser(logger); + this.pingTimer = new Timer(); this.powerSupply = powerSupply; - this.timer = new Timer(); this.wirelessAdapter = wirelessAdapter; } @@ -271,14 +269,16 @@ namespace SafeExamBrowser.Server } logger.Subscribe(this); - task = new Task(SendLog, cancellationTokenSource.Token); - task.Start(); + logTimer.AutoReset = false; + logTimer.Elapsed += LogTimer_Elapsed; + logTimer.Interval = 500; + logTimer.Start(); logger.Info("Started sending log items."); - timer.AutoReset = false; - timer.Elapsed += Timer_Elapsed; - timer.Interval = 1000; - timer.Start(); + pingTimer.AutoReset = false; + pingTimer.Elapsed += PingTimer_Elapsed; + pingTimer.Interval = 1000; + pingTimer.Start(); logger.Info("Started sending pings."); if (powerSupply != default(IPowerSupply) && wirelessAdapter != default(IWirelessAdapter)) @@ -299,24 +299,24 @@ namespace SafeExamBrowser.Server } logger.Unsubscribe(this); - cancellationTokenSource.Cancel(); - task?.Wait(); + logTimer.Stop(); + logTimer.Elapsed -= LogTimer_Elapsed; logger.Info("Stopped sending log items."); - timer.Stop(); - timer.Elapsed -= Timer_Elapsed; + pingTimer.Stop(); + pingTimer.Elapsed -= PingTimer_Elapsed; logger.Info("Stopped sending pings."); } - private void SendLog() + private void LogTimer_Elapsed(object sender, ElapsedEventArgs args) { - var authorization = ("Authorization", $"Bearer {oauth2Token}"); - var contentType = "application/json;charset=UTF-8"; - var token = ("SEBConnectionToken", connectionToken); - - while (!cancellationTokenSource.IsCancellationRequested) + try { - try + var authorization = ("Authorization", $"Bearer {oauth2Token}"); + var contentType = "application/json;charset=UTF-8"; + var token = ("SEBConnectionToken", connectionToken); + + while (!logContent.IsEmpty) { if (logContent.TryDequeue(out var c) && c is ILogMessage message) { @@ -331,46 +331,16 @@ namespace SafeExamBrowser.Server TryExecute(HttpMethod.Post, api.LogEndpoint, out var response, content, contentType, authorization, token); } } - catch (Exception e) - { - logger.Error("Failed to send log!", e); - } - } - } - - private void PowerSupply_StatusChanged(IPowerSupplyStatus status) - { - try - { - var value = Convert.ToInt32(status.BatteryCharge * 100); - - if (value != currentPowerSupplyValue) - { - var authorization = ("Authorization", $"Bearer {oauth2Token}"); - var chargeInfo = $"{status.BatteryChargeStatus} at {value}%"; - var contentType = "application/json;charset=UTF-8"; - var gridInfo = $"{(status.IsOnline ? "connected to" : "disconnected from")} the power grid"; - var token = ("SEBConnectionToken", connectionToken); - var json = new JObject - { - ["type"] = LogLevel.Info.ToLogType(), - ["timestamp"] = DateTime.Now.ToUnixTimestamp(), - ["text"] = $" {chargeInfo}, {status.BatteryTimeRemaining} remaining, {gridInfo}", - ["numericValue"] = value - }; - var content = json.ToString(); - - TryExecute(HttpMethod.Post, api.LogEndpoint, out var response, content, contentType, authorization, token); - currentPowerSupplyValue = value; - } } catch (Exception e) { - logger.Error("Failed to send power supply status!", e); + logger.Error("Failed to send log!", e); } + + logTimer.Start(); } - private void Timer_Elapsed(object sender, ElapsedEventArgs args) + private void PingTimer_Elapsed(object sender, ElapsedEventArgs args) { try { @@ -419,7 +389,39 @@ namespace SafeExamBrowser.Server logger.Error("Failed to send ping!", e); } - timer.Start(); + pingTimer.Start(); + } + + private void PowerSupply_StatusChanged(IPowerSupplyStatus status) + { + try + { + var value = Convert.ToInt32(status.BatteryCharge * 100); + + if (value != currentPowerSupplyValue) + { + var authorization = ("Authorization", $"Bearer {oauth2Token}"); + var chargeInfo = $"{status.BatteryChargeStatus} at {value}%"; + var contentType = "application/json;charset=UTF-8"; + var gridInfo = $"{(status.IsOnline ? "connected to" : "disconnected from")} the power grid"; + var token = ("SEBConnectionToken", connectionToken); + var json = new JObject + { + ["type"] = LogLevel.Info.ToLogType(), + ["timestamp"] = DateTime.Now.ToUnixTimestamp(), + ["text"] = $" {chargeInfo}, {status.BatteryTimeRemaining} remaining, {gridInfo}", + ["numericValue"] = value + }; + var content = json.ToString(); + + TryExecute(HttpMethod.Post, api.LogEndpoint, out var response, content, contentType, authorization, token); + currentPowerSupplyValue = value; + } + } + catch (Exception e) + { + logger.Error("Failed to send power supply status!", e); + } } private void WirelessAdapter_NetworksChanged()