ReC/src/ReC.Application/RecActions/Commands/InvokeBatchRecActionsCommand.cs
TekH de17d398ff Rename ActionId to Id across the codebase
Updated the `ActionId` property to `Id` in `RecActionDto`
and `RecAction` classes for consistency. Reflected this
change in all relevant files, including log messages,
property assignments, and database column mappings.
Standardized naming conventions to improve code clarity
and maintainability.
2025-12-01 12:30:57 +01:00

49 lines
1.5 KiB
C#

using MediatR;
using Microsoft.Extensions.Logging;
using ReC.Application.RecActions.Queries;
namespace ReC.Application.RecActions.Commands;
public record InvokeBatchRecActionsCommand : ReadRecActionQueryBase, IRequest;
public static class InvokeBatchRecActionsCommandExtensions
{
public static Task InvokeBatchRecAction(this ISender sender, int profileId)
=> sender.Send(new InvokeBatchRecActionsCommand { ProfileId = profileId });
}
public class InvokeRecActionsCommandHandler(ISender sender, IHttpClientFactory clientFactory, ILogger<InvokeRecActionsCommandHandler>? logger = null) : IRequestHandler<InvokeBatchRecActionsCommand>
{
public async Task Handle(InvokeBatchRecActionsCommand request, CancellationToken cancel)
{
var actions = await sender.Send(request.ToReadQuery(), cancel);
var http = clientFactory.CreateClient();
using var semaphore = new SemaphoreSlim(5);
var tasks = actions.Select(async action =>
{
await semaphore.WaitAsync(cancel);
try
{
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);
}
}