55 lines
1.9 KiB
C#
Raw Normal View History

using System.Threading;
using System.Threading.Tasks;
using EasyNetQ;
2020-10-26 17:06:29 +02:00
using MalwareMultiScan.Backends.Messages;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
2020-10-26 17:06:29 +02:00
using Microsoft.Extensions.Logging;
namespace MalwareMultiScan.Api.Services
{
public class ReceiverHostedService : IHostedService
{
private readonly IBus _bus;
private readonly IConfiguration _configuration;
private readonly ScanResultService _scanResultService;
2020-10-26 17:06:29 +02:00
private readonly ILogger<ReceiverHostedService> _logger;
2020-10-26 17:06:29 +02:00
public ReceiverHostedService(IBus bus, IConfiguration configuration, ScanResultService scanResultService,
ILogger<ReceiverHostedService> logger)
{
_bus = bus;
_configuration = configuration;
_scanResultService = scanResultService;
2020-10-26 17:06:29 +02:00
_logger = logger;
}
public Task StartAsync(CancellationToken cancellationToken)
{
_bus.Receive<ScanResultMessage>(_configuration.GetValue<string>("ResultsSubscriptionId"), async message =>
{
2020-10-26 17:06:29 +02:00
_logger.LogInformation(
$"Received a result from {message.Backend} for {message.Id} " +
$"with threats {string.Join(",", message.Threats)}");
await _scanResultService.UpdateScanResultForBackend(
message.Id, message.Backend, true, true, message.Threats);
});
2020-10-26 17:06:29 +02:00
_logger.LogInformation(
"Started hosted service for receiving scan results");
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
_bus.Dispose();
2020-10-26 17:06:29 +02:00
_logger.LogInformation(
"Stopped hosted service for receiving scan results");
return Task.CompletedTask;
}
}
}