mirror of
https://github.com/volodymyrsmirnov/MalwareMultiScan.git
synced 2025-08-24 21:42:23 +00:00
71 lines
2.4 KiB
C#
71 lines
2.4 KiB
C#
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using EasyNetQ;
|
|
using MalwareMultiScan.Backends.Messages;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace MalwareMultiScan.Api.Services
|
|
{
|
|
/// <summary>
|
|
/// Receiver hosted service.
|
|
/// </summary>
|
|
public class ReceiverHostedService : IHostedService
|
|
{
|
|
private readonly IBus _bus;
|
|
private readonly IConfiguration _configuration;
|
|
private readonly ILogger<ReceiverHostedService> _logger;
|
|
private readonly ScanResultService _scanResultService;
|
|
|
|
/// <summary>
|
|
/// Create receiver hosted service.
|
|
/// </summary>
|
|
/// <param name="bus">Service bus.</param>
|
|
/// <param name="configuration">Configuration.</param>
|
|
/// <param name="scanResultService">Scan result service.</param>
|
|
/// <param name="logger">Logger.</param>
|
|
public ReceiverHostedService(IBus bus, IConfiguration configuration, ScanResultService scanResultService,
|
|
ILogger<ReceiverHostedService> logger)
|
|
{
|
|
_bus = bus;
|
|
_configuration = configuration;
|
|
_scanResultService = scanResultService;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public Task StartAsync(CancellationToken cancellationToken)
|
|
{
|
|
_bus.Receive<ScanResultMessage>(_configuration.GetValue<string>("ResultsSubscriptionId"), async message =>
|
|
{
|
|
message.Threats ??= new string[] { };
|
|
|
|
_logger.LogInformation(
|
|
$"Received a result from {message.Backend} for {message.Id} " +
|
|
$"with threats {string.Join(",", message.Threats)}");
|
|
|
|
await _scanResultService.UpdateScanResultForBackend(
|
|
message.Id, message.Backend, message.Duration, true,
|
|
message.Succeeded, message.Threats);
|
|
});
|
|
|
|
_logger.LogInformation(
|
|
"Started hosted service for receiving scan results");
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
|
|
/// <inheritdoc />
|
|
public Task StopAsync(CancellationToken cancellationToken)
|
|
{
|
|
_bus.Dispose();
|
|
|
|
_logger.LogInformation(
|
|
"Stopped hosted service for receiving scan results");
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
}
|
|
} |