Skip to content

Commit

Permalink
pushing scheduled properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Scooletz committed Sep 3, 2024
1 parent fed5a86 commit b38f2dd
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 21 deletions.
11 changes: 3 additions & 8 deletions src/Nethermind/Nethermind.Monitoring/MetricPusher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ namespace Nethermind.Monitoring;
/// </summary>
public class MetricPusher : MetricHandler
{
private readonly TimeSpan _pushInterval;
private readonly HttpMethod _method;
private readonly Uri _targetUrl;
private readonly Func<HttpClient> _httpClientProvider;
Expand Down Expand Up @@ -64,8 +63,6 @@ public MetricPusher(MetricPusherOptions options)
}

_targetUrl = targetUrl;

_pushInterval = TimeSpan.FromMilliseconds(options.IntervalMilliseconds);
_onError = options.OnError;

_method = options.ReplaceOnPush ? HttpMethod.Put : HttpMethod.Post;
Expand All @@ -84,22 +81,20 @@ protected override Task StartServer(CancellationToken cancel)
{
try
{
await _wait.WaitAsync(_pushInterval, cancel);
await _wait.WaitAsync(cancel);
}
catch (OperationCanceledException)
{
break;
}

// As the wait above is cancellable, this will send at the end
await PushOnce();
}

// Push the final state
await PushOnce();
});
}

public void ForceUpdate() => _wait.Release();
public void ScheduleUpdatePush() => _wait.Release();

private async Task PushOnce()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Nethermind.Monitoring.Metrics
public interface IMetricsController
{
void RegisterMetrics(Type type);
void StartUpdating(Action onForced);
void StartUpdating(Action metricsUpdated);
void StopUpdating();
void AddMetricsUpdateAction(Action callback);
void ForceUpdate();
Expand Down
15 changes: 4 additions & 11 deletions src/Nethermind/Nethermind.Monitoring/Metrics/MetricsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public MetricsController(IMetricsConfig metricsConfig)
_useCounters = metricsConfig.CountersEnabled;
}

public void StartUpdating(Action onForced)
public void StartUpdating(Action metricsUpdated)
{
_cts = new CancellationTokenSource();
Task.Run(() => RunLoop(_cts.Token));
Expand All @@ -216,15 +216,11 @@ async Task RunLoop(CancellationToken ct)

while (ct.IsCancellationRequested == false)
{
bool forced = false;

Task wait = GetForceUpdateWait();

try
{

Task finished = await Task.WhenAny(wait, Task.Delay(waitTime, ct));
forced = finished == wait;
await Task.WhenAny(wait, Task.Delay(waitTime, ct));
}
catch (OperationCanceledException)
{
Expand All @@ -234,11 +230,8 @@ async Task RunLoop(CancellationToken ct)

UpdateMetrics();

if (forced && onForced != null)
{
// The update was forced and there's a onForced delegate. Execute it.
onForced();
}
// Inform about metrics updated
metricsUpdated?.Invoke();

if (ct.IsCancellationRequested == false)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Monitoring/MonitoringService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public Task StartAsync()
new NethermindKestrelMetricServer(_exposeHost, _exposePort.Value).Start();
}

_metricsController.StartUpdating(pusher != null ? pusher.ForceUpdate : null);
_metricsController.StartUpdating(pusher != null ? pusher.ScheduleUpdatePush : null);

if (_logger.IsInfo) _logger.Info($"Started monitoring for the group: {_options.Group}, instance: {_options.Instance}");

Expand Down

0 comments on commit b38f2dd

Please sign in to comment.