Deduplicate MassTransit routing slips, activities and consumers
This commit is contained in:
+21
-32
@@ -1,6 +1,7 @@
|
||||
using MassTransit;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Musify.Infrastructure.MassTransit.Arguments;
|
||||
using Musify.Infrastructure.MassTransit.RoutingSlip;
|
||||
|
||||
namespace Musify.Infrastructure.MassTransit.Activities.Pictures;
|
||||
|
||||
@@ -12,39 +13,27 @@ internal class GeneratePictureWorkflowPathsActivity(
|
||||
|
||||
internal const string ResizedPictureFileExtension = ".webp";
|
||||
|
||||
public async Task<ExecutionResult> Execute(ExecuteContext<GeneratePictureWorkflowPathsArguments> executeContext)
|
||||
public Task<ExecutionResult> Execute(ExecuteContext<GeneratePictureWorkflowPathsArguments> executeContext)
|
||||
{
|
||||
try
|
||||
var workingDirectory = Path.Combine(executeContext.Arguments.TemporaryRootDirectory, Guid.NewGuid().ToString());
|
||||
|
||||
var sourceFilePath = Path.Combine(
|
||||
workingDirectory, Guid.NewGuid().ToString() + Path.GetExtension(executeContext.Arguments.SourceKey));
|
||||
var smallPictureFilePath = Path.Combine(workingDirectory, Guid.NewGuid().ToString() + ResizedPictureFileExtension);
|
||||
var mediumPictureFilePath = Path.Combine(workingDirectory, Guid.NewGuid().ToString() + ResizedPictureFileExtension);
|
||||
var largePictureFilePath = Path.Combine(workingDirectory, Guid.NewGuid().ToString() + ResizedPictureFileExtension);
|
||||
|
||||
Directory.CreateDirectory(workingDirectory);
|
||||
|
||||
logger.LogDebug("Generated picture workflow paths in {WorkingDirectory}", workingDirectory);
|
||||
|
||||
return Task.FromResult(executeContext.CompletedWithVariables(new Dictionary<string, object>
|
||||
{
|
||||
var workingDirectory = Path.Combine(executeContext.Arguments.TemporaryRootDirectory, Guid.NewGuid().ToString());
|
||||
|
||||
var sourceFilePath = Path.Combine(
|
||||
workingDirectory, Guid.NewGuid().ToString() + Path.GetExtension(executeContext.Arguments.SourceKey));
|
||||
var smallPictureFilePath = Path.Combine(
|
||||
workingDirectory, Guid.NewGuid().ToString() + ResizedPictureFileExtension);
|
||||
var mediumPictureFilePath = Path.Combine(
|
||||
workingDirectory, Guid.NewGuid().ToString() + ResizedPictureFileExtension);
|
||||
var largePictureFilePath = Path.Combine(
|
||||
workingDirectory, Guid.NewGuid().ToString() + ResizedPictureFileExtension);
|
||||
|
||||
Directory.CreateDirectory(workingDirectory);
|
||||
|
||||
logger.LogDebug("Generated picture workflow paths in {WorkingDirectory}",
|
||||
workingDirectory);
|
||||
return executeContext.CompletedWithVariables(new Dictionary<string, object>
|
||||
{
|
||||
[RoutingSlipVariableNames.Workflow.TemporalDirectory] = workingDirectory,
|
||||
[RoutingSlipVariableNames.Picture.OriginalFilePath] = sourceFilePath,
|
||||
[RoutingSlipVariableNames.Picture.SmallResizedFilePath] = smallPictureFilePath,
|
||||
[RoutingSlipVariableNames.Picture.MediumResizedFilePath] = mediumPictureFilePath,
|
||||
[RoutingSlipVariableNames.Picture.LargeResizedFilePath] = largePictureFilePath
|
||||
});
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to generate picture workflow paths for {SourceKey}",
|
||||
executeContext.Arguments.SourceKey);
|
||||
throw;
|
||||
}
|
||||
[RoutingSlipVariableNames.Workflow.TemporalDirectory] = workingDirectory,
|
||||
[RoutingSlipVariableNames.Picture.OriginalFilePath] = sourceFilePath,
|
||||
[RoutingSlipVariableNames.Picture.SmallResizedFilePath] = smallPictureFilePath,
|
||||
[RoutingSlipVariableNames.Picture.MediumResizedFilePath] = mediumPictureFilePath,
|
||||
[RoutingSlipVariableNames.Picture.LargeResizedFilePath] = largePictureFilePath
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -16,9 +16,9 @@ internal class ResizePictureActivity(
|
||||
public async Task<ExecutionResult> Execute(ExecuteContext<ResizePictureLocalArguments> executeContext)
|
||||
{
|
||||
var sourceFilePath = executeContext.GetVariable<string>(executeContext.Arguments.SourceFilePathVariable);
|
||||
ArgumentNullException.ThrowIfNull(sourceFilePath, nameof(sourceFilePath));
|
||||
ArgumentNullException.ThrowIfNull(sourceFilePath);
|
||||
var destinationFilePath = executeContext.GetVariable<string>(executeContext.Arguments.DestinationFilePathVariable);
|
||||
ArgumentNullException.ThrowIfNull(destinationFilePath, nameof(destinationFilePath));
|
||||
ArgumentNullException.ThrowIfNull(destinationFilePath);
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
-110
@@ -1,110 +0,0 @@
|
||||
using MassTransit;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Musify.Application.Contracts;
|
||||
using Musify.Application.Events;
|
||||
using Musify.Domain.ValueObjects;
|
||||
using Musify.Infrastructure.MassTransit.Arguments;
|
||||
using Musify.Infrastructure.MassTransit.Logs;
|
||||
|
||||
namespace Musify.Infrastructure.MassTransit.Activities.Pictures;
|
||||
|
||||
internal class UpdateAlbumPictureActivity(
|
||||
IDatabase database,
|
||||
IPublishEndpoint publishEndpoint,
|
||||
ILogger<UpdateAlbumPictureActivity> logger)
|
||||
: IActivity<UpdateAlbumPictureArguments, UpdateAlbumPictureLog>
|
||||
{
|
||||
public const string ExecuteEndpointName = "update-album-picture";
|
||||
|
||||
public async Task<ExecutionResult> Execute(ExecuteContext<UpdateAlbumPictureArguments> executeContext)
|
||||
{
|
||||
var smallResizedVariable = executeContext.GetVariable<string>(executeContext.Arguments.SmallPictureVariable);
|
||||
ArgumentNullException.ThrowIfNull(smallResizedVariable, nameof(smallResizedVariable));
|
||||
var mediumResizedVariable = executeContext.GetVariable<string>(executeContext.Arguments.MediumPictureVariable);
|
||||
ArgumentNullException.ThrowIfNull(mediumResizedVariable, nameof(mediumResizedVariable));
|
||||
var largeResizedVariable = executeContext.GetVariable<string>(executeContext.Arguments.LargePictureVariable);
|
||||
ArgumentNullException.ThrowIfNull(largeResizedVariable, nameof(largeResizedVariable));
|
||||
|
||||
try
|
||||
{
|
||||
var album = await database.Albums.FindAsync(
|
||||
[executeContext.Arguments.AlbumId],
|
||||
cancellationToken: executeContext.CancellationToken);
|
||||
|
||||
if (album == null)
|
||||
{
|
||||
logger.LogWarning("Album {AlbumId} not found",
|
||||
executeContext.Arguments.AlbumId);
|
||||
throw new InvalidOperationException($"Album with id {executeContext.Arguments.AlbumId} not found");
|
||||
}
|
||||
|
||||
if (album.Pictures == null)
|
||||
{
|
||||
logger.LogWarning("Album {AlbumId} has no pending picture upload",
|
||||
executeContext.Arguments.AlbumId);
|
||||
throw new InvalidOperationException($"Album with id {executeContext.Arguments.AlbumId} has no pending picture upload");
|
||||
}
|
||||
|
||||
var log = new UpdateAlbumPictureLog(
|
||||
album.Id,
|
||||
album.Pictures.OriginalName,
|
||||
album.Pictures.SmallName,
|
||||
album.Pictures.MediumName,
|
||||
album.Pictures.LargeName);
|
||||
|
||||
album.Pictures.OriginalName = Path.GetFileName(executeContext.Arguments.OriginalPictureKey);
|
||||
album.Pictures.SmallName = Path.GetFileName(smallResizedVariable);
|
||||
album.Pictures.MediumName = Path.GetFileName(mediumResizedVariable);
|
||||
album.Pictures.LargeName = Path.GetFileName(largeResizedVariable);
|
||||
|
||||
await database.SaveChangesAsync(executeContext.CancellationToken);
|
||||
|
||||
logger.LogInformation("Updated album {AlbumId} pictures",
|
||||
executeContext.Arguments.AlbumId);
|
||||
|
||||
await publishEndpoint.Publish(
|
||||
new AlbumPictureProcessed(executeContext.Arguments.AlbumId),
|
||||
executeContext.CancellationToken);
|
||||
|
||||
return executeContext.Completed();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to update album {AlbumId} pictures",
|
||||
executeContext.Arguments.AlbumId);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<CompensationResult> Compensate(CompensateContext<UpdateAlbumPictureLog> compensateContext)
|
||||
{
|
||||
try
|
||||
{
|
||||
var album = await database.Albums.FindAsync(
|
||||
[compensateContext.Log.AlbumId],
|
||||
cancellationToken: compensateContext.CancellationToken);
|
||||
|
||||
if (album == null)
|
||||
{
|
||||
return compensateContext.Compensated();
|
||||
}
|
||||
|
||||
album.Pictures = new EntityPictures
|
||||
{
|
||||
OriginalName = compensateContext.Log.PreviousOriginalPictureKey,
|
||||
SmallName = compensateContext.Log.PreviousSmallPictureKey,
|
||||
MediumName = compensateContext.Log.PreviousMediumPictureKey,
|
||||
LargeName = compensateContext.Log.PreviousLargePictureKey
|
||||
};
|
||||
|
||||
await database.SaveChangesAsync(compensateContext.CancellationToken);
|
||||
|
||||
return compensateContext.Compensated();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to compensate album {AlbumId} pictures", compensateContext.Log.AlbumId);
|
||||
return compensateContext.Failed(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
+160
@@ -0,0 +1,160 @@
|
||||
using MassTransit;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Musify.Application.Contracts;
|
||||
using Musify.Application.Events;
|
||||
using Musify.Domain.Entities;
|
||||
using Musify.Domain.ValueObjects;
|
||||
using Musify.Infrastructure.MassTransit.Arguments;
|
||||
using Musify.Infrastructure.MassTransit.Logs;
|
||||
|
||||
namespace Musify.Infrastructure.MassTransit.Activities.Pictures;
|
||||
|
||||
internal sealed record PictureNames(string? Original, string? Small, string? Medium, string? Large);
|
||||
|
||||
/// <summary>Stores the processed picture names on the owner and reports it; compensation restores the previous names.</summary>
|
||||
internal abstract class UpdatePicturesActivity<TEntity>(
|
||||
IDatabase database,
|
||||
IPublishEndpoint publishEndpoint,
|
||||
ILogger logger)
|
||||
: IActivity<UpdatePicturesArguments, UpdatePicturesLog>
|
||||
where TEntity : class
|
||||
{
|
||||
protected IDatabase Database { get; } = database;
|
||||
|
||||
protected abstract Task<TEntity?> FindAsync(Guid id, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>Reads the current names; throws when the entity has no pending picture.</summary>
|
||||
protected abstract PictureNames Read(TEntity entity);
|
||||
|
||||
protected abstract void Write(TEntity entity, PictureNames names, bool succeeded);
|
||||
|
||||
protected abstract object ProcessedEvent(Guid id);
|
||||
|
||||
public async Task<ExecutionResult> Execute(ExecuteContext<UpdatePicturesArguments> executeContext)
|
||||
{
|
||||
var arguments = executeContext.Arguments;
|
||||
var small = RequireVariable(executeContext, arguments.SmallPictureVariable);
|
||||
var medium = RequireVariable(executeContext, arguments.MediumPictureVariable);
|
||||
var large = RequireVariable(executeContext, arguments.LargePictureVariable);
|
||||
|
||||
var entity = await FindAsync(arguments.SubjectId, executeContext.CancellationToken)
|
||||
?? throw new InvalidOperationException($"{typeof(TEntity).Name} {arguments.SubjectId} not found");
|
||||
|
||||
var previous = Read(entity);
|
||||
Write(
|
||||
entity,
|
||||
new PictureNames(
|
||||
Path.GetFileName(arguments.OriginalPictureKey),
|
||||
Path.GetFileName(small),
|
||||
Path.GetFileName(medium),
|
||||
Path.GetFileName(large)),
|
||||
succeeded: true);
|
||||
await Database.SaveChangesAsync(executeContext.CancellationToken);
|
||||
|
||||
logger.LogInformation("Updated {Entity} {Id} pictures", typeof(TEntity).Name, arguments.SubjectId);
|
||||
|
||||
await publishEndpoint.Publish(ProcessedEvent(arguments.SubjectId), executeContext.CancellationToken);
|
||||
|
||||
return executeContext.Completed(new UpdatePicturesLog(
|
||||
arguments.SubjectId, previous.Original, previous.Small, previous.Medium, previous.Large));
|
||||
}
|
||||
|
||||
public async Task<CompensationResult> Compensate(CompensateContext<UpdatePicturesLog> compensateContext)
|
||||
{
|
||||
var log = compensateContext.Log;
|
||||
|
||||
try
|
||||
{
|
||||
var entity = await FindAsync(log.SubjectId, compensateContext.CancellationToken);
|
||||
if (entity == null)
|
||||
return compensateContext.Compensated();
|
||||
|
||||
var previous = new PictureNames(log.PreviousOriginalName, log.PreviousSmallName, log.PreviousMediumName, log.PreviousLargeName);
|
||||
Write(entity, previous, succeeded: false);
|
||||
await Database.SaveChangesAsync(compensateContext.CancellationToken);
|
||||
|
||||
return compensateContext.Compensated();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to compensate {Entity} {Id} pictures", typeof(TEntity).Name, log.SubjectId);
|
||||
return compensateContext.Failed(exception);
|
||||
}
|
||||
}
|
||||
|
||||
protected static PictureNames ReadPictures(EntityPictures? pictures) =>
|
||||
pictures == null
|
||||
? throw new InvalidOperationException("The entity has no pending picture upload")
|
||||
: new PictureNames(pictures.OriginalName, pictures.SmallName, pictures.MediumName, pictures.LargeName);
|
||||
|
||||
protected static void WritePictures(EntityPictures? pictures, PictureNames names)
|
||||
{
|
||||
if (pictures == null)
|
||||
return;
|
||||
|
||||
pictures.OriginalName = names.Original ?? pictures.OriginalName;
|
||||
pictures.SmallName = names.Small;
|
||||
pictures.MediumName = names.Medium;
|
||||
pictures.LargeName = names.Large;
|
||||
}
|
||||
|
||||
private static string RequireVariable(ExecuteContext<UpdatePicturesArguments> executeContext, string variableName) =>
|
||||
executeContext.GetVariable<string>(variableName)
|
||||
?? throw new InvalidOperationException($"Missing routing slip variable {variableName}");
|
||||
}
|
||||
|
||||
internal sealed class UpdateTrackPictureActivity(IDatabase database, IPublishEndpoint publishEndpoint, ILogger<UpdateTrackPictureActivity> logger)
|
||||
: UpdatePicturesActivity<Track>(database, publishEndpoint, logger)
|
||||
{
|
||||
public const string ExecuteEndpointName = "update-track-picture";
|
||||
|
||||
protected override Task<Track?> FindAsync(Guid id, CancellationToken cancellationToken) =>
|
||||
Database.Tracks.SingleOrDefaultAsync(track => track.Id == id, cancellationToken);
|
||||
|
||||
protected override PictureNames Read(Track track) =>
|
||||
new(track.Pictures.OriginalName, track.Pictures.SmallName, track.Pictures.MediumName, track.Pictures.LargeName);
|
||||
|
||||
protected override void Write(Track track, PictureNames names, bool succeeded)
|
||||
{
|
||||
track.Pictures.OriginalName = names.Original;
|
||||
track.Pictures.SmallName = names.Small;
|
||||
track.Pictures.MediumName = names.Medium;
|
||||
track.Pictures.LargeName = names.Large;
|
||||
track.Pictures.ProcessingStatus = succeeded ? ProcessingStatus.Completed : ProcessingStatus.Failed;
|
||||
}
|
||||
|
||||
protected override object ProcessedEvent(Guid id) => new TrackPictureProcessed(id);
|
||||
}
|
||||
|
||||
internal sealed class UpdatePlayListPictureActivity(IDatabase database, IPublishEndpoint publishEndpoint, ILogger<UpdatePlayListPictureActivity> logger)
|
||||
: UpdatePicturesActivity<PlayList>(database, publishEndpoint, logger)
|
||||
{
|
||||
public const string ExecuteEndpointName = "update-play-list-picture";
|
||||
|
||||
protected override Task<PlayList?> FindAsync(Guid id, CancellationToken cancellationToken) =>
|
||||
Database.PlayLists.SingleOrDefaultAsync(playList => playList.Id == id, cancellationToken);
|
||||
|
||||
protected override PictureNames Read(PlayList playList) => ReadPictures(playList.Pictures);
|
||||
|
||||
protected override void Write(PlayList playList, PictureNames names, bool succeeded) =>
|
||||
WritePictures(playList.Pictures, names);
|
||||
|
||||
protected override object ProcessedEvent(Guid id) => new PlayListPictureProcessed(id);
|
||||
}
|
||||
|
||||
internal sealed class UpdateAlbumPictureActivity(IDatabase database, IPublishEndpoint publishEndpoint, ILogger<UpdateAlbumPictureActivity> logger)
|
||||
: UpdatePicturesActivity<Album>(database, publishEndpoint, logger)
|
||||
{
|
||||
public const string ExecuteEndpointName = "update-album-picture";
|
||||
|
||||
protected override Task<Album?> FindAsync(Guid id, CancellationToken cancellationToken) =>
|
||||
Database.Albums.SingleOrDefaultAsync(album => album.Id == id, cancellationToken);
|
||||
|
||||
protected override PictureNames Read(Album album) => ReadPictures(album.Pictures);
|
||||
|
||||
protected override void Write(Album album, PictureNames names, bool succeeded) =>
|
||||
WritePictures(album.Pictures, names);
|
||||
|
||||
protected override object ProcessedEvent(Guid id) => new AlbumPictureProcessed(id);
|
||||
}
|
||||
-110
@@ -1,110 +0,0 @@
|
||||
using MassTransit;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Musify.Application.Contracts;
|
||||
using Musify.Application.Events;
|
||||
using Musify.Domain.ValueObjects;
|
||||
using Musify.Infrastructure.MassTransit.Arguments;
|
||||
using Musify.Infrastructure.MassTransit.Logs;
|
||||
|
||||
namespace Musify.Infrastructure.MassTransit.Activities.Pictures;
|
||||
|
||||
internal class UpdatePlayListPictureActivity(
|
||||
IDatabase database,
|
||||
IPublishEndpoint publishEndpoint,
|
||||
ILogger<UpdatePlayListPictureActivity> logger)
|
||||
: IActivity<UpdatePlayListPictureArguments, UpdatePlayListPictureLog>
|
||||
{
|
||||
public const string ExecuteEndpointName = "update-play-list-picture";
|
||||
|
||||
public async Task<ExecutionResult> Execute(ExecuteContext<UpdatePlayListPictureArguments> executeContext)
|
||||
{
|
||||
var smallResizedVariable = executeContext.GetVariable<string>(executeContext.Arguments.SmallPictureVariable);
|
||||
ArgumentNullException.ThrowIfNull(smallResizedVariable, nameof(smallResizedVariable));
|
||||
var mediumResizedVariable = executeContext.GetVariable<string>(executeContext.Arguments.MediumPictureVariable);
|
||||
ArgumentNullException.ThrowIfNull(mediumResizedVariable, nameof(mediumResizedVariable));
|
||||
var largeResizedVariable = executeContext.GetVariable<string>(executeContext.Arguments.LargePictureVariable);
|
||||
ArgumentNullException.ThrowIfNull(largeResizedVariable, nameof(largeResizedVariable));
|
||||
|
||||
try
|
||||
{
|
||||
var playList = await database.PlayLists.FindAsync(
|
||||
[executeContext.Arguments.PlayListId],
|
||||
cancellationToken: executeContext.CancellationToken);
|
||||
|
||||
if (playList == null)
|
||||
{
|
||||
logger.LogWarning("Playlist {PlayListId} not found",
|
||||
executeContext.Arguments.PlayListId);
|
||||
throw new InvalidOperationException($"PlayList with id {executeContext.Arguments.PlayListId} not found");
|
||||
}
|
||||
|
||||
if (playList.Pictures == null)
|
||||
{
|
||||
logger.LogWarning("Playlist {PlayListId} has no pending picture upload",
|
||||
executeContext.Arguments.PlayListId);
|
||||
throw new InvalidOperationException($"PlayList with id {executeContext.Arguments.PlayListId} has no pending picture upload");
|
||||
}
|
||||
|
||||
var log = new UpdatePlayListPictureLog(
|
||||
playList.Id,
|
||||
playList.Pictures.OriginalName,
|
||||
playList.Pictures.SmallName,
|
||||
playList.Pictures.MediumName,
|
||||
playList.Pictures.LargeName);
|
||||
|
||||
playList.Pictures.OriginalName = Path.GetFileName(executeContext.Arguments.OriginalPictureKey);
|
||||
playList.Pictures.SmallName = Path.GetFileName(smallResizedVariable);
|
||||
playList.Pictures.MediumName = Path.GetFileName(mediumResizedVariable);
|
||||
playList.Pictures.LargeName = Path.GetFileName(largeResizedVariable);
|
||||
|
||||
await database.SaveChangesAsync(executeContext.CancellationToken);
|
||||
|
||||
logger.LogInformation("Updated playlist {PlayListId} pictures",
|
||||
executeContext.Arguments.PlayListId);
|
||||
|
||||
await publishEndpoint.Publish(
|
||||
new PlayListPictureProcessed(executeContext.Arguments.PlayListId),
|
||||
executeContext.CancellationToken);
|
||||
|
||||
return executeContext.Completed();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to update playlist {PlayListId} pictures",
|
||||
executeContext.Arguments.PlayListId);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<CompensationResult> Compensate(CompensateContext<UpdatePlayListPictureLog> compensateContext)
|
||||
{
|
||||
try
|
||||
{
|
||||
var playList = await database.PlayLists.FindAsync(
|
||||
[compensateContext.Log.PlayListId],
|
||||
cancellationToken: compensateContext.CancellationToken);
|
||||
|
||||
if (playList == null)
|
||||
{
|
||||
return compensateContext.Compensated();
|
||||
}
|
||||
|
||||
playList.Pictures = new EntityPictures
|
||||
{
|
||||
OriginalName = compensateContext.Log.PreviousOriginalPictureKey,
|
||||
SmallName = compensateContext.Log.PreviousSmallPictureKey,
|
||||
MediumName = compensateContext.Log.PreviousMediumPictureKey,
|
||||
LargeName = compensateContext.Log.PreviousLargePictureKey
|
||||
};
|
||||
|
||||
await database.SaveChangesAsync(compensateContext.CancellationToken);
|
||||
|
||||
return compensateContext.Compensated();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to compensate playlist {PlayListId} pictures", compensateContext.Log.PlayListId);
|
||||
return compensateContext.Failed(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
-100
@@ -1,100 +0,0 @@
|
||||
using MassTransit;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Musify.Application.Contracts;
|
||||
using Musify.Application.Events;
|
||||
using Musify.Domain.ValueObjects;
|
||||
using Musify.Infrastructure.MassTransit.Arguments;
|
||||
using Musify.Infrastructure.MassTransit.Logs;
|
||||
|
||||
namespace Musify.Infrastructure.MassTransit.Activities.Pictures;
|
||||
|
||||
internal class UpdateTrackPictureActivity(
|
||||
IDatabase database,
|
||||
IPublishEndpoint publishEndpoint,
|
||||
ILogger<UpdateTrackPictureActivity> logger)
|
||||
: IActivity<UpdateTrackPictureArguments, UpdateTrackPictureLog>
|
||||
{
|
||||
public const string ExecuteEndpointName = "update-track-picture";
|
||||
|
||||
public async Task<ExecutionResult> Execute(ExecuteContext<UpdateTrackPictureArguments> executeContext)
|
||||
{
|
||||
var smallResizedVariable = executeContext.GetVariable<string>(executeContext.Arguments.SmallPictureVariable);
|
||||
ArgumentNullException.ThrowIfNull(smallResizedVariable, nameof(smallResizedVariable));
|
||||
var mediumResizedVariable = executeContext.GetVariable<string>(executeContext.Arguments.MediumPictureVariable);
|
||||
ArgumentNullException.ThrowIfNull(mediumResizedVariable, nameof(mediumResizedVariable));
|
||||
var largeResizedVariable = executeContext.GetVariable<string>(executeContext.Arguments.LargePictureVariable);
|
||||
ArgumentNullException.ThrowIfNull(largeResizedVariable, nameof(largeResizedVariable));
|
||||
|
||||
try
|
||||
{
|
||||
var track = await database.Tracks.FindAsync(
|
||||
[executeContext.Arguments.TrackId],
|
||||
cancellationToken: executeContext.CancellationToken);
|
||||
|
||||
if (track == null)
|
||||
{
|
||||
logger.LogWarning("Track {TrackId} not found",
|
||||
executeContext.Arguments.TrackId);
|
||||
throw new InvalidOperationException($"Track with id {executeContext.Arguments.TrackId} not found");
|
||||
}
|
||||
|
||||
var log = new UpdateTrackPictureLog(
|
||||
track.Id,
|
||||
track.Pictures.OriginalName,
|
||||
track.Pictures.SmallName,
|
||||
track.Pictures.MediumName,
|
||||
track.Pictures.LargeName);
|
||||
|
||||
track.Pictures.OriginalName = Path.GetFileName(executeContext.Arguments.OriginalPictureKey);
|
||||
track.Pictures.SmallName = Path.GetFileName(smallResizedVariable);
|
||||
track.Pictures.MediumName = Path.GetFileName(mediumResizedVariable);
|
||||
track.Pictures.LargeName = Path.GetFileName(largeResizedVariable);
|
||||
track.Pictures.ProcessingStatus = ProcessingStatus.Completed;
|
||||
|
||||
await database.SaveChangesAsync(executeContext.CancellationToken);
|
||||
|
||||
logger.LogInformation("Updated track {TrackId} pictures",
|
||||
executeContext.Arguments.TrackId);
|
||||
|
||||
await publishEndpoint.Publish(
|
||||
new TrackPictureProcessed(executeContext.Arguments.TrackId),
|
||||
executeContext.CancellationToken);
|
||||
|
||||
return executeContext.Completed();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to update track {TrackId} pictures",
|
||||
executeContext.Arguments.TrackId);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<CompensationResult> Compensate(CompensateContext<UpdateTrackPictureLog> compensateContext)
|
||||
{
|
||||
try
|
||||
{
|
||||
var track = await database.Tracks.FindAsync(
|
||||
[compensateContext.Log.TrackId],
|
||||
cancellationToken: compensateContext.CancellationToken);
|
||||
|
||||
if (track == null)
|
||||
return compensateContext.Compensated();
|
||||
|
||||
track.Pictures.OriginalName = compensateContext.Log.PreviousOriginalPictureKey;
|
||||
track.Pictures.SmallName = compensateContext.Log.PreviousSmallPictureKey;
|
||||
track.Pictures.MediumName = compensateContext.Log.PreviousMediumPictureKey;
|
||||
track.Pictures.LargeName = compensateContext.Log.PreviousLargePictureKey;
|
||||
track.Pictures.ProcessingStatus = ProcessingStatus.Failed;
|
||||
|
||||
await database.SaveChangesAsync(compensateContext.CancellationToken);
|
||||
|
||||
return compensateContext.Compensated();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to compensate track {TrackId} pictures", compensateContext.Log.TrackId);
|
||||
return compensateContext.Failed(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user