Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: made heartbeat client delete heartbeat after completing (MAPCO-5927) #171

Merged
merged 4 commits into from
Dec 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions MergerLogic/Clients/HeartbeatClient.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -27,14 +30,16 @@ public HeartbeatClient(ILogger<GpkgClient> 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}");
Expand All @@ -50,7 +55,23 @@ public void Stop()
}
this._logger.LogInformation($"[{MethodBase.GetCurrentMethod().Name}] Stops heartbeats for taskId={this._taskId}");
this._timer.Stop();
this._taskId = null;
try
{
string relativeUri = $"heartbeat/remove";
CL-SHLOMIKONCHA marked this conversation as resolved.
Show resolved Hide resolved
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)
{
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
{
this._taskId = null;
}
}

public void Send(object? sender, ElapsedEventArgs elapsedEventArgs)
Expand All @@ -66,7 +87,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;
}

}
}
}
}
Loading