예제 #1
0
        /// <inheritdoc />
        public async Task Process(ScanQueueMessage message)
        {
            _logger.LogInformation(
                $"Starting scan of {message.Uri} via backend {_backend.Id} from {message.Id}");

            var cancellationTokenSource = new CancellationTokenSource(
                TimeSpan.FromSeconds(_configuration.GetValue <int>("MAX_SCANNING_TIME")));

            var cancellationToken = cancellationTokenSource.Token;

            var result = new ScanResultMessage
            {
                Status = ScanResultStatus.Queued
            };

            var stopwatch = new Stopwatch();

            stopwatch.Start();

            try
            {
                result.Threats = await _backend.ScanAsync(message.Uri, cancellationToken);

                result.Status = ScanResultStatus.Succeeded;

                _logger.LogInformation(
                    $"Backend {_backend.Id} completed a scan of {message.Id} " +
                    $"with result '{string.Join(", ", result.Threats)}'");
            }
            catch (Exception exception)
            {
                result.Status = ScanResultStatus.Failed;

                _logger.LogError(
                    exception, "Scanning failed with exception");
            }
            finally
            {
                stopwatch.Stop();
            }

            result.Duration = stopwatch.ElapsedMilliseconds / 1000;

            try
            {
                _logger.LogInformation(
                    $"Sending scan results with status {result.Status}");

                _jobClient.Create <IScanResultJob>(
                    x => x.Report(message.Id, _backend.Id, result),
                    new EnqueuedState("default"));
            }
            catch (Exception exception)
            {
                _logger.LogError(exception, "Failed to send scan results");
            }
        }
        /// <inheritdoc />
        public async Task QueueUrlScan(ScanResult result, string fileUrl)
        {
            var message = new ScanQueueMessage
            {
                Id  = result.Id,
                Uri = new Uri(fileUrl)
            };

            var scanners = await _consulClient.Health.Service("scanner", null, true);

            var backends = scanners.Response
                           .Select(s => s.Service.Meta.TryGetValue("BackendId", out var backendId) ? backendId : null)
                           .Where(q => q != null)
                           .Distinct()
                           .ToArray();

            foreach (var backend in backends)
            {
                await UpdateScanResultForBackend(result.Id, backend);

                _backgroundJobClient.Create <IScanBackgroundJob>(
                    j => j.Process(message), new EnqueuedState(backend));
            }
        }