2020-04-20 10:30:22 +00:00
|
|
|
using System;
|
2019-11-24 15:47:17 +00:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2019-11-26 18:24:27 +00:00
|
|
|
using Microsoft.Extensions.Options;
|
2020-08-01 15:07:13 +00:00
|
|
|
using NucuCar.Telemetry.Abstractions;
|
2019-11-24 15:47:17 +00:00
|
|
|
|
2020-04-17 15:11:07 +00:00
|
|
|
namespace NucuCar.Telemetry
|
2019-11-24 15:47:17 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Telemetry service, which pools the telemetry sources and pushes telemetry data to the cloud.
|
|
|
|
/// </summary>
|
2019-11-24 16:03:46 +00:00
|
|
|
public class TelemetryWorker : BackgroundService
|
2019-11-24 15:47:17 +00:00
|
|
|
{
|
|
|
|
private readonly int _interval;
|
2019-12-01 16:04:46 +00:00
|
|
|
private readonly bool _serviceEnabled;
|
2019-11-24 15:47:17 +00:00
|
|
|
private readonly ILogger _logger;
|
2021-08-02 18:28:02 +00:00
|
|
|
private readonly ITelemetryPublisher _telemetryPublisher;
|
2019-11-24 15:47:17 +00:00
|
|
|
|
2021-10-03 20:37:53 +00:00
|
|
|
public TelemetryWorker(ILogger<TelemetryWorker> logger, IOptions<Config> options,
|
2021-08-02 18:28:02 +00:00
|
|
|
ITelemetryPublisher telemetryPublisherProxy)
|
2019-11-24 15:47:17 +00:00
|
|
|
{
|
|
|
|
_logger = logger;
|
2019-11-26 18:24:27 +00:00
|
|
|
_interval = options.Value.PublishInterval;
|
2019-12-01 16:04:46 +00:00
|
|
|
_serviceEnabled = options.Value.ServiceEnabled;
|
2021-08-02 18:28:02 +00:00
|
|
|
_telemetryPublisher = telemetryPublisherProxy;
|
2019-11-24 15:47:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
|
|
|
{
|
2020-04-20 10:30:22 +00:00
|
|
|
try
|
2019-12-01 16:04:46 +00:00
|
|
|
{
|
2020-04-20 10:30:22 +00:00
|
|
|
if (!_serviceEnabled)
|
|
|
|
{
|
|
|
|
_logger.LogInformation("Telemetry service is disabled!");
|
|
|
|
return;
|
|
|
|
}
|
2019-12-06 22:31:28 +00:00
|
|
|
|
2020-04-20 10:30:22 +00:00
|
|
|
if (_telemetryPublisher == null)
|
|
|
|
{
|
|
|
|
_logger?.LogCritical("Invalid state! TelemetryPublisher is null!");
|
|
|
|
return;
|
|
|
|
}
|
2019-12-06 22:31:28 +00:00
|
|
|
|
2019-11-24 15:47:17 +00:00
|
|
|
await Task.Delay(_interval, stoppingToken);
|
2020-04-20 10:30:22 +00:00
|
|
|
while (!stoppingToken.IsCancellationRequested)
|
|
|
|
{
|
|
|
|
await _telemetryPublisher.PublishAsync(stoppingToken);
|
|
|
|
_logger?.LogDebug("Telemetry data published!");
|
|
|
|
await Task.Delay(_interval, stoppingToken);
|
|
|
|
}
|
|
|
|
}
|
2020-10-25 13:31:54 +00:00
|
|
|
catch (TaskCanceledException)
|
|
|
|
{
|
2021-08-01 18:01:19 +00:00
|
|
|
_logger?.LogInformation("The TelemetryWorker task was canceled");
|
2020-10-25 13:31:54 +00:00
|
|
|
}
|
2020-04-20 10:30:22 +00:00
|
|
|
catch (Exception e)
|
|
|
|
{
|
2021-08-01 18:01:19 +00:00
|
|
|
_logger?.LogError("Unhandled exception in TelemetryWorker. {Message}",e.Message);
|
|
|
|
_logger?.LogDebug("{StackTrace}", e.StackTrace);
|
2019-11-24 15:47:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|