1+ using System ;
2+ using System . Net . Http ;
3+ using System . Text ;
14using System . Threading ;
25using System . Threading . Tasks ;
36using EasyNetQ ;
47using MalwareMultiScan . Api . Services . Interfaces ;
58using MalwareMultiScan . Backends . Messages ;
69using Microsoft . Extensions . Configuration ;
710using Microsoft . Extensions . Logging ;
11+ using Newtonsoft . Json ;
812
913namespace MalwareMultiScan . Api . Services . Implementations
1014{
@@ -13,6 +17,7 @@ public class ReceiverHostedService : IReceiverHostedService
1317 {
1418 private readonly IBus _bus ;
1519 private readonly IConfiguration _configuration ;
20+ private readonly IHttpClientFactory _httpClientFactory ;
1621 private readonly ILogger < ReceiverHostedService > _logger ;
1722 private readonly IScanResultService _scanResultService ;
1823
@@ -23,31 +28,23 @@ public class ReceiverHostedService : IReceiverHostedService
2328 /// <param name="configuration">Configuration.</param>
2429 /// <param name="scanResultService">Scan result service.</param>
2530 /// <param name="logger">Logger.</param>
31+ /// <param name="httpClientFactory">HTTP client factory.</param>
2632 public ReceiverHostedService ( IBus bus , IConfiguration configuration , IScanResultService scanResultService ,
27- ILogger < ReceiverHostedService > logger )
33+ ILogger < ReceiverHostedService > logger , IHttpClientFactory httpClientFactory )
2834 {
2935 _bus = bus ;
3036 _configuration = configuration ;
3137 _scanResultService = scanResultService ;
3238 _logger = logger ;
39+ _httpClientFactory = httpClientFactory ;
3340 }
3441
3542
3643 /// <inheritdoc />
3744 public Task StartAsync ( CancellationToken cancellationToken )
3845 {
39- _bus . Receive < ScanResultMessage > ( _configuration . GetValue < string > ( "ResultsSubscriptionId" ) , async message =>
40- {
41- message . Threats ??= new string [ ] { } ;
42-
43- _logger . LogInformation (
44- $ "Received a result from { message . Backend } for { message . Id } " +
45- $ "with threats { string . Join ( "," , message . Threats ) } ") ;
46-
47- await _scanResultService . UpdateScanResultForBackend (
48- message . Id , message . Backend , message . Duration , true ,
49- message . Succeeded , message . Threats ) ;
50- } ) ;
46+ _bus . Receive < ScanResultMessage > (
47+ _configuration . GetValue < string > ( "ResultsSubscriptionId" ) , StoreScanResult ) ;
5148
5249 _logger . LogInformation (
5350 "Started hosted service for receiving scan results" ) ;
@@ -65,5 +62,42 @@ public Task StopAsync(CancellationToken cancellationToken)
6562
6663 return Task . CompletedTask ;
6764 }
65+
66+ private async Task StoreScanResult ( ScanResultMessage message )
67+ {
68+ message . Threats ??= new string [ ] { } ;
69+
70+ _logger . LogInformation (
71+ $ "Received a result from { message . Backend } for { message . Id } " +
72+ $ "with threats { string . Join ( "," , message . Threats ) } ") ;
73+
74+ await _scanResultService . UpdateScanResultForBackend (
75+ message . Id , message . Backend , message . Duration , true ,
76+ message . Succeeded , message . Threats ) ;
77+
78+ var result = await _scanResultService . GetScanResult ( message . Id ) ;
79+
80+ if ( result ? . CallbackUrl == null )
81+ return ;
82+
83+ var cancellationTokenSource = new CancellationTokenSource (
84+ TimeSpan . FromSeconds ( 3 ) ) ;
85+
86+ using var httpClient = _httpClientFactory . CreateClient ( ) ;
87+
88+ try
89+ {
90+ var response = await httpClient . PostAsync (
91+ result . CallbackUrl ,
92+ new StringContent ( JsonConvert . SerializeObject ( result ) , Encoding . UTF8 , "application/json" ) ,
93+ cancellationTokenSource . Token ) ;
94+
95+ response . EnsureSuccessStatusCode ( ) ;
96+ }
97+ catch ( Exception exception )
98+ {
99+ _logger . LogError ( exception , $ "Failed to POST to callback URL { result . CallbackUrl } ") ;
100+ }
101+ }
68102 }
69103}
0 commit comments