From 29742ebd31855264cecaaa6c3a20540a92ea7d05 Mon Sep 17 00:00:00 2001 From: Nitzan Morr Date: Mon, 23 Dec 2024 10:00:41 +0200 Subject: [PATCH 1/4] fix: made heartbeat client delete heartbeat after completing --- MergerLogic/Clients/HeartbeatClient.cs | 37 +++++++++++++++++++------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/MergerLogic/Clients/HeartbeatClient.cs b/MergerLogic/Clients/HeartbeatClient.cs index dcffb61a..715cb84c 100644 --- a/MergerLogic/Clients/HeartbeatClient.cs +++ b/MergerLogic/Clients/HeartbeatClient.cs @@ -1,7 +1,10 @@ using MergerLogic.Utils; using Microsoft.Extensions.Logging; +using Newtonsoft.Json; using System.Reflection; +using System.Text; using System.Timers; +using System.Net.Http.Json; namespace MergerLogic.Clients { @@ -27,14 +30,16 @@ public HeartbeatClient(ILogger logger, IConfigurationManager configu this._timer.Elapsed += this.Send; } - ~HeartbeatClient() { + ~HeartbeatClient() + { this._timer.Elapsed -= this.Send; this._timer.Dispose(); } public void Start(string taskId) { - if (this._timer.Enabled) { + if (this._timer.Enabled) + { this.Stop(); } this._logger.LogInformation($"[{MethodBase.GetCurrentMethod().Name}] Starting heartbeat for task={taskId}"); @@ -44,13 +49,28 @@ public void Start(string taskId) public void Stop() { - if (!this._timer.Enabled) + try { - throw new Exception($"[{MethodBase.GetCurrentMethod().Name}] Heartbeat interval must be running in order to stop it."); + string relativeUri = $"heartbeat/remove"; + string url = new Uri(new Uri(this._baseUrl), relativeUri).ToString(); + var content = JsonContent.Create(new[] { this._taskId }); + this._httpClient.PostData(url, content); + } + catch (Exception e) + { + this._logger.LogError($"[{MethodBase.GetCurrentMethod().Name}] Could not delete heartbeat for task={this._taskId}, {e.Message}"); + throw; + } + finally + { + if (!this._timer.Enabled) + { + throw new Exception($"[{MethodBase.GetCurrentMethod().Name}] Heartbeat interval must be running in order to stop it."); + } + this._logger.LogInformation($"[{MethodBase.GetCurrentMethod().Name}] Stops heartbeats for taskId={this._taskId}"); + this._timer.Stop(); + this._taskId = null; } - this._logger.LogInformation($"[{MethodBase.GetCurrentMethod().Name}] Stops heartbeats for taskId={this._taskId}"); - this._timer.Stop(); - this._taskId = null; } public void Send(object? sender, ElapsedEventArgs elapsedEventArgs) @@ -66,7 +86,6 @@ public void Send(object? sender, ElapsedEventArgs elapsedEventArgs) this._logger.LogError($"[{MethodBase.GetCurrentMethod().Name}] Could not send heartbeat for task={this._taskId}, {e.Message}"); throw; } - } } -} +} \ No newline at end of file From f28fcaaf474a162f0838ffd3a266be60dff6f33e Mon Sep 17 00:00:00 2001 From: Nitzan Morr Date: Tue, 24 Dec 2024 10:30:06 +0200 Subject: [PATCH 2/4] fix: moved timer stop to the beggining of the Stop function --- MergerLogic/Clients/HeartbeatClient.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/MergerLogic/Clients/HeartbeatClient.cs b/MergerLogic/Clients/HeartbeatClient.cs index 715cb84c..4bceaca6 100644 --- a/MergerLogic/Clients/HeartbeatClient.cs +++ b/MergerLogic/Clients/HeartbeatClient.cs @@ -49,6 +49,12 @@ public void Start(string taskId) public void Stop() { + if (!this._timer.Enabled) + { + throw new Exception($"[{MethodBase.GetCurrentMethod().Name}] Heartbeat interval must be running in order to stop it."); + } + this._logger.LogInformation($"[{MethodBase.GetCurrentMethod().Name}] Stops heartbeats for taskId={this._taskId}"); + this._timer.Stop(); try { string relativeUri = $"heartbeat/remove"; @@ -63,12 +69,6 @@ public void Stop() } finally { - if (!this._timer.Enabled) - { - throw new Exception($"[{MethodBase.GetCurrentMethod().Name}] Heartbeat interval must be running in order to stop it."); - } - this._logger.LogInformation($"[{MethodBase.GetCurrentMethod().Name}] Stops heartbeats for taskId={this._taskId}"); - this._timer.Stop(); this._taskId = null; } } From 078001c12386a4f081a67526dde6016a8d332392 Mon Sep 17 00:00:00 2001 From: Nitzan Morr Date: Tue, 24 Dec 2024 11:16:02 +0200 Subject: [PATCH 3/4] fix: improve error logging in HeartbeatClient by including exception details --- MergerLogic/Clients/HeartbeatClient.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/MergerLogic/Clients/HeartbeatClient.cs b/MergerLogic/Clients/HeartbeatClient.cs index 4bceaca6..f21b2d96 100644 --- a/MergerLogic/Clients/HeartbeatClient.cs +++ b/MergerLogic/Clients/HeartbeatClient.cs @@ -64,8 +64,9 @@ public void Stop() } catch (Exception e) { - this._logger.LogError($"[{MethodBase.GetCurrentMethod().Name}] Could not delete heartbeat for task={this._taskId}, {e.Message}"); - throw; + string message = ($"[{MethodBase.GetCurrentMethod().Name}] Could not delete heartbeat for task={this._taskId}, {e.Message}"); + this._logger.LogError(message); + throw new Exception(message, e); } finally { From 9253948fe0b7c22893dd82b05d8ceee9651f52c6 Mon Sep 17 00:00:00 2001 From: Nitzan Morr Date: Tue, 24 Dec 2024 11:58:34 +0200 Subject: [PATCH 4/4] fix: remove parentheses --- MergerLogic/Clients/HeartbeatClient.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MergerLogic/Clients/HeartbeatClient.cs b/MergerLogic/Clients/HeartbeatClient.cs index f21b2d96..4160e4e6 100644 --- a/MergerLogic/Clients/HeartbeatClient.cs +++ b/MergerLogic/Clients/HeartbeatClient.cs @@ -64,7 +64,7 @@ public void Stop() } catch (Exception e) { - string message = ($"[{MethodBase.GetCurrentMethod().Name}] Could not delete heartbeat for task={this._taskId}, {e.Message}"); + string message = $"[{MethodBase.GetCurrentMethod().Name}] Could not delete heartbeat for task={this._taskId}, {e.Message}"; this._logger.LogError(message); throw new Exception(message, e); }