Last consumer
This commit is contained in:
@@ -1,11 +1,9 @@
|
|||||||
using Ardalis.Result;
|
using Ardalis.Result;
|
||||||
using DispatchR.Abstractions.Send;
|
using DispatchR.Abstractions.Send;
|
||||||
using MassTransit;
|
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Musify.Application.Configuration;
|
using Musify.Application.Configuration;
|
||||||
using Musify.Application.Contracts.Infrastructure;
|
using Musify.Application.Contracts.Infrastructure;
|
||||||
using Musify.Application.Events;
|
using Musify.Application.Events;
|
||||||
using Musify.Application.PlayLists;
|
|
||||||
|
|
||||||
namespace Musify.Application.PlayLists.Commands.DeletePlayList
|
namespace Musify.Application.PlayLists.Commands.DeletePlayList
|
||||||
{
|
{
|
||||||
@@ -31,66 +29,33 @@ namespace Musify.Application.PlayLists.Commands.DeletePlayList
|
|||||||
|
|
||||||
if (!string.IsNullOrWhiteSpace(playList.OriginalPictureKeyName))
|
if (!string.IsNullOrWhiteSpace(playList.OriginalPictureKeyName))
|
||||||
{
|
{
|
||||||
var originalImageStorageKey = Path.Join(playListConfiguration.Routes.OriginalPictures, playList.OriginalPictureKeyName);
|
var keysToRemove = new List<string>
|
||||||
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
await eventBus.PublishAsync(new RemoveFileEvent(
|
Path.Join(playListConfiguration.Routes.OriginalPictures, playList.OriginalPictureKeyName)
|
||||||
Guid.NewGuid(),
|
};
|
||||||
storageConfiguration.BucketName,
|
|
||||||
originalImageStorageKey), cancellationToken);
|
|
||||||
}
|
|
||||||
catch (Exception exception)
|
|
||||||
{
|
|
||||||
logger.LogError(exception, "Failed to publish remove file event for original picture of PlayList id={PlayListId}.", playList.Id);
|
|
||||||
return Result.Error($"Failed to publish remove file event for PlayList with id {playList.Id}.");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (playList.SmallPictureKeyName != playListConfiguration.Routes.PresetSmallPicture)
|
if (playList.SmallPictureKeyName != playListConfiguration.Routes.PresetSmallPicture)
|
||||||
{
|
{
|
||||||
try
|
keysToRemove.Add(playList.SmallPictureKeyName);
|
||||||
{
|
|
||||||
await eventBus.PublishAsync(new RemoveFileEvent(
|
|
||||||
Guid.NewGuid(),
|
|
||||||
storageConfiguration.BucketName,
|
|
||||||
playList.SmallPictureKeyName), cancellationToken);
|
|
||||||
}
|
|
||||||
catch (Exception exception)
|
|
||||||
{
|
|
||||||
logger.LogError(exception, "Failed to publish remove file event for small picture of PlayList id={PlayListId}.", playList.Id);
|
|
||||||
return Result.Error($"Failed to publish remove file event for PlayList with id {playList.Id}.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (playList.MediumPictureKeyName != playListConfiguration.Routes.PresetMediumPicture)
|
if (playList.MediumPictureKeyName != playListConfiguration.Routes.PresetMediumPicture)
|
||||||
{
|
{
|
||||||
try
|
keysToRemove.Add(playList.MediumPictureKeyName);
|
||||||
{
|
|
||||||
await eventBus.PublishAsync(new RemoveFileEvent(
|
|
||||||
Guid.NewGuid(),
|
|
||||||
storageConfiguration.BucketName,
|
|
||||||
playList.MediumPictureKeyName), cancellationToken);
|
|
||||||
}
|
|
||||||
catch (Exception exception)
|
|
||||||
{
|
|
||||||
logger.LogError(exception, "Failed to publish remove file event for medium picture of PlayList id={PlayListId}.", playList.Id);
|
|
||||||
return Result.Error($"Failed to publish remove file event for PlayList with id {playList.Id}.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (playList.LargePictureKeyName != playListConfiguration.Routes.PresetLargePicture)
|
if (playList.LargePictureKeyName != playListConfiguration.Routes.PresetLargePicture)
|
||||||
{
|
{
|
||||||
try
|
keysToRemove.Add(playList.LargePictureKeyName);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var key in keysToRemove)
|
||||||
|
{
|
||||||
|
var publishResult = await TryPublishRemoveFileEventAsync(playList.Id, key, cancellationToken);
|
||||||
|
|
||||||
|
if (!publishResult.IsSuccess)
|
||||||
{
|
{
|
||||||
await eventBus.PublishAsync(new RemoveFileEvent(
|
return publishResult;
|
||||||
Guid.NewGuid(),
|
|
||||||
storageConfiguration.BucketName,
|
|
||||||
playList.LargePictureKeyName), cancellationToken);
|
|
||||||
}
|
|
||||||
catch (Exception exception)
|
|
||||||
{
|
|
||||||
logger.LogError(exception, "Failed to publish remove file event for large picture of PlayList id={PlayListId}.", playList.Id);
|
|
||||||
return Result.Error($"Failed to publish remove file event for PlayList with id {playList.Id}.");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -99,5 +64,23 @@ namespace Musify.Application.PlayLists.Commands.DeletePlayList
|
|||||||
await database.SaveChangesAsync(cancellationToken);
|
await database.SaveChangesAsync(cancellationToken);
|
||||||
return Result.NoContent();
|
return Result.NoContent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async Task<Result> TryPublishRemoveFileEventAsync(Guid playListId, string keyName, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await eventBus.PublishAsync(new RemoveFileEvent(
|
||||||
|
Guid.NewGuid(),
|
||||||
|
storageConfiguration.BucketName,
|
||||||
|
keyName), cancellationToken);
|
||||||
|
|
||||||
|
return Result.Success();
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
logger.LogError(exception, "Failed to publish remove file event for PlayList id={PlayListId} and key={KeyName}.", playListId, keyName);
|
||||||
|
return Result.Error($"Failed to publish remove file event for PlayList with id {playListId}.");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
using MassTransit;
|
||||||
|
using Musify.Application.Events;
|
||||||
|
using Musify.Infrastructure.Messaging.Activities;
|
||||||
|
using Musify.Infrastructure.Messaging.Activities.Arguments;
|
||||||
|
|
||||||
|
namespace Musify.Infrastructure.Messaging.Consumers
|
||||||
|
{
|
||||||
|
public class RemoveFileConsumer(IBus bus)
|
||||||
|
: IConsumer<RemoveFileEvent>
|
||||||
|
{
|
||||||
|
public const string QueueName = "Remove-File";
|
||||||
|
|
||||||
|
public async Task Consume(ConsumeContext<RemoveFileEvent> consumeContext)
|
||||||
|
{
|
||||||
|
var routingSlipBuilder = new RoutingSlipBuilder(NewId.NextGuid());
|
||||||
|
|
||||||
|
routingSlipBuilder.AddActivity(
|
||||||
|
"RemoveFile",
|
||||||
|
BuildExecuteUri(RemoveFilesActivity.ExecuteEndpointName),
|
||||||
|
new RemoveFileArguments(
|
||||||
|
consumeContext.Message.BucketName,
|
||||||
|
consumeContext.Message.KeyName));
|
||||||
|
|
||||||
|
var routingSlip = routingSlipBuilder.Build();
|
||||||
|
await bus.Execute(routingSlip);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Uri BuildExecuteUri(string endpointName) => new($"queue:{endpointName}_execute");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -25,6 +25,7 @@ namespace Musify.Infrastructure.Messaging
|
|||||||
serviceDescriptors.AddMassTransit(options =>
|
serviceDescriptors.AddMassTransit(options =>
|
||||||
{
|
{
|
||||||
options.AddConsumer<UpdatePlayListPictureConsumer>();
|
options.AddConsumer<UpdatePlayListPictureConsumer>();
|
||||||
|
options.AddConsumer<RemoveFileConsumer>();
|
||||||
options.AddExecuteActivity<ResizePictureActivity, ResizePictureArgument>();
|
options.AddExecuteActivity<ResizePictureActivity, ResizePictureArgument>();
|
||||||
options.AddExecuteActivity<RemoveFilesActivity, RemoveFileArguments>();
|
options.AddExecuteActivity<RemoveFilesActivity, RemoveFileArguments>();
|
||||||
options.AddExecuteActivity<UpdatePlayListPictureActivity, UpdatePlayListPictureArguments>();
|
options.AddExecuteActivity<UpdatePlayListPictureActivity, UpdatePlayListPictureArguments>();
|
||||||
@@ -38,6 +39,11 @@ namespace Musify.Infrastructure.Messaging
|
|||||||
endpointConfigurator.ConfigureConsumer<UpdatePlayListPictureConsumer>(busRegistrationContext);
|
endpointConfigurator.ConfigureConsumer<UpdatePlayListPictureConsumer>(busRegistrationContext);
|
||||||
} );
|
} );
|
||||||
|
|
||||||
|
busFactoryConfigurator.ReceiveEndpoint(RemoveFileConsumer.QueueName, endpointConfigurator =>
|
||||||
|
{
|
||||||
|
endpointConfigurator.ConfigureConsumer<RemoveFileConsumer>(busRegistrationContext);
|
||||||
|
} );
|
||||||
|
|
||||||
ConfigureExecuteActivityEndpoint<ResizePictureActivity, ResizePictureArgument>(
|
ConfigureExecuteActivityEndpoint<ResizePictureActivity, ResizePictureArgument>(
|
||||||
busFactoryConfigurator,
|
busFactoryConfigurator,
|
||||||
busRegistrationContext,
|
busRegistrationContext,
|
||||||
|
|||||||
Reference in New Issue
Block a user