refactoring, WiP

This commit is contained in:
Volodymyr Smirnov 2020-11-01 22:48:16 +02:00
parent b918a82255
commit 1e666d2ed2
16 changed files with 111 additions and 28 deletions

View File

@ -7,6 +7,7 @@ using Hangfire;
using Hangfire.States; using Hangfire.States;
using MalwareMultiScan.Api.Data; using MalwareMultiScan.Api.Data;
using MalwareMultiScan.Api.Services.Interfaces; using MalwareMultiScan.Api.Services.Interfaces;
using MalwareMultiScan.Shared.Enums;
using MalwareMultiScan.Shared.Message; using MalwareMultiScan.Shared.Message;
using MalwareMultiScan.Shared.Services.Interfaces; using MalwareMultiScan.Shared.Services.Interfaces;
using MongoDB.Bson; using MongoDB.Bson;
@ -71,7 +72,10 @@ namespace MalwareMultiScan.Api.Services
public async Task UpdateScanResultForBackend(string resultId, string backendId, public async Task UpdateScanResultForBackend(string resultId, string backendId,
ScanResultMessage result = null) ScanResultMessage result = null)
{ {
result ??= new ScanResultMessage(); result ??= new ScanResultMessage
{
Status = ScanResultStatus.Queued
};
await _collection.UpdateOneAsync( await _collection.UpdateOneAsync(
Builders<ScanResult>.Filter.Where(r => r.Id == resultId), Builders<ScanResult>.Filter.Where(r => r.Id == resultId),

View File

@ -4,8 +4,8 @@
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>netcoreapp3.1</TargetFramework>
<Company>Volodymyr Smirnov</Company> <Company>Volodymyr Smirnov</Company>
<Product>MalwareMultiScan Backends</Product> <Product>MalwareMultiScan Backends</Product>
<AssemblyVersion>1.0.0</AssemblyVersion> <AssemblyVersion>1.5.0</AssemblyVersion>
<FileVersion>1.0.0</FileVersion> <FileVersion>1.5.0</FileVersion>
<GenerateDocumentationFile>true</GenerateDocumentationFile> <GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup> </PropertyGroup>

View File

@ -1,15 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Worker"> <Project Sdk="Microsoft.NET.Sdk.Worker">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>netcoreapp3.1</TargetFramework>
<Company>Volodymyr Smirnov</Company>
<Product>MalwareMultiScan Scanner</Product>
<AssemblyVersion>1.5.0</AssemblyVersion>
<FileVersion>1.5.0</FileVersion>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\MalwareMultiScan.Backends\MalwareMultiScan.Backends.csproj" /> <ProjectReference Include="..\MalwareMultiScan.Backends\MalwareMultiScan.Backends.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<PackageReference Include="Hangfire" Version="1.7.17" />
<PackageReference Include="Hangfire.AspNetCore" Version="1.7.17" />
<PackageReference Include="HangFire.Redis.StackExchange" Version="1.8.4" />
</ItemGroup>
</Project> </Project>

View File

@ -1,10 +1,8 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Hangfire;
using MalwareMultiScan.Backends.Backends.Interfaces;
using MalwareMultiScan.Backends.Extensions; using MalwareMultiScan.Backends.Extensions;
using MalwareMultiScan.Backends.Services.Implementations; using MalwareMultiScan.Backends.Services.Implementations;
using MalwareMultiScan.Backends.Services.Interfaces; using MalwareMultiScan.Backends.Services.Interfaces;
using MalwareMultiScan.ScannerWorker.Services; using MalwareMultiScan.Scanner.Services;
using MalwareMultiScan.Shared.Extensions; using MalwareMultiScan.Shared.Extensions;
using MalwareMultiScan.Shared.Services.Interfaces; using MalwareMultiScan.Shared.Services.Interfaces;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
@ -12,9 +10,9 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace MalwareMultiScan.ScannerWorker namespace MalwareMultiScan.Scanner
{ {
public static class Program internal static class Program
{ {
public static async Task Main(string[] args) public static async Task Main(string[] args)
{ {

View File

@ -8,7 +8,7 @@ using MalwareMultiScan.Backends.Backends.Interfaces;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
namespace MalwareMultiScan.ScannerWorker.Services namespace MalwareMultiScan.Scanner.Services
{ {
public class ConsulHostedService : BackgroundService public class ConsulHostedService : BackgroundService
{ {
@ -17,7 +17,7 @@ namespace MalwareMultiScan.ScannerWorker.Services
private readonly IConsulClient _consulClient; private readonly IConsulClient _consulClient;
private readonly AgentServiceRegistration _registration; private readonly AgentServiceRegistration _registration;
public ConsulHostedService( public ConsulHostedService(
IConsulClient consulClient, IConsulClient consulClient,
IConfiguration configuration, IConfiguration configuration,
@ -37,6 +37,7 @@ namespace MalwareMultiScan.ScannerWorker.Services
}; };
} }
/// <inheritdoc />
protected override async Task ExecuteAsync(CancellationToken stoppingToken) protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{ {
while (!stoppingToken.IsCancellationRequested) while (!stoppingToken.IsCancellationRequested)
@ -48,6 +49,7 @@ namespace MalwareMultiScan.ScannerWorker.Services
} }
} }
/// <inheritdoc />
public override async Task StartAsync(CancellationToken cancellationToken) public override async Task StartAsync(CancellationToken cancellationToken)
{ {
await _consulClient.Agent.ServiceDeregister( await _consulClient.Agent.ServiceDeregister(
@ -68,6 +70,7 @@ namespace MalwareMultiScan.ScannerWorker.Services
await base.StartAsync(cancellationToken); await base.StartAsync(cancellationToken);
} }
/// <inheritdoc />
public override async Task StopAsync(CancellationToken cancellationToken) public override async Task StopAsync(CancellationToken cancellationToken)
{ {
await _consulClient.Agent.ServiceDeregister( await _consulClient.Agent.ServiceDeregister(

View File

@ -11,7 +11,7 @@ using MalwareMultiScan.Shared.Services.Interfaces;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace MalwareMultiScan.ScannerWorker.Services namespace MalwareMultiScan.Scanner.Services
{ {
/// <inheritdoc /> /// <inheritdoc />
public class ScanBackgroundJob : IScanBackgroundJob public class ScanBackgroundJob : IScanBackgroundJob
@ -51,7 +51,10 @@ namespace MalwareMultiScan.ScannerWorker.Services
var cancellationToken = cancellationTokenSource.Token; var cancellationToken = cancellationTokenSource.Token;
var result = new ScanResultMessage(); var result = new ScanResultMessage
{
Status = ScanResultStatus.Queued
};
var stopwatch = new Stopwatch(); var stopwatch = new Stopwatch();

View File

@ -5,5 +5,6 @@
"CONSUL_SERVICE_NAME": "scanner", "CONSUL_SERVICE_NAME": "scanner",
"BACKEND_ID": "dummy", "BACKEND_ID": "dummy",
"MAX_SCANNING_TIME": 60 "MAX_SCANNING_TIME": 60,
"WORKER_COUNT": 4
} }

View File

@ -1,9 +1,23 @@
namespace MalwareMultiScan.Shared.Enums namespace MalwareMultiScan.Shared.Enums
{ {
/// <summary>
/// Scan result status.
/// </summary>
public enum ScanResultStatus public enum ScanResultStatus
{ {
/// <summary>
/// Scan is queued.
/// </summary>
Queued, Queued,
/// <summary>
/// Scan succeeded.
/// </summary>
Succeeded, Succeeded,
/// <summary>
/// Scan failed.
/// </summary>
Failed Failed
} }
} }

View File

@ -1,7 +1,6 @@
using System; using System;
using Consul; using Consul;
using Hangfire; using Hangfire;
using Hangfire.Redis;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@ -25,6 +24,12 @@ namespace MalwareMultiScan.Shared.Extensions
})); }));
} }
/// <summary>
/// Add Hangfire with Redis storage.
/// </summary>
/// <param name="services">Service collection.</param>
/// <param name="configuration">Configuration.</param>
/// <param name="queues">Queue names.</param>
public static void AddHangfire(this IServiceCollection services, public static void AddHangfire(this IServiceCollection services,
IConfiguration configuration, params string[] queues) IConfiguration configuration, params string[] queues)
{ {
@ -34,7 +39,19 @@ namespace MalwareMultiScan.Shared.Extensions
services.AddHangfire(options => services.AddHangfire(options =>
options.UseRedisStorage(configuration.GetValue<string>("REDIS_ADDRESS"))); options.UseRedisStorage(configuration.GetValue<string>("REDIS_ADDRESS")));
services.AddHangfireServer(options => options.Queues = queues); 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<int>("WORKER_COUNT");
if (workerCount > 0)
options.WorkerCount = workerCount;
});
} }
} }
} }

View File

@ -1,6 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>netcoreapp3.1</TargetFramework>
<Company>Volodymyr Smirnov</Company>
<Product>MalwareMultiScan Shared</Product>
<AssemblyVersion>1.5.0</AssemblyVersion>
<FileVersion>1.5.0</FileVersion>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
@ -10,11 +15,5 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="3.1.9" /> <PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="3.1.9" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.9" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.9" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.1.9" /> <PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.1.9" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.1.9" />
<PackageReference Include="MongoDB.Bson" Version="2.11.3" />
</ItemGroup>
<ItemGroup>
<Folder Include="Services" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -2,9 +2,19 @@ using System;
namespace MalwareMultiScan.Shared.Message namespace MalwareMultiScan.Shared.Message
{ {
/// <summary>
/// Scan queue message.
/// </summary>
public class ScanQueueMessage public class ScanQueueMessage
{ {
/// <summary>
/// Scan result id.
/// </summary>
public string Id { get; set; } public string Id { get; set; }
/// <summary>
/// Scan file URL.
/// </summary>
public Uri Uri { get; set; } public Uri Uri { get; set; }
} }
} }

View File

@ -2,12 +2,24 @@ using MalwareMultiScan.Shared.Enums;
namespace MalwareMultiScan.Shared.Message namespace MalwareMultiScan.Shared.Message
{ {
/// <summary>
/// Scan result message.
/// </summary>
public class ScanResultMessage public class ScanResultMessage
{ {
/// <summary>
/// Scan status.
/// </summary>
public ScanResultStatus Status { get; set; } public ScanResultStatus Status { get; set; }
/// <summary>
/// Scan duration in seconds.
/// </summary>
public long Duration { get; set; } public long Duration { get; set; }
/// <summary>
/// Detected threats.
/// </summary>
public string[] Threats { get; set; } public string[] Threats { get; set; }
} }
} }

View File

@ -4,9 +4,16 @@ using MalwareMultiScan.Shared.Message;
namespace MalwareMultiScan.Shared.Services.Interfaces namespace MalwareMultiScan.Shared.Services.Interfaces
{ {
/// <summary>
/// Scan background job.
/// </summary>
[AutomaticRetry(Attempts = 0, OnAttemptsExceeded = AttemptsExceededAction.Delete)] [AutomaticRetry(Attempts = 0, OnAttemptsExceeded = AttemptsExceededAction.Delete)]
public interface IScanBackgroundJob public interface IScanBackgroundJob
{ {
/// <summary>
/// Start scan.
/// </summary>
/// <param name="message">Scan queue message.</param>
Task Process(ScanQueueMessage message); Task Process(ScanQueueMessage message);
} }
} }

View File

@ -5,11 +5,27 @@ using MalwareMultiScan.Shared.Message;
namespace MalwareMultiScan.Shared.Services.Interfaces namespace MalwareMultiScan.Shared.Services.Interfaces
{ {
/// <summary>
/// Scan background result job.
/// </summary>
public interface IScanResultJob public interface IScanResultJob
{ {
/// <summary>
/// Report job status.
/// </summary>
/// <param name="resultId">Result id.</param>
/// <param name="backendId">Backend id.</param>
/// <param name="result">Scan result.</param>
[AutomaticRetry(Attempts = 0, OnAttemptsExceeded = AttemptsExceededAction.Delete)] [AutomaticRetry(Attempts = 0, OnAttemptsExceeded = AttemptsExceededAction.Delete)]
Task Report(string resultId, string backendId, ScanResultMessage result); Task Report(string resultId, string backendId, ScanResultMessage result);
/// <summary>
/// Notify remote URL on scan result.
/// </summary>
/// <param name="uri">Base URL.</param>
/// <param name="resultId">Result id.</param>
/// <param name="backendId">Backend id.</param>
/// <param name="result">Scan result.</param>
[AutomaticRetry(Attempts = 0, OnAttemptsExceeded = AttemptsExceededAction.Delete)] [AutomaticRetry(Attempts = 0, OnAttemptsExceeded = AttemptsExceededAction.Delete)]
Task Notify(Uri uri, string resultId, string backendId, ScanResultMessage result); Task Notify(Uri uri, string resultId, string backendId, ScanResultMessage result);
} }

View File

@ -15,7 +15,7 @@ EndProjectSection
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MalwareMultiScan.Tests", "MalwareMultiScan.Tests\MalwareMultiScan.Tests.csproj", "{9896162D-8FC7-4911-933F-A78C94128923}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MalwareMultiScan.Tests", "MalwareMultiScan.Tests\MalwareMultiScan.Tests.csproj", "{9896162D-8FC7-4911-933F-A78C94128923}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MalwareMultiScan.ScannerWorker", "MalwareMultiScan.ScannerWorker\MalwareMultiScan.ScannerWorker.csproj", "{D36ED4DD-4EEA-4609-8AED-B2FD496E4C90}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MalwareMultiScan.Scanner", "MalwareMultiScan.Scanner\MalwareMultiScan.Scanner.csproj", "{D36ED4DD-4EEA-4609-8AED-B2FD496E4C90}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MalwareMultiScan.Shared", "MalwareMultiScan.Shared\MalwareMultiScan.Shared.csproj", "{534E3C92-FD6D-401C-99D4-792DB11B57AE}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MalwareMultiScan.Shared", "MalwareMultiScan.Shared\MalwareMultiScan.Shared.csproj", "{534E3C92-FD6D-401C-99D4-792DB11B57AE}"
EndProject EndProject