From 48c9153aa1895303aa6fb1d92a32859dc01a0361 Mon Sep 17 00:00:00 2001 From: Ion Dormenco Date: Wed, 26 May 2021 17:19:11 +0300 Subject: [PATCH 1/4] Update Program.cs --- backend/src/StamAcasa.JobScheduler/Program.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/backend/src/StamAcasa.JobScheduler/Program.cs b/backend/src/StamAcasa.JobScheduler/Program.cs index 2b5f89a4..40625312 100644 --- a/backend/src/StamAcasa.JobScheduler/Program.cs +++ b/backend/src/StamAcasa.JobScheduler/Program.cs @@ -66,7 +66,15 @@ private static IHostBuilder CreateHostBuilder(string[] args) => services.AddAutoMapper(typeof(Program), typeof(UserDbContext)); services.AddDbContextPool(options => - options.UseNpgsql(hostContext.Configuration.GetConnectionString("UserDBConnection"))); + options.UseNpgsql(hostContext.Configuration.GetConnectionString("UserDBConnection"), sqlOptions => + { + sqlOptions.EnableRetryOnFailure( + maxRetryCount: 5, + maxRetryDelay: TimeSpan.FromSeconds(5), + errorCodesToAdd: null + ); + })); + services.AddSingleton(RabbitHutch.CreateBus(string.Format("host={0}:{1};username={2};password={3}", hostContext.Configuration.GetValue("RabbitMQ:HostName"), From 1cef10ab5cff85948ba97ae15c75ca72f0b4fca3 Mon Sep 17 00:00:00 2001 From: Ion Dormenco Date: Wed, 26 May 2021 17:22:53 +0300 Subject: [PATCH 2/4] Add rabbit mq config in api --- backend/src/StamAcasa.Api/Startup.cs | 8 ++- backend/src/StamAcasa.Api/appsettings.json | 58 ++++++++++++---------- 2 files changed, 39 insertions(+), 27 deletions(-) diff --git a/backend/src/StamAcasa.Api/Startup.cs b/backend/src/StamAcasa.Api/Startup.cs index cf166c03..5386e344 100644 --- a/backend/src/StamAcasa.Api/Startup.cs +++ b/backend/src/StamAcasa.Api/Startup.cs @@ -4,6 +4,7 @@ using System.Net.Http; using System.Threading.Tasks; using AutoMapper; +using EasyNetQ; using Hellang.Middleware.ProblemDetails; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Builder; @@ -95,7 +96,12 @@ public void ConfigureServices(IServiceCollection services) services.ConfigureSwagger(Configuration); services.AddProblemDetails(ConfigureProblemDetails); - + services.AddSingleton(RabbitHutch.CreateBus(string.Format("host={0}:{1};username={2};password={3}", + Configuration.GetValue("RabbitMQ:HostName"), + Configuration.GetValue("RabbitMQ:Port").ToString(), + Configuration.GetValue("RabbitMQ:User"), + Configuration.GetValue("RabbitMQ:Password")) + )); services.AddHostedService(); } diff --git a/backend/src/StamAcasa.Api/appsettings.json b/backend/src/StamAcasa.Api/appsettings.json index 8437ad2b..37ddf852 100644 --- a/backend/src/StamAcasa.Api/appsettings.json +++ b/backend/src/StamAcasa.Api/appsettings.json @@ -1,29 +1,35 @@ { - "ConnectionStrings": { - "UserDBConnection": "Server=postgres;Port=5432;Database=UserDb;User Id=docker;Password=docker;" + "ConnectionStrings": { + "UserDBConnection": "Server=postgres;Port=5432;Database=UserDb;User Id=docker;Password=docker;" + }, + "Logging": { + "IncludeScopes": false, + "LogLevel": { + "Default": "Debug", + "System": "Information", + "Microsoft": "Information" + } + }, + "IdentityServerUrl": "http://localhost:5001", + "InternalIdentityServerUrl": "http://identityserver", + "AllowedCorsOrigins": [ "http://localhost:3000", "http://localhost:5002" ], + "ApiConfiguration": [ + { + "ApiName": "answersApi", + "ApiSecret": "svpqYnJSR8xzn8Rl" }, - "Logging": { - "IncludeScopes": false, - "LogLevel": { - "Default": "Debug", - "System": "Information", - "Microsoft": "Information" - } - }, - "IdentityServerUrl": "http://localhost:5001", - "InternalIdentityServerUrl": "http://identityserver", - "AllowedCorsOrigins": [ "http://localhost:3000", "http://localhost:5002" ], - "ApiConfiguration": [ - { - "ApiName": "answersApi", - "ApiSecret": "svpqYnJSR8xzn8Rl" - }, - { - "ApiName": "usersApi", - "ApiSecret": "st4k!b7s$af201cv" - } - ], - "TargetFolder": "answers", - "StorageType": "FileSystem", - "SwaggerOidcClientName": "swaggerClientLocalhost" + { + "ApiName": "usersApi", + "ApiSecret": "st4k!b7s$af201cv" + } + ], + "TargetFolder": "answers", + "StorageType": "FileSystem", + "SwaggerOidcClientName": "swaggerClientLocalhost", + "RabbitMQ": { + "HostName": "localhost", + "Port": 5672, + "User": "admin", + "Password": "Corona2020" + } } \ No newline at end of file From cc3fa37afbca74fab2356a081522e2d0d859e58e Mon Sep 17 00:00:00 2001 From: Ion Dormenco Date: Thu, 27 May 2021 14:40:24 +0300 Subject: [PATCH 3/4] Fix queue for sending emails to user && fix api --- backend/src/StamAcasa.Api/Startup.cs | 3 +++ backend/src/StamAcasa.Common/Queue/QueueService.cs | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/backend/src/StamAcasa.Api/Startup.cs b/backend/src/StamAcasa.Api/Startup.cs index 5386e344..aa8fd82c 100644 --- a/backend/src/StamAcasa.Api/Startup.cs +++ b/backend/src/StamAcasa.Api/Startup.cs @@ -19,6 +19,7 @@ using StamAcasa.Api.Models; using StamAcasa.Api.Services; using StamAcasa.Common; +using StamAcasa.Common.Queue; using StamAcasa.Common.Services; using StamAcasa.Common.Services.Assessment; using StamAcasa.Common.Services.Excel; @@ -102,6 +103,8 @@ public void ConfigureServices(IServiceCollection services) Configuration.GetValue("RabbitMQ:User"), Configuration.GetValue("RabbitMQ:Password")) )); + + services.AddSingleton(); services.AddHostedService(); } diff --git a/backend/src/StamAcasa.Common/Queue/QueueService.cs b/backend/src/StamAcasa.Common/Queue/QueueService.cs index 47b4aa2f..fcdaf745 100644 --- a/backend/src/StamAcasa.Common/Queue/QueueService.cs +++ b/backend/src/StamAcasa.Common/Queue/QueueService.cs @@ -55,7 +55,7 @@ public async Task PublishNotification(T message) where T : class public async Task PublishEmailRequest(T message) where T : class { var messageWrapper = new Message(message); - await _bus.Advanced.PublishAsync(Exchange.GetDefault(), UserRequestsQueueName, false, messageWrapper); + await _bus.Advanced.PublishAsync(Exchange.GetDefault(), EmailRequestsQueueName, false, messageWrapper); } public async Task PublishUserRequest(T message) where T : class From 8824aef69ce5515ce6ea491735f6d390279c78c5 Mon Sep 17 00:00:00 2001 From: Ion Dormenco Date: Thu, 27 May 2021 20:41:01 +0300 Subject: [PATCH 4/4] Finaly fixed api .... Finaly fix api --- .../BackgroundServices/UserManagementService.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/backend/src/StamAcasa.Api/BackgroundServices/UserManagementService.cs b/backend/src/StamAcasa.Api/BackgroundServices/UserManagementService.cs index f390a1b2..50d35d77 100644 --- a/backend/src/StamAcasa.Api/BackgroundServices/UserManagementService.cs +++ b/backend/src/StamAcasa.Api/BackgroundServices/UserManagementService.cs @@ -9,16 +9,18 @@ namespace StamAcasa.Api.BackgroundServices { + using Microsoft.Extensions.DependencyInjection; + public class UserManagementService : BackgroundService { private readonly ILogger _logger; private readonly IQueueService _queueService; - private readonly IUserService _userService; + private readonly IServiceProvider _serviceProvider; - public UserManagementService(IQueueService queueService, IUserService userService, ILogger logger) + public UserManagementService(IQueueService queueService, IServiceProvider serviceProvider, ILogger logger) { _queueService = queueService; - _userService = userService; + _serviceProvider = serviceProvider; _logger = logger; } @@ -31,9 +33,10 @@ public override Task StartAsync(CancellationToken cancellationToken) { try { + var userService = _serviceProvider.GetService(); _logger.LogInformation($"Will soft delete user with sub= {request.Sub}"); - await _userService.MarkUserAsDeleted(request.Sub); + await userService.MarkUserAsDeleted(request.Sub); _logger.LogInformation($"Done soft delete of user with sub= {request.Sub}"); }