fix(api): actually run metrics reporters

create MetricsRunner.cs
This commit is contained in:
spiral 2022-11-25 09:32:47 +00:00
parent 770907f232
commit e0cde35b3d
No known key found for this signature in database
GPG Key ID: 244A11E4B0BCF40E
2 changed files with 50 additions and 0 deletions

View File

@ -0,0 +1,48 @@
using App.Metrics;
using NodaTime;
namespace PluralKit.API;
public class MetricsRunner : IHostedService, IDisposable
{
private readonly Serilog.ILogger _logger;
private readonly IMetrics _metrics;
private Timer? _periodicTask = null;
public MetricsRunner(Serilog.ILogger logger, IMetrics metrics)
{
_logger = logger;
_metrics = metrics;
}
public Task StartAsync(CancellationToken stoppingToken)
{
var timeNow = SystemClock.Instance.GetCurrentInstant();
var timeTillNextWholeMinute = TimeSpan.FromMilliseconds(60000 - timeNow.ToUnixTimeMilliseconds() % 60000 + 250);
_periodicTask = new Timer(_ =>
{
var __ = ReportMetrics();
}, null, timeTillNextWholeMinute, TimeSpan.FromMinutes(1));
return Task.CompletedTask;
}
private async Task ReportMetrics()
{
await Task.WhenAll(((IMetricsRoot)_metrics).ReportRunner.RunAllAsync());
_logger.Debug("Submitted metrics to backend");
}
public Task StopAsync(CancellationToken stoppingToken)
{
_periodicTask.Change(Timeout.Infinite, 0);
return Task.CompletedTask;
}
public void Dispose()
{
_periodicTask?.Dispose();
}
}

View File

@ -26,6 +26,8 @@ public class Startup
options.InvalidModelStateResponseFactory = context =>
throw Errors.GenericBadRequest
);
services.AddHostedService<MetricsRunner>();
}
public void ConfigureContainer(ContainerBuilder builder)