From 31fdefb0388ef80212367a60c414b84ed7f29350 Mon Sep 17 00:00:00 2001 From: Niko Ehrenfeuchter <nikolaus.ehrenfeuchter@unibas.ch> Date: Tue, 13 Mar 2018 00:30:23 +0100 Subject: [PATCH] Prototype using event handlers for reacting on CPU load changes. Refers to #36 --- ATxService/AutoTx.cs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/ATxService/AutoTx.cs b/ATxService/AutoTx.cs index 53cf4a2..92e4468 100644 --- a/ATxService/AutoTx.cs +++ b/ATxService/AutoTx.cs @@ -45,6 +45,7 @@ namespace ATxService private int _txCurFileProgress; private int _waitCyclesBeforeNextTx; private Cpu _cpu; + private bool _cpuLoadHigh; private DateTime _lastUserDirCheck = DateTime.MinValue; @@ -98,6 +99,8 @@ namespace ATxService Probation = 16, Enabled = true }; + _cpu.LoadAboveLimit += OnLoadAboveLimit; + _cpu.LoadBelowLimit += OnLoadBelowLimit; } catch (UnauthorizedAccessException ex) { Log.Error("Not enough permissions to monitor the CPU load.\nMake sure the " + @@ -518,6 +521,22 @@ namespace ATxService #region general methods + /// <summary> + /// Event handler for CPU load dropping below the configured limit. + /// </summary> + private void OnLoadBelowLimit(object sender, EventArgs e) { + Log.Warn("CPU load is below given limit."); + _cpuLoadHigh = false; + } + + /// <summary> + /// Event handler for CPU load exceeding the configured limit. + /// </summary> + private void OnLoadAboveLimit(object sender, EventArgs e) { + Log.Warn("High CPU load detected!"); + _cpuLoadHigh = true; + } + /// <summary> /// Check system parameters for valid ranges and update the global service state accordingly. /// </summary> @@ -526,7 +545,7 @@ namespace ATxService // check all system parameters for valid ranges and remember the reason in a string // if one of them is failing (to report in the log why we're suspended) - if (_cpu.Load() >= _config.MaxCpuUsage) + if (_cpuLoadHigh) limitReason = "CPU usage"; else if (SystemChecks.GetFreeMemory() < _config.MinAvailableMemory) limitReason = "RAM usage"; -- GitLab