From 37c88812e1b9a4f6ec6fda2e9492e6d191f9da98 Mon Sep 17 00:00:00 2001 From: TekH Date: Tue, 9 Dec 2025 13:56:35 +0100 Subject: [PATCH] Simplify InvokeRecActionsCommandHandler dependencies and logic Refactored InvokeRecActionsCommandHandler to only require ISender, removing IServiceScopeFactory, IHttpClientFactory, and ILogger. Replaced concurrent invocation and error handling with a simple sequential loop, streamlining batch recommendation action execution. --- .../Commands/InvokeBatchRecActionsCommand.cs | 35 ++----------------- 1 file changed, 3 insertions(+), 32 deletions(-) diff --git a/src/ReC.Application/RecActions/Commands/InvokeBatchRecActionsCommand.cs b/src/ReC.Application/RecActions/Commands/InvokeBatchRecActionsCommand.cs index 87770f7..4f27e82 100644 --- a/src/ReC.Application/RecActions/Commands/InvokeBatchRecActionsCommand.cs +++ b/src/ReC.Application/RecActions/Commands/InvokeBatchRecActionsCommand.cs @@ -1,6 +1,4 @@ using MediatR; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; using ReC.Application.RecActions.Queries; namespace ReC.Application.RecActions.Commands; @@ -13,40 +11,13 @@ public static class InvokeBatchRecActionsCommandExtensions => sender.Send(new InvokeBatchRecActionsCommand { ProfileId = profileId }, cancel); } -public class InvokeRecActionsCommandHandler(ISender sender, IServiceScopeFactory scopeFactory, IHttpClientFactory clientFactory, ILogger? logger = null) : IRequestHandler +public class InvokeRecActionsCommandHandler(ISender sender) : IRequestHandler { public async Task Handle(InvokeBatchRecActionsCommand request, CancellationToken cancel) { var actions = await sender.Send(request.ToReadQuery(q => q.Invoked = false), cancel); - var http = clientFactory.CreateClient(); - - using var semaphore = new SemaphoreSlim(5); - - var tasks = actions.Select(async action => - { - await semaphore.WaitAsync(cancel); - try - { - using var scope = scopeFactory.CreateScope(); - var sender = scope.ServiceProvider.GetRequiredService(); - await sender.Send(action.ToInvokeCommand(), cancel); - } - catch(Exception ex) - { - logger?.LogError( - ex, - "Error invoking Rec action. ProfileId: {ProfileId}, Id: {Id}", - action.ProfileId, - action.Id - ); - } - finally - { - semaphore.Release(); - } - }); - - await Task.WhenAll(tasks); + foreach (var action in actions) + await sender.Send(action.ToInvokeCommand(), cancel); } } \ No newline at end of file