ReC/src/ReC.Application/RecActions/Commands/InvokeBatchRecActionsCommand.cs
TekH bf02cc80d1 Add new POST endpoint and update profileId type to long
Added a new HTTP POST endpoint `invoke/fake` in `RecActionController`
to invoke batch actions with a fake profile ID. Updated the `profileId`
parameter type in the `InvokeBatchRecAction` extension method from
`int` to `long` to support larger profile IDs. These changes improve
flexibility and introduce a new endpoint for specific use cases.
2025-12-03 13:50:23 +01:00

49 lines
1.6 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, long profileId, CancellationToken cancel = default)
=> sender.Send(new InvokeBatchRecActionsCommand { ProfileId = profileId }, cancel);
}
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);
}
}