From 891d462af1541bcf7ef06d6cc18b6e02e05eea9a Mon Sep 17 00:00:00 2001 From: "HOME-LAPTOP\\kshkulev" Date: Sat, 25 Oct 2025 20:30:49 +0900 Subject: [PATCH] Add operation executor --- Hcs.Broker.Mock/Api/MockNsiCommonApi.cs | 7 +++++ Hcs.Broker/Api/INsiCommonApi.cs | 10 +++++++ Hcs.Broker/Api/NsiCommonApi.cs | 6 +++++ .../ExportAllRegistryElementsExecutor.cs | 13 ++++++++++ .../Executors/ExecutorBase.cs | 13 ++++++++++ .../Executors/ExecutorFactory.cs | 20 ++++++++++++++ .../BackgroundServices/Executors/IExecutor.cs | 7 +++++ .../OperationExecutionService.cs | 24 ++++++++++++++--- Hcs.WebApp/Components/Dialogs/AddUser.razor | 6 ++--- Hcs.WebApp/Components/Dialogs/EditUser.razor | 6 ++--- .../Components/Pages/Account/Manage.razor | 6 ++--- .../Components/Pages/Management/Users.razor | 26 +++++++++---------- .../Components/Pages/Registry/Common.razor | 26 +++++++++---------- Hcs.WebApp/Components/Pages/Test/Export.razor | 4 +-- Hcs.WebApp/Data/Hcs/Operation.cs | 2 +- Hcs.WebApp/Services/IdentityService.cs | 2 +- Hcs.WebApp/Services/OperationService.cs | 8 +++--- Hcs.WebApp/Services/RegistryService.cs | 2 +- Hcs.WebApp/Services/UsersService.cs | 8 +++--- 19 files changed, 144 insertions(+), 52 deletions(-) create mode 100644 Hcs.WebApp/BackgroundServices/Executors/15_7_0_1/NsiCommon/ExportAllRegistryElementsExecutor.cs create mode 100644 Hcs.WebApp/BackgroundServices/Executors/ExecutorBase.cs create mode 100644 Hcs.WebApp/BackgroundServices/Executors/ExecutorFactory.cs create mode 100644 Hcs.WebApp/BackgroundServices/Executors/IExecutor.cs diff --git a/Hcs.Broker.Mock/Api/MockNsiCommonApi.cs b/Hcs.Broker.Mock/Api/MockNsiCommonApi.cs index 8c50ea0..fcf671d 100644 --- a/Hcs.Broker.Mock/Api/MockNsiCommonApi.cs +++ b/Hcs.Broker.Mock/Api/MockNsiCommonApi.cs @@ -21,5 +21,12 @@ namespace Hcs.Broker.Mock.Api return null; } + + public async Task RequestExportNsiItemAsync(int registryNumber, ListGroup listGroup, CancellationToken token = default) + { + await Task.Delay(3000, token); + + return Guid.NewGuid().ToString(); + } } } diff --git a/Hcs.Broker/Api/INsiCommonApi.cs b/Hcs.Broker/Api/INsiCommonApi.cs index 942420f..22fe164 100644 --- a/Hcs.Broker/Api/INsiCommonApi.cs +++ b/Hcs.Broker/Api/INsiCommonApi.cs @@ -21,5 +21,15 @@ namespace Hcs.Broker.Api /// Токен отмены /// Перечень общесистемных справочников Task ExportNsiListAsync(ListGroup listGroup, CancellationToken token = default); + + + /// + /// Запрашивает экспорт данных общесистемного справочника + /// + /// Реестровый номер справочника + /// Группа справочников, где NSI - общесистемный, а NSIRAO - ОЖФ + /// Токен отмены + /// Идентификатор сообщения операции экспорта + Task RequestExportNsiItemAsync(int registryNumber, ListGroup listGroup, CancellationToken token = default); } } diff --git a/Hcs.Broker/Api/NsiCommonApi.cs b/Hcs.Broker/Api/NsiCommonApi.cs index 10178c2..c93afd5 100644 --- a/Hcs.Broker/Api/NsiCommonApi.cs +++ b/Hcs.Broker/Api/NsiCommonApi.cs @@ -34,5 +34,11 @@ namespace Hcs.Broker.Api return null; } } + + /// + public Task RequestExportNsiItemAsync(int registryNumber, ListGroup listGroup, CancellationToken token = default) + { + throw new NotImplementedException(); + } } } diff --git a/Hcs.WebApp/BackgroundServices/Executors/15_7_0_1/NsiCommon/ExportAllRegistryElementsExecutor.cs b/Hcs.WebApp/BackgroundServices/Executors/15_7_0_1/NsiCommon/ExportAllRegistryElementsExecutor.cs new file mode 100644 index 0000000..aa44fbd --- /dev/null +++ b/Hcs.WebApp/BackgroundServices/Executors/15_7_0_1/NsiCommon/ExportAllRegistryElementsExecutor.cs @@ -0,0 +1,13 @@ +using Hcs.Broker; +using Hcs.WebApp.Data.Hcs; + +namespace Hcs.WebApp.BackgroundServices.Executors._15_7_0_1.NsiCommon +{ + public class ExportAllRegistryElementsExecutor(IClient client, Operation operation) : ExecutorBase(client, operation) + { + public override async Task ExecuteAsync(CancellationToken cancellationToken) + { + return await client.NsiCommon.RequestExportNsiItemAsync(1, Service.Async.NsiCommon.ListGroup.NSI, cancellationToken); + } + } +} diff --git a/Hcs.WebApp/BackgroundServices/Executors/ExecutorBase.cs b/Hcs.WebApp/BackgroundServices/Executors/ExecutorBase.cs new file mode 100644 index 0000000..d1d8737 --- /dev/null +++ b/Hcs.WebApp/BackgroundServices/Executors/ExecutorBase.cs @@ -0,0 +1,13 @@ +using Hcs.Broker; +using Hcs.WebApp.Data.Hcs; + +namespace Hcs.WebApp.BackgroundServices.Executors +{ + public abstract class ExecutorBase(IClient client, Operation operation) : IExecutor + { + protected readonly IClient client = client; + protected readonly Operation operation = operation; + + public abstract Task ExecuteAsync(CancellationToken cancellationToken); + } +} diff --git a/Hcs.WebApp/BackgroundServices/Executors/ExecutorFactory.cs b/Hcs.WebApp/BackgroundServices/Executors/ExecutorFactory.cs new file mode 100644 index 0000000..62a5245 --- /dev/null +++ b/Hcs.WebApp/BackgroundServices/Executors/ExecutorFactory.cs @@ -0,0 +1,20 @@ +using Hcs.Broker; +using Hcs.WebApp.BackgroundServices.Executors._15_7_0_1.NsiCommon; +using Hcs.WebApp.Data.Hcs; + +namespace Hcs.WebApp.BackgroundServices.Executors +{ + public class ExecutorFactory + { + public IExecutor CreateExecutor(IClient client, Operation operation) + { + switch (operation.Type) + { + case Operation.OperationType.NsiCommon_15_7_0_1_ExportAllRegistryElements: + return new ExportAllRegistryElementsExecutor(client, operation); + } + + throw new NotImplementedException(); + } + } +} diff --git a/Hcs.WebApp/BackgroundServices/Executors/IExecutor.cs b/Hcs.WebApp/BackgroundServices/Executors/IExecutor.cs new file mode 100644 index 0000000..df02e3f --- /dev/null +++ b/Hcs.WebApp/BackgroundServices/Executors/IExecutor.cs @@ -0,0 +1,7 @@ +namespace Hcs.WebApp.BackgroundServices.Executors +{ + public interface IExecutor + { + Task ExecuteAsync(CancellationToken cancellationToken); + } +} diff --git a/Hcs.WebApp/BackgroundServices/OperationExecutionService.cs b/Hcs.WebApp/BackgroundServices/OperationExecutionService.cs index bdbd848..9d86881 100644 --- a/Hcs.WebApp/BackgroundServices/OperationExecutionService.cs +++ b/Hcs.WebApp/BackgroundServices/OperationExecutionService.cs @@ -1,6 +1,7 @@ using Hcs.Broker; using Hcs.Broker.Logger; using Hcs.Broker.MessageCapturer; +using Hcs.WebApp.BackgroundServices.Executors; using Hcs.WebApp.Config; using Hcs.WebApp.Services; @@ -12,6 +13,7 @@ namespace Hcs.WebApp.BackgroundServices private readonly OperationExecutionState state = state; private readonly IServiceScopeFactory scopeFactory = scopeFactory; + private readonly ExecutorFactory executorFactory = new(); private Broker.Logger.ILogger logger; private IMessageCapturer messageCapturer; @@ -19,7 +21,7 @@ namespace Hcs.WebApp.BackgroundServices protected override async Task ExecuteAsync(CancellationToken stoppingToken) { - await InitializeState(); + await InitializeStateAsync(); InitializeClient(); @@ -30,19 +32,33 @@ namespace Hcs.WebApp.BackgroundServices { while (state.TryDequeueOperation(out var operation)) { - await operationService.SetOperationMessageGuid(operation.Id, Guid.NewGuid().ToString()); + var messageGuid = string.Empty; + try + { + var executor = executorFactory.CreateExecutor(client, operation); + messageGuid = await executor.ExecuteAsync(stoppingToken); + } + catch + { + state.EnqueueOperation(operation); + } + + if (!string.IsNullOrEmpty(messageGuid)) + { + await operationService.SetOperationMessageGuidAsync(operation.Id, messageGuid); + } } await Task.Delay(SLEEP_TIME, stoppingToken); } } - private async Task InitializeState() + private async Task InitializeStateAsync() { using var scope = scopeFactory.CreateScope(); var operationService = scope.ServiceProvider.GetRequiredService(); - var operations = await operationService.GetInitiatedOperations(); + var operations = await operationService.GetInitiatedOperationsAsync(); foreach (var operation in operations) { state.EnqueueOperation(operation); diff --git a/Hcs.WebApp/Components/Dialogs/AddUser.razor b/Hcs.WebApp/Components/Dialogs/AddUser.razor index 66a45f2..aa0e867 100644 --- a/Hcs.WebApp/Components/Dialogs/AddUser.razor +++ b/Hcs.WebApp/Components/Dialogs/AddUser.razor @@ -11,7 +11,7 @@ - + @errorMessage @@ -88,7 +88,7 @@ Input.Role = roles.First(); } - async Task DoAddUser(InputModel input) + async Task DoAddUserAsync(InputModel input) { if (inProgress) return; @@ -97,7 +97,7 @@ try { - var result = await UsersService.CreateUser(input.UserName, input.Role.Name, input.Password); + var result = await UsersService.CreateUserAsync(input.UserName, input.Role.Name, input.Password); if (result.Succeeded) { DialogService.Close(true); diff --git a/Hcs.WebApp/Components/Dialogs/EditUser.razor b/Hcs.WebApp/Components/Dialogs/EditUser.razor index 076c283..ef27977 100644 --- a/Hcs.WebApp/Components/Dialogs/EditUser.razor +++ b/Hcs.WebApp/Components/Dialogs/EditUser.razor @@ -12,7 +12,7 @@ - + @errorMessage @@ -90,7 +90,7 @@ roles = await RoleManager.Roles.OrderBy(x => x.Priority).ToListAsync(); } - async Task DoEditUser(InputModel input) + async Task DoEditUserAsync(InputModel input) { if (inProgress) return; @@ -99,7 +99,7 @@ try { - await UsersService.UpdateUser(UserId, input.RoleId, input.Password); + await UsersService.UpdateUserAsync(UserId, input.RoleId, input.Password); DialogService.Close(true); } diff --git a/Hcs.WebApp/Components/Pages/Account/Manage.razor b/Hcs.WebApp/Components/Pages/Account/Manage.razor index ef81feb..18d2dd0 100644 --- a/Hcs.WebApp/Components/Pages/Account/Manage.razor +++ b/Hcs.WebApp/Components/Pages/Account/Manage.razor @@ -13,7 +13,7 @@
- + @errorMessage @@ -78,7 +78,7 @@ [SupplyParameterFromForm] PasswordInputModel PasswordInput { get; set; } = new(); - async Task ChangePassword() + async Task ChangePasswordAsync() { hasError = false; hasSuccess = false; @@ -87,7 +87,7 @@ { busyOverlay.Show(); - await IdentityService.ChangePassword(PasswordInput.OldPassword, PasswordInput.NewPassword); + await IdentityService.ChangePasswordAsync(PasswordInput.OldPassword, PasswordInput.NewPassword); hasSuccess = true; } diff --git a/Hcs.WebApp/Components/Pages/Management/Users.razor b/Hcs.WebApp/Components/Pages/Management/Users.razor index be7cff1..5620300 100644 --- a/Hcs.WebApp/Components/Pages/Management/Users.razor +++ b/Hcs.WebApp/Components/Pages/Management/Users.razor @@ -24,7 +24,7 @@ - + @@ -33,7 +33,7 @@ @errorMessage - + @@ -41,7 +41,7 @@ @@ -74,7 +74,7 @@ if (state.User.IsInRole(AppRole.ADMINISTRATOR_TYPE)) { currentUserId = state.User.FindFirst(ClaimTypes.NameIdentifier)!.Value; - usersWithRoles = await UsersService.GetUsersWithRole(); + usersWithRoles = await UsersService.GetUsersWithRoleAsync(); } isLoading = false; @@ -83,7 +83,7 @@ } } - async Task AddUser() + async Task AddUserAsync() { var success = await DialogService.OpenAsync( "Создание пользователя", @@ -98,11 +98,11 @@ if (success) { - await UpdateGrid(); + await UpdateGridAsync(); } } - async Task EditUser(AppUserWithRole userWithRole) + async Task EditUserAsync(AppUserWithRole userWithRole) { var success = await DialogService.OpenAsync( "Редактирование пользователя", @@ -122,11 +122,11 @@ if (success) { - await UpdateGrid(); + await UpdateGridAsync(); } } - async Task DeleteUser(AppUserWithRole userWithRole) + async Task DeleteUserAsync(AppUserWithRole userWithRole) { try { @@ -141,10 +141,10 @@ { isLoading = true; - var result = await UsersService.DeleteUser(userWithRole.User.Id); + var result = await UsersService.DeleteUserAsync(userWithRole.User.Id); if (result.Succeeded) { - await UpdateGrid(); + await UpdateGridAsync(); } else { @@ -162,12 +162,12 @@ } } - async Task UpdateGrid() + async Task UpdateGridAsync() { isLoading = true; hasError = false; - usersWithRoles = await UsersService.GetUsersWithRole(); + usersWithRoles = await UsersService.GetUsersWithRoleAsync(); isLoading = false; } diff --git a/Hcs.WebApp/Components/Pages/Registry/Common.razor b/Hcs.WebApp/Components/Pages/Registry/Common.razor index 24d9566..c031a3b 100644 --- a/Hcs.WebApp/Components/Pages/Registry/Common.razor +++ b/Hcs.WebApp/Components/Pages/Registry/Common.razor @@ -29,7 +29,7 @@ - + @@ -38,14 +38,14 @@ @errorMessage - + @@ -83,14 +83,14 @@ var state = await AuthenticationStateProvider.GetAuthenticationStateAsync(); if (state.User.IsInRole(AppRole.ADMINISTRATOR_TYPE) || state.User.IsInRole(AppRole.OPERATOR_TYPE)) { - var operationInProgress = await OperationService.HasActiveOperation(Operation.OperationType.Nsi_15_7_0_1_ExportAllRegistryElements); + var operationInProgress = await OperationService.HasActiveOperationAsync(Operation.OperationType.NsiCommon_15_7_0_1_ExportAllRegistryElements); if (operationInProgress) { finalState = CommonPageState.OperationWaiting; } else { - registries = await RegistryService.GetAllRegistries(true); + registries = await RegistryService.GetAllRegistriesAsync(true); } OperationExecutionState.OnOperationStarted += OnOperationStarted; @@ -100,34 +100,34 @@ } } - async Task SyncRegistries() + async Task SyncRegistriesAsync() { if (state == CommonPageState.OperationWaiting) return; ChangeState(CommonPageState.OperationWaiting); - if (await OperationService.HasActiveOperation(Operation.OperationType.Nsi_15_7_0_1_ExportAllRegistryElements)) + if (await OperationService.HasActiveOperationAsync(Operation.OperationType.NsiCommon_15_7_0_1_ExportAllRegistryElements)) { ChangeState(CommonPageState.Idle); } else { - var operation = await OperationService.InitiateOperation(Operation.OperationType.Nsi_15_7_0_1_ExportAllRegistryElements, ""); + var operation = await OperationService.InitiateOperationAsync(Operation.OperationType.NsiCommon_15_7_0_1_ExportAllRegistryElements, ""); OperationExecutionState.EnqueueOperation(operation); } } - async Task ViewRegistry(Registry userWithRole) + async Task ViewRegistryAsync(Registry userWithRole) { // TODO } - async Task EditRegistry(Registry registry) + async Task EditRegistryAsync(Registry registry) { // TODO } - async Task SyncRegistry(Registry registry) + async Task SyncRegistryAsync(Registry registry) { // TODO } @@ -163,7 +163,7 @@ void OnOperationStarted(Operation operation) { - if (operation.Type == Operation.OperationType.Nsi_15_7_0_1_ExportAllRegistryElements) + if (operation.Type == Operation.OperationType.NsiCommon_15_7_0_1_ExportAllRegistryElements) { InvokeAsync(() => ChangeState(CommonPageState.OperationWaiting)); } diff --git a/Hcs.WebApp/Components/Pages/Test/Export.razor b/Hcs.WebApp/Components/Pages/Test/Export.razor index 38426fd..dd53eb7 100644 --- a/Hcs.WebApp/Components/Pages/Test/Export.razor +++ b/Hcs.WebApp/Components/Pages/Test/Export.razor @@ -29,7 +29,7 @@ Сервис nsi - OnNsiExportItem1Click()) Disabled=@inputDisabled Text="Экспорт НСИ 1" ButtonStyle="ButtonStyle.Primary" /> + @@ -92,7 +92,7 @@ } } - async Task OnNsiExportItem1Click() + async Task OnNsiExportItem1ClickAsync() { try { diff --git a/Hcs.WebApp/Data/Hcs/Operation.cs b/Hcs.WebApp/Data/Hcs/Operation.cs index 299c105..0963b7a 100644 --- a/Hcs.WebApp/Data/Hcs/Operation.cs +++ b/Hcs.WebApp/Data/Hcs/Operation.cs @@ -6,7 +6,7 @@ namespace Hcs.WebApp.Data.Hcs { public enum OperationType { - Nsi_15_7_0_1_ExportAllRegistryElements + NsiCommon_15_7_0_1_ExportAllRegistryElements } public int Id { get; set; } diff --git a/Hcs.WebApp/Services/IdentityService.cs b/Hcs.WebApp/Services/IdentityService.cs index 42f11f7..ee9d118 100644 --- a/Hcs.WebApp/Services/IdentityService.cs +++ b/Hcs.WebApp/Services/IdentityService.cs @@ -7,7 +7,7 @@ namespace Hcs.WebApp.Services private readonly Uri baseUri = new($"{navigationManager.BaseUri}identity/"); private readonly HttpClient httpClient = factory.CreateClient("WithIdentity"); - public async Task ChangePassword(string oldPassword, string newPassword) + public async Task ChangePasswordAsync(string oldPassword, string newPassword) { var uri = new Uri($"{baseUri}change-password"); var content = new FormUrlEncodedContent(new Dictionary { diff --git a/Hcs.WebApp/Services/OperationService.cs b/Hcs.WebApp/Services/OperationService.cs index 0503737..31a94cd 100644 --- a/Hcs.WebApp/Services/OperationService.cs +++ b/Hcs.WebApp/Services/OperationService.cs @@ -7,13 +7,13 @@ namespace Hcs.WebApp.Services { private readonly IDbContextFactory factory = factory; - public async Task HasActiveOperation(Operation.OperationType type) + public async Task HasActiveOperationAsync(Operation.OperationType type) { using var context = factory.CreateDbContext(); return await context.Operations.AnyAsync(x => x.Type == type && !x.EndedAt.HasValue); } - public async Task InitiateOperation(Operation.OperationType type, string initiatorId) + public async Task InitiateOperationAsync(Operation.OperationType type, string initiatorId) { using var context = factory.CreateDbContext(); var operation = new Operation() @@ -27,7 +27,7 @@ namespace Hcs.WebApp.Services return operation; } - public async Task> GetInitiatedOperations() + public async Task> GetInitiatedOperationsAsync() { using var context = factory.CreateDbContext(); return await (from operation in context.Operations @@ -35,7 +35,7 @@ namespace Hcs.WebApp.Services select operation).ToListAsync(); } - public async Task SetOperationMessageGuid(int operationId, string messageGuid) + public async Task SetOperationMessageGuidAsync(int operationId, string messageGuid) { using var context = factory.CreateDbContext(); var operation = await context.Operations.FirstOrDefaultAsync(x => x.Id == operationId); diff --git a/Hcs.WebApp/Services/RegistryService.cs b/Hcs.WebApp/Services/RegistryService.cs index a516fe4..69f85fd 100644 --- a/Hcs.WebApp/Services/RegistryService.cs +++ b/Hcs.WebApp/Services/RegistryService.cs @@ -7,7 +7,7 @@ namespace Hcs.WebApp.Services { private readonly IDbContextFactory factory = factory; - public async Task> GetAllRegistries(bool isCommon) + public async Task> GetAllRegistriesAsync(bool isCommon) { using var context = factory.CreateDbContext(); return await (from registry in context.Registries diff --git a/Hcs.WebApp/Services/UsersService.cs b/Hcs.WebApp/Services/UsersService.cs index ac79d15..53a4368 100644 --- a/Hcs.WebApp/Services/UsersService.cs +++ b/Hcs.WebApp/Services/UsersService.cs @@ -13,7 +13,7 @@ namespace Hcs.WebApp.Services private readonly UserManager userManager = userManager; private readonly IPasswordHasher passwordHasher = passwordHasher; - public async Task> GetUsersWithRole() + public async Task> GetUsersWithRoleAsync() { using var context = factory.CreateDbContext(); return await (from user in context.Users @@ -26,7 +26,7 @@ namespace Hcs.WebApp.Services }).ToListAsync(); } - public async Task CreateUser(string userName, string roleName, string password) + public async Task CreateUserAsync(string userName, string roleName, string password) { var user = new AppUser() { @@ -41,7 +41,7 @@ namespace Hcs.WebApp.Services return result; } - public async Task UpdateUser(string userId, string roleId, string password) + public async Task UpdateUserAsync(string userId, string roleId, string password) { using var context = factory.CreateDbContext(); using var transaction = await context.Database.BeginTransactionAsync(); @@ -89,7 +89,7 @@ namespace Hcs.WebApp.Services await transaction.CommitAsync(); } - public async Task DeleteUser(string userId) + public async Task DeleteUserAsync(string userId) { var user = await userManager.FindByIdAsync(userId); return await userManager.DeleteAsync(user);