diff --git a/Api.GateWay/Api.GateWay.csproj b/Api.GateWay/Api.GateWay.csproj new file mode 100644 index 00000000..d660cf11 --- /dev/null +++ b/Api.GateWay/Api.GateWay.csproj @@ -0,0 +1,15 @@ + + + + net8.0 + enable + enable + + + + + + + + + diff --git a/Api.GateWay/LoadBalancers/WeightedRoundRobin.cs b/Api.GateWay/LoadBalancers/WeightedRoundRobin.cs new file mode 100644 index 00000000..3790c23e --- /dev/null +++ b/Api.GateWay/LoadBalancers/WeightedRoundRobin.cs @@ -0,0 +1,45 @@ +using Ocelot.LoadBalancer.LoadBalancers; +using Ocelot.Responses; +using Ocelot.Values; + +namespace Api.GateWay.LoadBalancers; + +/// +/// Балансировщик нагрузки на основе параметров запроса +/// +/// Функция получения списка доступных сервисов +public class WeightedRoundRobin(Func>> services) : ILoadBalancer +{ + private readonly Func>> _services = services; + private readonly int[] _weights = [1, 2, 3, 2, 1]; + private int _currentIndex = -1; + private int _remainingCalls = 0; + + public string Type => nameof(WeightedRoundRobin); + + private static readonly object _lock = new(); + + public async Task> LeaseAsync(HttpContext httpContext) + { + var services = await _services.Invoke(); + if (services == null || services.Count == 0) + return new ErrorResponse( + new ServicesAreEmptyError("No services available")); + + lock (_lock) + { + if (_currentIndex == -1 || _remainingCalls == 0) + { + _currentIndex = (_currentIndex + 1) % services.Count; + _remainingCalls = _weights[_currentIndex]; + } + + var selectedService = services[_currentIndex]; + _remainingCalls--; + + return new OkResponse(selectedService.HostAndPort); + } + } + + public void Release(ServiceHostAndPort hostAndPort) { } +} diff --git a/Api.GateWay/Program.cs b/Api.GateWay/Program.cs new file mode 100644 index 00000000..7f5b2d8d --- /dev/null +++ b/Api.GateWay/Program.cs @@ -0,0 +1,26 @@ +using Api.GateWay.LoadBalancers; +using Ocelot.DependencyInjection; +using Ocelot.Middleware; + +var builder = WebApplication.CreateBuilder(args); + +builder.AddServiceDefaults(); +builder.Services.AddServiceDiscovery(); +builder.Configuration.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true); +builder.Services.AddOcelot() + .AddCustomLoadBalancer((_, _, provider) => new(provider.GetAsync)); + +builder.Services.AddCors(options => options.AddDefaultPolicy(policy => +{ + policy.WithOrigins(["http://localhost:5127", "https://localhost:7282"]); + policy.WithMethods("GET"); + policy.WithHeaders("Content-Type"); +})); + +var app = builder.Build(); + +app.UseCors(); + +await app.UseOcelot(); + +app.Run(); diff --git a/Api.GateWay/Properties/launchSettings.json b/Api.GateWay/Properties/launchSettings.json new file mode 100644 index 00000000..79e9512d --- /dev/null +++ b/Api.GateWay/Properties/launchSettings.json @@ -0,0 +1,38 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:9811", + "sslPort": 44389 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:5142", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:7147;http://localhost:5142", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/Api.GateWay/appsettings.Development.json b/Api.GateWay/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/Api.GateWay/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Api.GateWay/appsettings.json b/Api.GateWay/appsettings.json new file mode 100644 index 00000000..10f68b8c --- /dev/null +++ b/Api.GateWay/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/Api.GateWay/ocelot.json b/Api.GateWay/ocelot.json new file mode 100644 index 00000000..5d8524c4 --- /dev/null +++ b/Api.GateWay/ocelot.json @@ -0,0 +1,35 @@ +{ + "Routes": [ + { + "UpstreamPathTemplate": "/employee", + "UpstreamHttpMethod": [ "GET" ], + "DownstreamPathTemplate": "/employee", + "DownstreamScheme": "https", + "LoadBalancerOptions": { + "Type": "WeightedRoundRobin" + }, + "DownstreamHostAndPorts": [ + { + "Host": "localhost", + "Port": 15000 + }, + { + "Host": "localhost", + "Port": 15001 + }, + { + "Host": "localhost", + "Port": 15002 + }, + { + "Host": "localhost", + "Port": 15003 + }, + { + "Host": "localhost", + "Port": 15004 + } + ] + } + ] +} \ No newline at end of file diff --git a/Aspire.AppHost.Tests/Aspire.AppHost.Tests.csproj b/Aspire.AppHost.Tests/Aspire.AppHost.Tests.csproj new file mode 100644 index 00000000..cc105413 --- /dev/null +++ b/Aspire.AppHost.Tests/Aspire.AppHost.Tests.csproj @@ -0,0 +1,27 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + + + diff --git a/Aspire.AppHost.Tests/IntegrationTests.cs b/Aspire.AppHost.Tests/IntegrationTests.cs new file mode 100644 index 00000000..ef1aec67 --- /dev/null +++ b/Aspire.AppHost.Tests/IntegrationTests.cs @@ -0,0 +1,161 @@ +using Aspire.Hosting; +using Aspire.Hosting.Testing; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Service.Api.Entities; +using System.Text.Json; +using Xunit.Abstractions; +using System.Collections.Concurrent; + +namespace Aspire.AppHost.Tests; + +/// +/// Интеграционные тесты для проверки микросервисного пайплайна: +/// API -> SQS -> Event.Sink -> MinIO +/// +/// Служба журналирования юнит-тестов +public class IntegrationTests(ITestOutputHelper output) : IAsyncLifetime +{ + private static readonly JsonSerializerOptions _jsonOptions = new() { PropertyNameCaseInsensitive = true }; + + private DistributedApplication? _app; + + /// + public async Task InitializeAsync() + { + var cancellationToken = CancellationToken.None; + var builder = await DistributedApplicationTestingBuilder + .CreateAsync(cancellationToken); + + builder.Configuration["DcpPublisher:RandomizePorts"] = "false"; + builder.Services.AddLogging(logging => + { + logging.AddXUnit(output); + logging.SetMinimumLevel(LogLevel.Debug); + logging.AddFilter("Aspire.Hosting.Dcp", LogLevel.Debug); + logging.AddFilter("Aspire.Hosting", LogLevel.Debug); + }); + + _app = await builder.BuildAsync(cancellationToken); + await _app.StartAsync(cancellationToken); + } + + /// + /// Основной тест: запрос сотрудника через gateway -> файл появляется в Minio, + /// данные в ответе API и в объектном хранилище совпадают. + /// + [Fact] + public async Task EmployeePipeline_ApiToStorage_Success() + { + var id = new Random().Next(1, 100); + + using var gatewayClient = _app!.CreateHttpClient("api-gateway", "http"); + using var gatewayResponse = await gatewayClient.GetAsync($"/employee?id={id}"); + gatewayResponse.EnsureSuccessStatusCode(); + var apiEmployee = JsonSerializer.Deserialize( + await gatewayResponse.Content.ReadAsStringAsync(), _jsonOptions); + + await Task.Delay(TimeSpan.FromSeconds(5)); + + using var storageClient = _app!.CreateHttpClient("employee-sink", "http"); + using var listResponse = await storageClient.GetAsync("/api/files"); + listResponse.EnsureSuccessStatusCode(); + var fileList = JsonSerializer.Deserialize>( + await listResponse.Content.ReadAsStringAsync()); + + using var fileResponse = await storageClient.GetAsync($"/api/files/employee_{id}.json"); + fileResponse.EnsureSuccessStatusCode(); + var s3Employee = JsonSerializer.Deserialize( + await fileResponse.Content.ReadAsStringAsync(), _jsonOptions); + + Assert.NotNull(fileList); + Assert.Single(fileList); + Assert.Equal($"employee_{id}.json", fileList![0]); + + Assert.NotNull(apiEmployee); + Assert.NotNull(s3Employee); + Assert.Equal(id, s3Employee!.Id); + Assert.Equivalent(apiEmployee, s3Employee); + } + + /// + /// Проверка устойчивости: некорректный запрос (без id) не попадает в очередь + /// и не создаёт мусорных файлов. + /// + [Fact] + public async Task InvalidRequest_DoesNotCreateFile() + { + using var gatewayClient = _app!.CreateHttpClient("api-gateway", "http"); + + using var badResponse = await gatewayClient.GetAsync("/employee"); + Assert.False(badResponse.IsSuccessStatusCode); + + await Task.Delay(TimeSpan.FromSeconds(3)); + + using var storageClient = _app!.CreateHttpClient("employee-sink", "http"); + using var listResponse = await storageClient.GetAsync("/api/files"); + listResponse.EnsureSuccessStatusCode(); + var files = JsonSerializer.Deserialize>(await listResponse.Content.ReadAsStringAsync()); + + Assert.NotNull(files); + Assert.DoesNotContain(files!, f => f.StartsWith("employee_0") || f.Equals("employee_.json")); + } + + /// + /// Параллельная отправка нескольких сотрудников: все файлы должны быть созданы, + /// содержимое совпадает с ответами API. + /// + [Fact] + public async Task ConcurrentRequests_AllEmployeesStored() + { + const int concurrentCount = 5; + var ids = Enumerable.Range(1, concurrentCount).Select(_ => new Random().Next(200, 300)).Distinct().ToArray(); + var results = new ConcurrentDictionary(); + + using var gatewayClient = _app!.CreateHttpClient("api-gateway", "http"); + + var tasks = ids.Select(async id => + { + using var response = await gatewayClient.GetAsync($"/employee?id={id}"); + response.EnsureSuccessStatusCode(); + var emp = JsonSerializer.Deserialize( + await response.Content.ReadAsStringAsync(), _jsonOptions); + results[id] = emp; + }); + await Task.WhenAll(tasks); + + await Task.Delay(TimeSpan.FromSeconds(8)); + + using var storageClient = _app!.CreateHttpClient("employee-sink", "http"); + using var listResponse = await storageClient.GetAsync("/api/files"); + listResponse.EnsureSuccessStatusCode(); + var fileList = JsonSerializer.Deserialize>(await listResponse.Content.ReadAsStringAsync()); + + Assert.NotNull(fileList); + foreach (var id in ids) + { + var expectedFileName = $"employee_{id}.json"; + Assert.Contains(expectedFileName, fileList!); + + using var fileResponse = await storageClient.GetAsync($"/api/files/{expectedFileName}"); + fileResponse.EnsureSuccessStatusCode(); + var storedEmployee = JsonSerializer.Deserialize( + await fileResponse.Content.ReadAsStringAsync(), _jsonOptions); + + Assert.NotNull(storedEmployee); + Assert.Equal(id, storedEmployee!.Id); + Assert.True(results.TryGetValue(id, out var apiEmployee)); + Assert.Equivalent(apiEmployee, storedEmployee); + } + } + + /// + public async Task DisposeAsync() + { + if (_app is not null) + { + await _app.StopAsync(); + await _app.DisposeAsync(); + } + } +} \ No newline at end of file diff --git a/Client.Wasm/Client.Wasm.csproj b/Client.Wasm/Client.Wasm.csproj index 0ba9f90c..a221285d 100644 --- a/Client.Wasm/Client.Wasm.csproj +++ b/Client.Wasm/Client.Wasm.csproj @@ -13,7 +13,7 @@ - + diff --git a/Client.Wasm/Components/StudentCard.razor b/Client.Wasm/Components/StudentCard.razor index 661f1181..9cd21467 100644 --- a/Client.Wasm/Components/StudentCard.razor +++ b/Client.Wasm/Components/StudentCard.razor @@ -4,10 +4,10 @@ - Номер №X "Название лабораторной" - Вариант №Х "Название варианта" - Выполнена Фамилией Именем 65ХХ - Ссылка на форк + Номер №3 Интеграционное тестирование + Вариант №40 "Сотрудник Компании" + Выполнена Золотилов Никита 6513 + Ссылка на форк diff --git a/Client.Wasm/Properties/launchSettings.json b/Client.Wasm/Properties/launchSettings.json index 0d824ea7..60120ec3 100644 --- a/Client.Wasm/Properties/launchSettings.json +++ b/Client.Wasm/Properties/launchSettings.json @@ -12,7 +12,7 @@ "http": { "commandName": "Project", "dotnetRunMessages": true, - "launchBrowser": true, + "launchBrowser": false, "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", "applicationUrl": "http://localhost:5127", "environmentVariables": { @@ -22,7 +22,7 @@ "https": { "commandName": "Project", "dotnetRunMessages": true, - "launchBrowser": true, + "launchBrowser": false, "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", "applicationUrl": "https://localhost:7282;http://localhost:5127", "environmentVariables": { @@ -31,7 +31,7 @@ }, "IIS Express": { "commandName": "IISExpress", - "launchBrowser": true, + "launchBrowser": false, "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" diff --git a/Client.Wasm/wwwroot/appsettings.json b/Client.Wasm/wwwroot/appsettings.json index d1fe7ab3..ea68b3ea 100644 --- a/Client.Wasm/wwwroot/appsettings.json +++ b/Client.Wasm/wwwroot/appsettings.json @@ -6,5 +6,5 @@ } }, "AllowedHosts": "*", - "BaseAddress": "" + "BaseAddress": "https://localhost:7147/employee" } diff --git a/CloudDevelopment.AppHost/AppHost.cs b/CloudDevelopment.AppHost/AppHost.cs new file mode 100644 index 00000000..2ac18640 --- /dev/null +++ b/CloudDevelopment.AppHost/AppHost.cs @@ -0,0 +1,53 @@ +using Amazon; +using Aspire.Hosting.LocalStack.Container; + +var builder = DistributedApplication.CreateBuilder(args); + +var cache = builder.AddRedis("employee-cache") + .WithRedisInsight(containerName: "employee-insight"); + +var gateway = builder.AddProject("api-gateway"); + +var awsConfig = builder.AddAWSSDKConfig() + .WithProfile("default") + .WithRegion(RegionEndpoint.EUCentral1); + +var localStack = builder + .AddLocalStack("employee-localstack", awsConfig: awsConfig, configureContainer: container => + { + container.Lifetime = ContainerLifetime.Session; + container.DebugLevel = 1; + container.LogLevel = LocalStackLogLevel.Debug; + container.Port = 4566; + container.AdditionalEnvironmentVariables.Add("DEBUG", "1"); + }); + +var awsResources = builder.AddAWSCloudFormationTemplate("resources", "CloudFormation\\employee-template.yaml", "employee") + .WithReference(awsConfig); + +var minio = builder.AddMinioContainer("employee-minio"); + +for (var i = 0; i < 5; i++) +{ + var service = builder.AddProject($"service-api-{i}", launchProfileName: null) + .WithHttpsEndpoint(port: 15000 + i) + .WithReference(cache, "RedisCache") + .WithReference(awsResources) + .WaitFor(cache) + .WaitFor(awsResources); + gateway.WaitFor(service); +} + +var client = builder.AddProject("employee") + .WaitFor(gateway); + +builder.AddProject("employee-sink") + .WithReference(awsResources) + .WithReference(minio) + .WithEnvironment("AWS__Resources__MinioBucketName", "employee-bucket") + .WaitFor(awsResources) + .WaitFor(minio); + +builder.UseLocalStack(localStack); + +builder.Build().Run(); diff --git a/CloudDevelopment.AppHost/CloudDevelopment.AppHost.csproj b/CloudDevelopment.AppHost/CloudDevelopment.AppHost.csproj new file mode 100644 index 00000000..16132bb7 --- /dev/null +++ b/CloudDevelopment.AppHost/CloudDevelopment.AppHost.csproj @@ -0,0 +1,35 @@ + + + + Exe + net8.0 + enable + enable + 4d29b81c-d306-4bbe-9b5e-f203441bda82 + + + + + + + + + + + + + + + + + + + + + + + Always + + + + diff --git a/CloudDevelopment.AppHost/CloudFormation/employee-template.yaml b/CloudDevelopment.AppHost/CloudFormation/employee-template.yaml new file mode 100644 index 00000000..685eb030 --- /dev/null +++ b/CloudDevelopment.AppHost/CloudFormation/employee-template.yaml @@ -0,0 +1,32 @@ +AWSTemplateFormatVersion: '2010-09-09' +Description: 'CloudFormation template for employee project' + +Parameters: + QueueName: + Type: String + Description: Name for the SQS queue + Default: 'employee-queue' + +Resources: + EmployeeQueue: + Type: AWS::SQS::Queue + Properties: + QueueName: !Ref QueueName + VisibilityTimeout: 30 + MessageRetentionPeriod: 345600 + DelaySeconds: 0 + ReceiveMessageWaitTimeSeconds: 0 + Tags: + - Key: Name + Value: !Ref QueueName + - Key: Environment + Value: LocalDev + +Outputs: + SQSQueueArn: + Description: ARN of the SQS queue + Value: !GetAtt EmployeeQueue.Arn + + SQSQueueUrl: + Description: URL of the SQS queue + Value: !Ref EmployeeQueue \ No newline at end of file diff --git a/CloudDevelopment.AppHost/Properties/launchSettings.json b/CloudDevelopment.AppHost/Properties/launchSettings.json new file mode 100644 index 00000000..596f0aad --- /dev/null +++ b/CloudDevelopment.AppHost/Properties/launchSettings.json @@ -0,0 +1,31 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:17059;http://localhost:15263", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21068", + "ASPIRE_DASHBOARD_MCP_ENDPOINT_URL": "https://localhost:23203", + "ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22043" + } + }, + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:15263", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19234", + "ASPIRE_DASHBOARD_MCP_ENDPOINT_URL": "http://localhost:18255", + "ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20127" + } + } + } +} diff --git a/CloudDevelopment.AppHost/appsettings.Development.json b/CloudDevelopment.AppHost/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/CloudDevelopment.AppHost/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/CloudDevelopment.AppHost/appsettings.json b/CloudDevelopment.AppHost/appsettings.json new file mode 100644 index 00000000..a6b256bb --- /dev/null +++ b/CloudDevelopment.AppHost/appsettings.json @@ -0,0 +1,12 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning", + "Aspire.Hosting.Dcp": "Warning" + } + }, + "LocalStack": { + "UseLocalStack": true + } +} diff --git a/CloudDevelopment.ServiceDefaults/CloudDevelopment.ServiceDefaults.csproj b/CloudDevelopment.ServiceDefaults/CloudDevelopment.ServiceDefaults.csproj new file mode 100644 index 00000000..8ad67261 --- /dev/null +++ b/CloudDevelopment.ServiceDefaults/CloudDevelopment.ServiceDefaults.csproj @@ -0,0 +1,22 @@ + + + + net8.0 + enable + enable + true + + + + + + + + + + + + + + + diff --git a/CloudDevelopment.ServiceDefaults/Extensions.cs b/CloudDevelopment.ServiceDefaults/Extensions.cs new file mode 100644 index 00000000..224d3d90 --- /dev/null +++ b/CloudDevelopment.ServiceDefaults/Extensions.cs @@ -0,0 +1,126 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Diagnostics.HealthChecks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Diagnostics.HealthChecks; +using Microsoft.Extensions.Logging; +using OpenTelemetry; +using OpenTelemetry.Metrics; +using OpenTelemetry.Trace; + +namespace Microsoft.Extensions.Hosting; + +// Adds common Aspire services: service discovery, resilience, health checks, and OpenTelemetry. +// This project should be referenced by each service project in your solution. +// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults +public static class Extensions +{ + private const string HealthEndpointPath = "/health"; + private const string AlivenessEndpointPath = "/alive"; + + public static TBuilder AddServiceDefaults(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + builder.ConfigureOpenTelemetry(); + + builder.AddDefaultHealthChecks(); + + builder.Services.AddServiceDiscovery(); + + builder.Services.ConfigureHttpClientDefaults(http => + { + // Turn on resilience by default + http.AddStandardResilienceHandler(); + + // Turn on service discovery by default + http.AddServiceDiscovery(); + }); + + // Uncomment the following to restrict the allowed schemes for service discovery. + // builder.Services.Configure(options => + // { + // options.AllowedSchemes = ["https"]; + // }); + + return builder; + } + + public static TBuilder ConfigureOpenTelemetry(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + builder.Logging.AddOpenTelemetry(logging => + { + logging.IncludeFormattedMessage = true; + logging.IncludeScopes = true; + }); + + builder.Services.AddOpenTelemetry() + .WithMetrics(metrics => + { + metrics.AddAspNetCoreInstrumentation() + .AddHttpClientInstrumentation() + .AddRuntimeInstrumentation(); + }) + .WithTracing(tracing => + { + tracing.AddSource(builder.Environment.ApplicationName) + .AddAspNetCoreInstrumentation(tracing => + // Exclude health check requests from tracing + tracing.Filter = context => + !context.Request.Path.StartsWithSegments(HealthEndpointPath) + && !context.Request.Path.StartsWithSegments(AlivenessEndpointPath) + ) + // Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package) + //.AddGrpcClientInstrumentation() + .AddHttpClientInstrumentation(); + }); + + builder.AddOpenTelemetryExporters(); + + return builder; + } + + private static TBuilder AddOpenTelemetryExporters(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]); + + if (useOtlpExporter) + { + builder.Services.AddOpenTelemetry().UseOtlpExporter(); + } + + // Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package) + //if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"])) + //{ + // builder.Services.AddOpenTelemetry() + // .UseAzureMonitor(); + //} + + return builder; + } + + public static TBuilder AddDefaultHealthChecks(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + builder.Services.AddHealthChecks() + // Add a default liveness check to ensure app is responsive + .AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]); + + return builder; + } + + public static WebApplication MapDefaultEndpoints(this WebApplication app) + { + // Adding health checks endpoints to applications in non-development environments has security implications. + // See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments. + if (app.Environment.IsDevelopment()) + { + // All health checks must pass for app to be considered ready to accept traffic after starting + app.MapHealthChecks(HealthEndpointPath); + + // Only health checks tagged with the "live" tag must pass for app to be considered alive + app.MapHealthChecks(AlivenessEndpointPath, new HealthCheckOptions + { + Predicate = r => r.Tags.Contains("live") + }); + } + + return app; + } +} diff --git a/CloudDevelopment.sln b/CloudDevelopment.sln index cb48241d..b2787967 100644 --- a/CloudDevelopment.sln +++ b/CloudDevelopment.sln @@ -1,10 +1,22 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.14.36811.4 +# Visual Studio Version 18 +VisualStudioVersion = 18.5.11605.296 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client.Wasm", "Client.Wasm\Client.Wasm.csproj", "{AE7EEA74-2FE0-136F-D797-854FD87E022A}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Service.Api", "Service.Api\Service.Api.csproj", "{80A9FC01-11CE-A33B-47BE-CACEC33F4A47}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CloudDevelopment.AppHost", "CloudDevelopment.AppHost\CloudDevelopment.AppHost.csproj", "{10372068-9964-4BA3-8F2A-4A334E5D1301}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CloudDevelopment.ServiceDefaults", "CloudDevelopment.ServiceDefaults\CloudDevelopment.ServiceDefaults.csproj", "{DC017A15-5E73-C618-2A78-CD0D64478DC9}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Api.GateWay", "Api.GateWay\Api.GateWay.csproj", "{C99E72F4-9BA7-7D56-C88E-FB28534EFCB6}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Event.Sink", "Event.Sink\Event.Sink.csproj", "{7FFF40C3-40F4-1344-C1E9-0979075A9AD3}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.AppHost.Tests", "Aspire.AppHost.Tests\Aspire.AppHost.Tests.csproj", "{53974365-3678-FAB6-8CF8-A97B45620415}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +27,30 @@ Global {AE7EEA74-2FE0-136F-D797-854FD87E022A}.Debug|Any CPU.Build.0 = Debug|Any CPU {AE7EEA74-2FE0-136F-D797-854FD87E022A}.Release|Any CPU.ActiveCfg = Release|Any CPU {AE7EEA74-2FE0-136F-D797-854FD87E022A}.Release|Any CPU.Build.0 = Release|Any CPU + {80A9FC01-11CE-A33B-47BE-CACEC33F4A47}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {80A9FC01-11CE-A33B-47BE-CACEC33F4A47}.Debug|Any CPU.Build.0 = Debug|Any CPU + {80A9FC01-11CE-A33B-47BE-CACEC33F4A47}.Release|Any CPU.ActiveCfg = Release|Any CPU + {80A9FC01-11CE-A33B-47BE-CACEC33F4A47}.Release|Any CPU.Build.0 = Release|Any CPU + {10372068-9964-4BA3-8F2A-4A334E5D1301}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {10372068-9964-4BA3-8F2A-4A334E5D1301}.Debug|Any CPU.Build.0 = Debug|Any CPU + {10372068-9964-4BA3-8F2A-4A334E5D1301}.Release|Any CPU.ActiveCfg = Release|Any CPU + {10372068-9964-4BA3-8F2A-4A334E5D1301}.Release|Any CPU.Build.0 = Release|Any CPU + {DC017A15-5E73-C618-2A78-CD0D64478DC9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DC017A15-5E73-C618-2A78-CD0D64478DC9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DC017A15-5E73-C618-2A78-CD0D64478DC9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DC017A15-5E73-C618-2A78-CD0D64478DC9}.Release|Any CPU.Build.0 = Release|Any CPU + {C99E72F4-9BA7-7D56-C88E-FB28534EFCB6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C99E72F4-9BA7-7D56-C88E-FB28534EFCB6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C99E72F4-9BA7-7D56-C88E-FB28534EFCB6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C99E72F4-9BA7-7D56-C88E-FB28534EFCB6}.Release|Any CPU.Build.0 = Release|Any CPU + {7FFF40C3-40F4-1344-C1E9-0979075A9AD3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7FFF40C3-40F4-1344-C1E9-0979075A9AD3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7FFF40C3-40F4-1344-C1E9-0979075A9AD3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7FFF40C3-40F4-1344-C1E9-0979075A9AD3}.Release|Any CPU.Build.0 = Release|Any CPU + {53974365-3678-FAB6-8CF8-A97B45620415}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {53974365-3678-FAB6-8CF8-A97B45620415}.Debug|Any CPU.Build.0 = Debug|Any CPU + {53974365-3678-FAB6-8CF8-A97B45620415}.Release|Any CPU.ActiveCfg = Release|Any CPU + {53974365-3678-FAB6-8CF8-A97B45620415}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Event.Sink/Controllers/S3StorageController.cs b/Event.Sink/Controllers/S3StorageController.cs new file mode 100644 index 00000000..a4af5ce1 --- /dev/null +++ b/Event.Sink/Controllers/S3StorageController.cs @@ -0,0 +1,69 @@ +using Event.Sink.Storage; +using Microsoft.AspNetCore.Mvc; +using System.Text; +using System.Text.Json.Nodes; + +namespace Event.Sink.Controllers; + +/// +/// Контроллер для взаимодействия с S3 +/// +/// Служба для работы с S3 +/// Логгер +[ApiController] +[Route("api/files")] +public class S3StorageController(IS3Service storageService, ILogger logger) : ControllerBase +{ + /// + /// Получает список хранящихся в S3 файлов + /// + /// Список ключей файлов + [HttpGet] + [ProducesResponseType(typeof(List), 200)] + [ProducesResponseType(500)] + public async Task>> ListFiles() + { + logger.LogInformation("The {method} method of the {controller} controller has been called", nameof(ListFiles), nameof(S3StorageController)); + try + { + var list = await storageService.GetFileList(); + logger.LogInformation("Received a list of {count} files from the bucket", list.Count); + return Ok(list); + } + catch (Exception ex) + { + logger.LogError(ex, "Error when executing the {method} method of the {controller} controller", nameof(ListFiles), nameof(S3StorageController)); + return BadRequest(ex.Message); + } + } + + /// + /// Получает строковое представление хранящегося в S3 файла + /// + /// Ключ файла + /// Строковое представление файла + [HttpGet("{key}")] + [ProducesResponseType(typeof(JsonNode), 200)] + [ProducesResponseType(404)] + [ProducesResponseType(500)] + public async Task> GetFile(string key) + { + logger.LogInformation("The {method} method of the {controller} controller has been called", nameof(GetFile), nameof(S3StorageController)); + try + { + var node = await storageService.DownloadFile(key); + logger.LogInformation("Received JSON of {size} bytes", Encoding.UTF8.GetByteCount(node.ToJsonString())); + return Ok(node); + } + catch (InvalidOperationException ex) when (ex.Message.Contains("Error occurred downloading")) + { + logger.LogWarning(ex, "The {key} file was not found", key); + return NotFound($"File '{key}' not found"); + } + catch (Exception ex) + { + logger.LogError(ex, "Error downloading the {key} file", key); + return BadRequest(ex.Message); + } + } +} diff --git a/Event.Sink/Event.Sink.csproj b/Event.Sink/Event.Sink.csproj new file mode 100644 index 00000000..4eb041a0 --- /dev/null +++ b/Event.Sink/Event.Sink.csproj @@ -0,0 +1,20 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + + + diff --git a/Event.Sink/Messaging/SQSConsumerService.cs b/Event.Sink/Messaging/SQSConsumerService.cs new file mode 100644 index 00000000..be01de01 --- /dev/null +++ b/Event.Sink/Messaging/SQSConsumerService.cs @@ -0,0 +1,106 @@ +using Amazon.SQS; +using Amazon.SQS.Model; +using Event.Sink.Storage; + +namespace Event.Sink.Messaging; + +/// +/// Клиентская служба для приема сррьщений из очереди SQS +/// +/// Клиент SQS +/// Фабрика контекста +/// Конфигурация +/// Логгер +public class SqsConsumerService(IAmazonSQS sqsClient, + IServiceScopeFactory scopeFactory, + IConfiguration configuration, + ILogger logger) : BackgroundService +{ + private readonly string _queueUrl = configuration["AWS:Resources:SQSQueueUrl"] + ?? throw new KeyNotFoundException("SQS queue name was not found in configuration"); + + /// + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + logger.LogInformation("SQS consumer service started."); + + while (!stoppingToken.IsCancellationRequested) + { + var response = await sqsClient.ReceiveMessageAsync( + new ReceiveMessageRequest + { + QueueUrl = _queueUrl, + MaxNumberOfMessages = 10, + WaitTimeSeconds = 5 + }, stoppingToken); + + if (response?.Messages == null || response.Messages.Count == 0) + { + logger.LogWarning("Received null or empty from {queue}", _queueUrl); + continue; + } + + logger.LogInformation("Received {count} messages", response.Messages.Count); + + foreach (var message in response.Messages) + { + await ProcessMessageAsync(message, stoppingToken); + } + + logger.LogInformation("Batch of {count} messages processed", response.Messages.Count); + } + } + + /// + /// Сохраняет тело сообщения в объектное хранилище и удаляет сообщение из очереди + /// + /// Сообщение SQS + /// Токен отмены + /// + private async Task ProcessMessageAsync(Message message, CancellationToken stoppingToken) + { + try + { + logger.LogInformation("Processing message: {messageId}", message.MessageId); + + using var scope = scopeFactory.CreateScope(); + var s3Service = scope.ServiceProvider.GetRequiredService(); + var uploaded = await s3Service.UploadFile(message.Body); + + if (uploaded) + { + await sqsClient.DeleteMessageAsync(_queueUrl, message.ReceiptHandle, stoppingToken); + logger.LogInformation("Message {messageId} processed and deleted", message.MessageId); + } + else + { + logger.LogWarning("UploadFile returned false for message {messageId}, deleting to avoid loop", message.MessageId); + await SafeDeleteMessageAsync(message.ReceiptHandle, message.MessageId, stoppingToken); + } + } + catch (Exception ex) + { + logger.LogError(ex, "Error processing message: {messageId}", message.MessageId); + await SafeDeleteMessageAsync(message.ReceiptHandle, message.MessageId, stoppingToken); + } + } + + /// + /// Удаляет сообщение из очереди + /// + /// Идентификатор получения сообщения выдаваемый SQS при чтении + /// Идентификатор сообщения + /// Токен отмены + /// + private async Task SafeDeleteMessageAsync(string receiptHandle, string messageId, CancellationToken stoppingToken) + { + try + { + await sqsClient.DeleteMessageAsync(_queueUrl, receiptHandle, stoppingToken); + } + catch (Exception deleteEx) + { + logger.LogError(deleteEx, "Failed to delete message {messageId}", messageId); + } + } +} \ No newline at end of file diff --git a/Event.Sink/Program.cs b/Event.Sink/Program.cs new file mode 100644 index 00000000..631a9b9b --- /dev/null +++ b/Event.Sink/Program.cs @@ -0,0 +1,37 @@ +using Amazon.SQS; +using Event.Sink.Messaging; +using Event.Sink.Storage; +using LocalStack.Client.Extensions; + +var builder = WebApplication.CreateBuilder(args); + +builder.AddServiceDefaults(); + +builder.Services.AddControllers(); +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); + +builder.Services.AddLocalStack(builder.Configuration); +builder.Services.AddAwsService(); +builder.Services.AddHostedService(); + +builder.AddMinioClient("employee-minio"); +builder.Services.AddScoped(); + +var app = builder.Build(); + +using (var scope = app.Services.CreateScope()) +{ + var storage = scope.ServiceProvider.GetRequiredService(); + await storage.EnsureBucketExists(); +} + +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.MapDefaultEndpoints(); +app.MapControllers(); +app.Run(); \ No newline at end of file diff --git a/Event.Sink/Properties/launchSettings.json b/Event.Sink/Properties/launchSettings.json new file mode 100644 index 00000000..5c34fedd --- /dev/null +++ b/Event.Sink/Properties/launchSettings.json @@ -0,0 +1,40 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:12660", + "sslPort": 44375 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5225", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7178;http://localhost:5225", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/Event.Sink/Storage/IS3Service.cs b/Event.Sink/Storage/IS3Service.cs new file mode 100644 index 00000000..b53b4668 --- /dev/null +++ b/Event.Sink/Storage/IS3Service.cs @@ -0,0 +1,34 @@ +using System.Text.Json.Nodes; + +namespace Event.Sink.Storage; + +/// +/// Интерфейс службы для манипуляции файлами в объектном хранилище +/// +public interface IS3Service +{ + /// + /// Отправляет файл в хранилище + /// + /// Строковое представление сохраняемого файла + public Task UploadFile(string fileData); + + /// + /// Получает список всех файлов из хранилища + /// + /// Список путей к файлам + public Task> GetFileList(); + + /// + /// Получает строковое представление файла из хранилища + /// + /// Путь к файлу в бакете + /// Строковое представление прочтенного файла + public Task DownloadFile(string filePath); + + /// + /// Создает S3 бакет при необходимости + /// + /// + public Task EnsureBucketExists(); +} diff --git a/Event.Sink/Storage/S3MinioService.cs b/Event.Sink/Storage/S3MinioService.cs new file mode 100644 index 00000000..e2678c88 --- /dev/null +++ b/Event.Sink/Storage/S3MinioService.cs @@ -0,0 +1,142 @@ +using Minio; +using Minio.DataModel.Args; +using System.Net; +using System.Text; +using System.Text.Json.Nodes; + +namespace Event.Sink.Storage; + +/// +/// Служба для манипуляции файлами в объектном хранилище Minio +/// +/// Minio клиент +/// Конфигурация +/// Логгер +public class S3MinioService(IMinioClient client, IConfiguration configuration, ILogger logger) : IS3Service +{ + private readonly string _bucketName = configuration["AWS:Resources:MinioBucketName"] + ?? throw new KeyNotFoundException("Minio bucket name was not found in configuration"); + + /// + public async Task> GetFileList() + { + var list = new List(); + var request = new ListObjectsArgs() + .WithBucket(_bucketName) + .WithPrefix("") + .WithRecursive(true); + logger.LogInformation("Requesting a list of files in the bucket {bucket}", _bucketName); + var responseList = client.ListObjectsEnumAsync(request); + + await foreach (var item in responseList) + { + if (item != null) + list.Add(item.Key); + } + + if (list.Count == 0) + logger.LogWarning("The bucket {bucket} is empty or does not contain any objects", _bucketName); + else + logger.LogInformation("{count} files received from the {bucket} bucket", list.Count, _bucketName); + + return list; + } + + /// + public async Task UploadFile(string fileData) + { + var rootNode = JsonNode.Parse(fileData) ?? throw new ArgumentException("Passed string is not a valid JSON"); + var id = rootNode["id"]?.GetValue() ?? throw new ArgumentException("Passed JSON has invalid structure"); + + var bytes = Encoding.UTF8.GetBytes(fileData); + using var stream = new MemoryStream(bytes); + stream.Seek(0, SeekOrigin.Begin); + + logger.LogInformation("Starting uploading the employee {file} to the bucket {bucket}", id, _bucketName); + var request = new PutObjectArgs() + .WithBucket(_bucketName) + .WithStreamData(stream) + .WithObjectSize(bytes.Length) + .WithObject($"employee_{id}.json"); + + try + { + var response = await client.PutObjectAsync(request); + if (response.ResponseStatusCode == HttpStatusCode.OK) + { + logger.LogInformation("Employee {Id} successfully uploaded to bucket {bucket}", id, _bucketName); + return true; + } + + logger.LogError("Couldn't upload employee {Id}: status {statusCode}", id, response.ResponseStatusCode); + return false; + } + catch (Exception ex) + { + logger.LogError(ex, "Error when uploading an employee {Id}", id); + return false; + } + } + + /// + public async Task DownloadFile(string key) + { + logger.LogInformation("Starting downloading the {file} file from the {bucket} bucket", key, _bucketName); + + try + { + var memoryStream = new MemoryStream(); + + var request = new GetObjectArgs() + .WithBucket(_bucketName) + .WithObject(key) + .WithCallbackStream(async (stream, cancellationToken) => + { + await stream.CopyToAsync(memoryStream, cancellationToken); + memoryStream.Seek(0, SeekOrigin.Begin); + }); + + var response = await client.GetObjectAsync(request); + + if (response == null) + { + logger.LogError("Couldn't download the file {file}", key); + throw new InvalidOperationException($"Error occurred downloading {key}"); + } + using var reader = new StreamReader(memoryStream, Encoding.UTF8); + return JsonNode.Parse(reader.ReadToEnd()) ?? throw new InvalidOperationException("Downloaded document is not a valid JSON"); + } + catch (Exception ex) + { + logger.LogError(ex, "Error when downloading a file {file}", key); + throw; + } + } + + /// + public async Task EnsureBucketExists() + { + logger.LogInformation("Checking the existence of a bucket {bucket}", _bucketName); + try + { + var request = new BucketExistsArgs() + .WithBucket(_bucketName); + + var exists = await client.BucketExistsAsync(request); + if (!exists) + { + logger.LogInformation("Creating bucket {bucket}", _bucketName); + var createRequest = new MakeBucketArgs() + .WithBucket(_bucketName); + await client.MakeBucketAsync(createRequest); + return; + } + logger.LogInformation("Bucket {bucket} already exists", _bucketName); + } + catch (Exception ex) + { + logger.LogError(ex, "An unhandled error when checking a bucket {bucket}", _bucketName); + throw; + } + } +} \ No newline at end of file diff --git a/Event.Sink/appsettings.Development.json b/Event.Sink/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/Event.Sink/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Event.Sink/appsettings.json b/Event.Sink/appsettings.json new file mode 100644 index 00000000..10f68b8c --- /dev/null +++ b/Event.Sink/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/Service.Api/Caching/CacheService.cs b/Service.Api/Caching/CacheService.cs new file mode 100644 index 00000000..59c97963 --- /dev/null +++ b/Service.Api/Caching/CacheService.cs @@ -0,0 +1,54 @@ +using Microsoft.Extensions.Caching.Distributed; +using Service.Api.Entities; +using System.Text.Json; + +namespace Service.Api.Caching; + +/// +/// Служба для с кэшем сотрудников компании +/// +/// Кэш +/// Логгер +public class CacheService(IDistributedCache cache, ILogger logger) : ICacheService +{ + /// + /// Время жизни кэша + /// + private static readonly TimeSpan _cacheExpiration = TimeSpan.FromMinutes(5); + + /// + public async Task RetrieveFromCache(int id) + { + try + { + var json = await cache.GetStringAsync(id.ToString()); + if (string.IsNullOrEmpty(json)) + return null; + return JsonSerializer.Deserialize(json); + } + catch (Exception ex) + { + logger.LogError(ex, "Error retrieving employee {EmployeeId} from cache", id); + return null; + } + } + + /// + public async Task PopulateCache(Employee employee) + { + try + { + var json = JsonSerializer.Serialize(employee); + await cache.SetStringAsync(employee.Id.ToString(), json, + new DistributedCacheEntryOptions + { + AbsoluteExpirationRelativeToNow = _cacheExpiration + }); + logger.LogDebug("Successfully cached employee {EmployeeId}", employee.Id); + } + catch (Exception ex) + { + logger.LogError(ex, "Failed to cache employee {EmployeeId}", employee.Id); + } + } +} diff --git a/Service.Api/Caching/ICacheService.cs b/Service.Api/Caching/ICacheService.cs new file mode 100644 index 00000000..c4251484 --- /dev/null +++ b/Service.Api/Caching/ICacheService.cs @@ -0,0 +1,23 @@ +using Service.Api.Entities; + +namespace Service.Api.Caching; + +/// +/// Интерфейс для работы с кэшем сотрудников компании +/// +public interface ICacheService +{ + /// + /// Пытается достать сотрудника из кэша + /// + /// Идентификатор + /// Сотрудника компании или null + public Task RetrieveFromCache(int id); + + /// + /// Кладёт сотрудника в кэш + /// + /// Сотрудник компании + /// + public Task PopulateCache(Employee employee); +} diff --git a/Service.Api/Entities/Employee.cs b/Service.Api/Entities/Employee.cs new file mode 100644 index 00000000..bf5a224a --- /dev/null +++ b/Service.Api/Entities/Employee.cs @@ -0,0 +1,69 @@ +using System.Text.Json.Serialization; + +namespace Service.Api.Entities; + +/// +/// Сотрудник компании +/// +public class Employee +{ + /// + /// Идентификатор + /// + [JsonPropertyName("id")] + public int Id { get; set; } + + /// + /// ФИО + /// + [JsonPropertyName("fullName")] + public required string FullName { get; set; } + + /// + /// Должность + /// + [JsonPropertyName("post")] + public required string Post { get; set; } + + /// + /// Отдел + /// + [JsonPropertyName("department")] + public required string Department { get; set; } + + /// + /// Дата приема + /// + [JsonPropertyName("hireDate ")] + public required DateOnly HireDate { get; set; } + + /// + /// Оклад + /// + [JsonPropertyName("salary")] + public required decimal Salary { get; set; } + + /// + /// Электронная почта + /// + [JsonPropertyName("email")] + public required string Email { get; set; } + + /// + /// Номер телефона + /// + [JsonPropertyName("phone")] + public required string Phone { get; set; } + + /// + /// Индикатор увольнения + /// + [JsonPropertyName("isFired")] + public required bool IsFired { get; set; } + + /// + /// Дата увольнения + /// + [JsonPropertyName("fireDate ")] + public DateOnly? FireDate { get; set; } +} diff --git a/Service.Api/Generator/EmployeeGenerator.cs b/Service.Api/Generator/EmployeeGenerator.cs new file mode 100644 index 00000000..84407727 --- /dev/null +++ b/Service.Api/Generator/EmployeeGenerator.cs @@ -0,0 +1,64 @@ +using Bogus; +using Service.Api.Entities; + +namespace Service.Api.Generator; + +/// +/// Генератор сотрудников компании со случайными свойствами +/// +public static class EmployeeGenerator +{ + /// + /// Справочник категорий профессий + /// + private static readonly string[] _professions = { "Developer", "Manager", "Analyst", "Designer", "QA" }; + + /// + /// Словарь связи категории должности с заработной платой + /// + private static readonly Dictionary _salaryRanges = new() + { + ["Junior"] = (30000m, 60000m), + ["Middle"] = (60000m, 120000m), + ["Senior"] = (120000m, 200000m), + ["Lead"] = (150000m, 250000m) + }; + + /// + /// Справочник категорий суффиксов должностей + /// + private static readonly string[] _suffixes = [.. _salaryRanges.Keys]; + + private static readonly Faker _faker = new Faker("ru") + .RuleFor(e => e.Id, f => f.IndexFaker + 1) + .RuleFor(e => e.FullName, f => f.Name.FullName()) + .RuleFor(e => e.Post, f => f.PickRandom(_suffixes) + " " + f.PickRandom(_professions)) + .RuleFor(e => e.Department, f => f.Commerce.Department()) + .RuleFor(e => e.HireDate, f => DateOnly.FromDateTime(f.Date.Past(10))) + .RuleFor(e => e.Salary, (f, e) => + { + var suffix = _suffixes.FirstOrDefault(s => e.Post.Contains(s)); + if (suffix != null) + { + var (min, max) = _salaryRanges[suffix]; + return Math.Round(f.Random.Decimal(min, max), 2); + } + return Math.Round(f.Random.Decimal(40000m, 100000m), 2); + }) + .RuleFor(e => e.Email, f => f.Internet.Email()) + .RuleFor(e => e.Phone, f => f.Phone.PhoneNumber("+7(###)###-##-##")) + .RuleFor(e => e.IsFired, f => f.Random.Bool(0.2f)) + .RuleFor(e => e.FireDate, (f, e) => e.IsFired ? DateOnly.FromDateTime(f.Date.Past(1)) : null); + + /// + /// Метод генерации СК + /// + /// Идентификатор + /// Сотрудник компании + public static Employee Generate(int id) + { + var employee = _faker.Generate(); + employee.Id = id; + return employee; + } +} diff --git a/Service.Api/Generator/EmployeeService.cs b/Service.Api/Generator/EmployeeService.cs new file mode 100644 index 00000000..8b86414d --- /dev/null +++ b/Service.Api/Generator/EmployeeService.cs @@ -0,0 +1,43 @@ +using Service.Api.Caching; +using Service.Api.Entities; +using Service.Api.Messaging; + +namespace Service.Api.Generator; + +/// +/// Служба для запуска юзкейса по обработке сотрудников компании +/// +/// Кэш +/// Логгер +public class EmployeeService(ICacheService cache, ILogger logger, IProducerService messagingService) : IEmployeeService +{ + /// + public async Task ProcessEmployee(int id) + { + try + { + logger.LogInformation("Processing employee request for ID: {EmployeeId}", id); + + var employee = await cache.RetrieveFromCache(id); + if (employee != null) + { + logger.LogInformation("Cache HIT for employee {EmployeeId}", id); + return employee; + } + + logger.LogInformation("Cache MISS for employee {EmployeeId}. Generating new data.", id); + employee = EmployeeGenerator.Generate(id); + await messagingService.SendMessage(employee); + logger.LogInformation("Populating the cache with employee {id}", id); + + _ = Task.Run(() => cache.PopulateCache(employee)); + + return employee; + } + catch (Exception ex) + { + logger.LogError(ex, "Unexpected error while processing employee {EmployeeId}", id); + throw; + } + } +} diff --git a/Service.Api/Generator/IEmployeeService.cs b/Service.Api/Generator/IEmployeeService.cs new file mode 100644 index 00000000..f810d198 --- /dev/null +++ b/Service.Api/Generator/IEmployeeService.cs @@ -0,0 +1,16 @@ +using Service.Api.Entities; + +namespace Service.Api.Generator; + +/// +/// Интерфейс для запуска юзкейса по обработке сотрудников компании +/// +public interface IEmployeeService +{ + /// + /// Обработка запроса на генерацию сотрудника компании + /// + /// Идентификатор + /// Сотрудник компании + public Task ProcessEmployee(int id); +} diff --git a/Service.Api/Messaging/IProducerService.cs b/Service.Api/Messaging/IProducerService.cs new file mode 100644 index 00000000..2476c8cf --- /dev/null +++ b/Service.Api/Messaging/IProducerService.cs @@ -0,0 +1,15 @@ +using Service.Api.Entities; + +namespace Service.Api.Messaging; + +/// +/// Интерфейс для отправки генерируемых сотрудников в брокер сообщений +/// +public interface IProducerService +{ + /// + /// Отправляет сообщение в брокер + /// + /// Сотрудник компании + public Task SendMessage(Employee employee); +} diff --git a/Service.Api/Messaging/ProducerService.cs b/Service.Api/Messaging/ProducerService.cs new file mode 100644 index 00000000..6b367a01 --- /dev/null +++ b/Service.Api/Messaging/ProducerService.cs @@ -0,0 +1,58 @@ +using Amazon.SQS; +using Service.Api.Entities; +using System.Net; +using System.Text.Json; + +namespace Service.Api.Messaging; + +/// +/// Класс для отправки сообщений в брокер +/// +/// Клиент SQS +/// Конфигурация +/// Логгер +public class ProducerService(IAmazonSQS client, IConfiguration configuration, ILogger logger) : IProducerService +{ + private readonly string _queueUrl = configuration["AWS:Resources:SQSQueueUrl"] + ?? throw new KeyNotFoundException("SQS queue link was not found in configuration"); + + private readonly int _maxRetries = 3; + private readonly TimeSpan _initialDelay = TimeSpan.FromSeconds(1); + + /// + public async Task SendMessage(Employee employee) + { + var delay = _initialDelay; + + for (var attempt = 1; attempt <= _maxRetries; attempt++) + { + try + { + var json = JsonSerializer.Serialize(employee); + var response = await client.SendMessageAsync(_queueUrl, json); + + if (response.HttpStatusCode == HttpStatusCode.OK) + { + logger.LogInformation("Employee {Id} sent to SQS queue (attempt {attempt})", employee.Id, attempt); + return; + } + + logger.LogWarning("Send returned {statusCode} on attempt {attempt}", response.HttpStatusCode, attempt); + } + catch (Exception ex) when (attempt < _maxRetries) + { + logger.LogWarning(ex, "Attempt {attempt} failed, retrying...", attempt); + } + + if (attempt < _maxRetries) + { + logger.LogDebug("Waiting {delay} before next retry", delay); + await Task.Delay(delay); + delay = delay * 2; + } + } + + logger.LogError("Failed to send employee {Id} after {maxRetries} attempts", employee.Id, _maxRetries); + } + +} diff --git a/Service.Api/Program.cs b/Service.Api/Program.cs new file mode 100644 index 00000000..1b7eb4f6 --- /dev/null +++ b/Service.Api/Program.cs @@ -0,0 +1,23 @@ +using Amazon.SQS; +using LocalStack.Client.Extensions; +using Service.Api.Caching; +using Service.Api.Generator; +using Service.Api.Messaging; + +var builder = WebApplication.CreateBuilder(args); + +builder.AddServiceDefaults(); +builder.AddRedisDistributedCache("RedisCache"); + +builder.Services.AddScoped(); +builder.Services.AddScoped(); + +builder.Services.AddLocalStack(builder.Configuration); +builder.Services.AddAwsService(); +builder.Services.AddScoped(); + +var app = builder.Build(); + +app.MapDefaultEndpoints(); +app.MapGet("/employee", (IEmployeeService service, int id) => service.ProcessEmployee(id)); +app.Run(); diff --git a/Service.Api/Properties/launchSettings.json b/Service.Api/Properties/launchSettings.json new file mode 100644 index 00000000..908fc6dc --- /dev/null +++ b/Service.Api/Properties/launchSettings.json @@ -0,0 +1,38 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:58376", + "sslPort": 44394 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:5088", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:7111;http://localhost:5088", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/Service.Api/Service.Api.csproj b/Service.Api/Service.Api.csproj new file mode 100644 index 00000000..eac317c4 --- /dev/null +++ b/Service.Api/Service.Api.csproj @@ -0,0 +1,21 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + + + + diff --git a/Service.Api/appsettings.Development.json b/Service.Api/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/Service.Api/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Service.Api/appsettings.json b/Service.Api/appsettings.json new file mode 100644 index 00000000..10f68b8c --- /dev/null +++ b/Service.Api/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +}