From cfcfe92784e4f55c2aefbbdac0cfc12db285c173 Mon Sep 17 00:00:00 2001 From: Volodymyr Smirnov Date: Mon, 2 Nov 2020 11:39:12 +0200 Subject: [PATCH] code cleanup --- MalwareMultiScan.Api/Services/ScanResultJob.cs | 4 ++-- MalwareMultiScan.Api/Startup.cs | 8 ++++---- MalwareMultiScan.Scanner/Program.cs | 2 +- .../Services/ConsulHostedService.cs | 2 +- .../Services/ScanBackgroundJob.cs | 4 ++-- MalwareMultiScan.Shared/Enums/ScanResultStatus.cs | 4 ++-- .../Extensions/ServiceCollectionExtensions.cs | 14 +++++++------- .../Message/ScanQueueMessage.cs | 2 +- .../Services/Interfaces/IScanResultJob.cs | 2 +- .../Api/ScanResultServiceTests.cs | 8 ++++---- README.md | 6 +++--- docker-compose.yaml | 2 -- 12 files changed, 28 insertions(+), 30 deletions(-) diff --git a/MalwareMultiScan.Api/Services/ScanResultJob.cs b/MalwareMultiScan.Api/Services/ScanResultJob.cs index f4677b9..cea9e93 100644 --- a/MalwareMultiScan.Api/Services/ScanResultJob.cs +++ b/MalwareMultiScan.Api/Services/ScanResultJob.cs @@ -57,7 +57,7 @@ namespace MalwareMultiScan.Api.Services return; _backgroundJobClient.Create( - x => x.Notify(scanResult.CallbackUrl, resultId, backendId, result), + x => x.Notify(scanResult.CallbackUrl, resultId, backendId, result), new EnqueuedState("default")); } @@ -82,7 +82,7 @@ namespace MalwareMultiScan.Api.Services cancellationTokenSource.Token); response.EnsureSuccessStatusCode(); - + _logger.LogInformation($"Sent POST to callback URL {uri}"); } catch (Exception exception) diff --git a/MalwareMultiScan.Api/Startup.cs b/MalwareMultiScan.Api/Startup.cs index c40d624..e9f7072 100644 --- a/MalwareMultiScan.Api/Startup.cs +++ b/MalwareMultiScan.Api/Startup.cs @@ -29,10 +29,10 @@ namespace MalwareMultiScan.Api services.AddConsul(_configuration); services.AddMongoDb(_configuration); services.AddHangfire(_configuration); - + services.AddSingleton(); services.AddSingleton(); - + services.AddControllers(); services.AddHttpClient(); } @@ -41,9 +41,9 @@ namespace MalwareMultiScan.Api { app.UseRouting(); app.UseEndpoints(endpoints => endpoints.MapControllers()); - + app.UseForwardedHeaders(); - + if (hostEnvironment.IsDevelopment()) app.UseHangfireDashboard(); } diff --git a/MalwareMultiScan.Scanner/Program.cs b/MalwareMultiScan.Scanner/Program.cs index e612b93..57f56ca 100644 --- a/MalwareMultiScan.Scanner/Program.cs +++ b/MalwareMultiScan.Scanner/Program.cs @@ -34,7 +34,7 @@ namespace MalwareMultiScan.Scanner services.AddConsul(context.Configuration); services.AddScanBackend(context.Configuration); - services.AddHangfire(context.Configuration, + services.AddHangfire(context.Configuration, context.Configuration.GetValue("BACKEND_ID")); services.AddSingleton(); diff --git a/MalwareMultiScan.Scanner/Services/ConsulHostedService.cs b/MalwareMultiScan.Scanner/Services/ConsulHostedService.cs index 4674c3b..1a73747 100644 --- a/MalwareMultiScan.Scanner/Services/ConsulHostedService.cs +++ b/MalwareMultiScan.Scanner/Services/ConsulHostedService.cs @@ -19,7 +19,7 @@ namespace MalwareMultiScan.Scanner.Services private readonly IConsulClient _consulClient; private readonly AgentServiceRegistration _registration; - + /// /// Initialize consul hosted service. /// diff --git a/MalwareMultiScan.Scanner/Services/ScanBackgroundJob.cs b/MalwareMultiScan.Scanner/Services/ScanBackgroundJob.cs index 0fbf5da..b92e17c 100644 --- a/MalwareMultiScan.Scanner/Services/ScanBackgroundJob.cs +++ b/MalwareMultiScan.Scanner/Services/ScanBackgroundJob.cs @@ -18,8 +18,8 @@ namespace MalwareMultiScan.Scanner.Services { private readonly IScanBackend _backend; private readonly IConfiguration _configuration; - private readonly ILogger _logger; private readonly IBackgroundJobClient _jobClient; + private readonly ILogger _logger; /// /// Initialize scan background job. @@ -90,7 +90,7 @@ namespace MalwareMultiScan.Scanner.Services $"Sending scan results with status {result.Status}"); _jobClient.Create( - x => x.Report(message.Id, _backend.Id, result), + x => x.Report(message.Id, _backend.Id, result), new EnqueuedState("default")); } catch (Exception exception) diff --git a/MalwareMultiScan.Shared/Enums/ScanResultStatus.cs b/MalwareMultiScan.Shared/Enums/ScanResultStatus.cs index 0f2ab06..d7cf2c7 100644 --- a/MalwareMultiScan.Shared/Enums/ScanResultStatus.cs +++ b/MalwareMultiScan.Shared/Enums/ScanResultStatus.cs @@ -9,12 +9,12 @@ namespace MalwareMultiScan.Shared.Enums /// Scan is queued. /// Queued, - + /// /// Scan succeeded. /// Succeeded, - + /// /// Scan failed. /// diff --git a/MalwareMultiScan.Shared/Extensions/ServiceCollectionExtensions.cs b/MalwareMultiScan.Shared/Extensions/ServiceCollectionExtensions.cs index d3c5715..6267f02 100644 --- a/MalwareMultiScan.Shared/Extensions/ServiceCollectionExtensions.cs +++ b/MalwareMultiScan.Shared/Extensions/ServiceCollectionExtensions.cs @@ -26,30 +26,30 @@ namespace MalwareMultiScan.Shared.Extensions config.WaitTime = TimeSpan.FromSeconds(120); })); } - + /// /// Add Hangfire with Redis storage. /// /// Service collection. /// Configuration. /// Queue names. - public static void AddHangfire(this IServiceCollection services, + public static void AddHangfire(this IServiceCollection services, IConfiguration configuration, params string[] queues) { if (queues.Length == 0) queues = new[] {"default"}; - - services.AddHangfire(options => + + services.AddHangfire(options => options.UseRedisStorage(configuration.GetValue("REDIS_ADDRESS"))); - + services.AddHangfireServer(options => { options.Queues = queues; - + options.ServerTimeout = TimeSpan.FromSeconds(30); options.HeartbeatInterval = TimeSpan.FromSeconds(5); options.ServerCheckInterval = TimeSpan.FromSeconds(15); - + var workerCount = configuration.GetValue("WORKER_COUNT"); if (workerCount > 0) diff --git a/MalwareMultiScan.Shared/Message/ScanQueueMessage.cs b/MalwareMultiScan.Shared/Message/ScanQueueMessage.cs index b900abb..a013002 100644 --- a/MalwareMultiScan.Shared/Message/ScanQueueMessage.cs +++ b/MalwareMultiScan.Shared/Message/ScanQueueMessage.cs @@ -11,7 +11,7 @@ namespace MalwareMultiScan.Shared.Message /// Scan result id. /// public string Id { get; set; } - + /// /// Scan file URL. /// diff --git a/MalwareMultiScan.Shared/Services/Interfaces/IScanResultJob.cs b/MalwareMultiScan.Shared/Services/Interfaces/IScanResultJob.cs index 63f4237..44026de 100644 --- a/MalwareMultiScan.Shared/Services/Interfaces/IScanResultJob.cs +++ b/MalwareMultiScan.Shared/Services/Interfaces/IScanResultJob.cs @@ -18,7 +18,7 @@ namespace MalwareMultiScan.Shared.Services.Interfaces /// Scan result. [AutomaticRetry(Attempts = 0, OnAttemptsExceeded = AttemptsExceededAction.Delete)] Task Report(string resultId, string backendId, ScanResultMessage result); - + /// /// Notify remote URL on scan result. /// diff --git a/MalwareMultiScan.Tests/Api/ScanResultServiceTests.cs b/MalwareMultiScan.Tests/Api/ScanResultServiceTests.cs index 9332c5b..c3e1f0a 100644 --- a/MalwareMultiScan.Tests/Api/ScanResultServiceTests.cs +++ b/MalwareMultiScan.Tests/Api/ScanResultServiceTests.cs @@ -28,10 +28,10 @@ namespace MalwareMultiScan.Tests.Api var connection = new MongoClient(_mongoDbRunner.ConnectionString); var database = connection.GetDatabase("Test"); var gridFsBucket = new GridFSBucket(database); - + _resultService = new ScanResultService( - database, gridFsBucket, - Mock.Of(), + database, gridFsBucket, + Mock.Of(), Mock.Of()); } @@ -83,7 +83,7 @@ namespace MalwareMultiScan.Tests.Api { Duration = 100, Status = ScanResultStatus.Succeeded, - Threats = new []{"Test"} + Threats = new[] {"Test"} }); result = await _resultService.GetScanResult(result.Id); diff --git a/README.md b/README.md index ee113f8..6aa9394 100644 --- a/README.md +++ b/README.md @@ -80,9 +80,9 @@ I.e. when you define `callbackUrl=http://localhost:1234/scan-results`, the POST ```json { - "Status":1, - "Duration":5, - "Threats":["Malware.Dummy.Result"] + "Status": 1, + "Duration": 5, + "Threats": ["Malware.Dummy.Result"] } ``` diff --git a/docker-compose.yaml b/docker-compose.yaml index 1b73f3e..34a7d2b 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -25,8 +25,6 @@ services: restart: on-failure ports: - "8888:8888" - expose: - - "8888" depends_on: - api environment: