fix(api): actually run metrics reporters
create MetricsRunner.cs
This commit is contained in:
parent
770907f232
commit
e0cde35b3d
48
PluralKit.API/MetricsRunner.cs
Normal file
48
PluralKit.API/MetricsRunner.cs
Normal 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();
|
||||
}
|
||||
}
|
@ -26,6 +26,8 @@ public class Startup
|
||||
options.InvalidModelStateResponseFactory = context =>
|
||||
throw Errors.GenericBadRequest
|
||||
);
|
||||
|
||||
services.AddHostedService<MetricsRunner>();
|
||||
}
|
||||
|
||||
public void ConfigureContainer(ContainerBuilder builder)
|
||||
|
Loading…
Reference in New Issue
Block a user