This commit is contained in:
2026-03-24 14:45:20 +01:00
parent 4f5230ddf6
commit 22a65b7efa
66 changed files with 1830 additions and 922 deletions
@@ -0,0 +1,76 @@
using MassTransit;
using Microsoft.Extensions.Logging;
using Musify.Application.Contracts.Infrastructure;
using Musify.Domain.Entities;
using Musify.Infrastructure.Messaging.Activities.Arguments;
namespace Musify.Infrastructure.Messaging.Activities
{
public class GeneratePictureWorkflowPathsActivity(
ILogger<GeneratePictureWorkflowPathsActivity> logger,
IProcessTrackingStore processTrackingStore)
: IExecuteActivity<GeneratePictureWorkflowPathsArguments>
{
public const string ExecuteEndpointName = "Generate-Picture-Workflow-Paths";
public async Task<ExecutionResult> Execute(ExecuteContext<GeneratePictureWorkflowPathsArguments> executeContext)
{
var processId = await processTrackingStore.GetOrCreateProcessAsync(
"RoutingSlip",
executeContext.CorrelationId ?? executeContext.TrackingNumber,
executeContext.ConversationId,
executeContext.MessageId,
executeContext.CancellationToken);
var stepId = await processTrackingStore.StartStepAsync(
processId,
nameof(GeneratePictureWorkflowPathsActivity),
ProcessStepComponentType.Activity,
0,
executeContext.CancellationToken);
try
{
var destinationFolderName = Guid.NewGuid().ToString();
var originalFileName = Path.GetFileName(executeContext.Arguments.SourceKeyName);
var sourceFileExtension = Path.GetExtension(originalFileName);
var workingDirectory = Path.Combine(executeContext.Arguments.TemporaryRootDirectory, destinationFolderName);
var sourceFileName = Guid.NewGuid().ToString() + sourceFileExtension;
var smallPictureFileName = Guid.NewGuid().ToString() + sourceFileExtension;
var mediumPictureFileName = Guid.NewGuid().ToString() + sourceFileExtension;
var largePictureFileName = Guid.NewGuid().ToString() + sourceFileExtension;
var sourceFilePath = Path.Combine(workingDirectory, sourceFileName);
var smallPictureFilePath = Path.Combine(workingDirectory, smallPictureFileName);
var mediumPictureFilePath = Path.Combine(workingDirectory, mediumPictureFileName);
var largePictureFilePath = Path.Combine(workingDirectory, largePictureFileName);
Directory.CreateDirectory(workingDirectory);
logger.LogDebug("Generated picture workflow paths. WorkingDirectory: {WorkingDirectory}, SourceFilePath: {SourceFilePath}",
workingDirectory,
sourceFilePath);
await processTrackingStore.CompleteStepAsync(processId, stepId, executeContext.CancellationToken);
return executeContext.CompletedWithVariables(new Dictionary<string, object>
{
[RoutingSlipVariableNames.Workflow.TempDirectory] = workingDirectory,
[RoutingSlipVariableNames.Picture.OriginalFilePath] = sourceFilePath,
[RoutingSlipVariableNames.Picture.SmallResizedFilePath] = smallPictureFilePath,
[RoutingSlipVariableNames.Picture.MediumResizedFilePath] = mediumPictureFilePath,
[RoutingSlipVariableNames.Picture.LargeResizedFilePath] = largePictureFilePath,
[RoutingSlipVariableNames.Picture.DestinationFolderName] = destinationFolderName
} );
}
catch (Exception exception)
{
logger.LogError(exception, "Error generating picture workflow paths for key {SourceKeyName}",
executeContext.Arguments.SourceKeyName);
await processTrackingStore.FailStepAsync(processId, stepId, exception.Message, executeContext.CancellationToken);
throw;
}
}
}
}
@@ -0,0 +1,113 @@
using MassTransit;
using Microsoft.Extensions.Logging;
using Musify.Application.Contracts.Infrastructure;
using Musify.Domain.Entities;
using Musify.Infrastructure.Messaging.Activities.Arguments;
using Musify.Infrastructure.Messaging.Activities.Logs;
namespace Musify.Infrastructure.Messaging.Activities
{
public class ResizePictureActivity(
IPictureHandler pictureHandler,
ILogger<ResizePictureActivity> logger,
IProcessTrackingStore processTrackingStore)
: IActivity<ResizePictureLocalArguments, ResizePictureLog>
{
public const string ExecuteEndpointName = "Resize-Picture";
public async Task<ExecutionResult> Execute(ExecuteContext<ResizePictureLocalArguments> executeContext)
{
var processId = await processTrackingStore.GetOrCreateProcessAsync(
"RoutingSlip",
executeContext.CorrelationId ?? executeContext.TrackingNumber,
executeContext.ConversationId,
executeContext.MessageId,
executeContext.CancellationToken);
var stepId = await processTrackingStore.StartStepAsync(
processId,
nameof(ResizePictureActivity),
ProcessStepComponentType.Activity,
0,
executeContext.CancellationToken);
var sourceFilePath = executeContext.GetVariable<string>(executeContext.Arguments.SourceFilePathVariableName);
var destinationFilePath = executeContext.GetVariable<string>(executeContext.Arguments.DestinationFilePathVariableName);
if (string.IsNullOrWhiteSpace(sourceFilePath) || string.IsNullOrWhiteSpace(destinationFilePath))
{
throw new InvalidOperationException("Resize activity requires source and destination file path variables.");
}
try
{
if (!File.Exists(sourceFilePath))
{
logger.LogWarning("Source file not found at {SourceFilePath}", sourceFilePath);
throw new FileNotFoundException($"Source file not found: {sourceFilePath}");
}
await using var fileStream = File.OpenRead(sourceFilePath);
var resizedPicture = await pictureHandler.ResizePictureAsync(
fileStream,
executeContext.Arguments.Width,
executeContext.Arguments.Height,
executeContext.CancellationToken);
if (!resizedPicture.IsSuccess)
{
var errorMessage = string.Join("; ", resizedPicture.Errors);
logger.LogError("Failed to resize picture {SourceFilePath} to {Width}x{Height}. Errors: {Errors}",
sourceFilePath,
executeContext.Arguments.Width,
executeContext.Arguments.Height,
errorMessage);
throw new Exception(errorMessage);
}
var destinationDirectory = Path.GetDirectoryName(destinationFilePath);
if (!string.IsNullOrEmpty(destinationDirectory))
{
Directory.CreateDirectory(destinationDirectory);
}
await using var destinationStream = File.Create(destinationFilePath);
await resizedPicture.Value.CopyToAsync(destinationStream, executeContext.CancellationToken);
logger.LogDebug("Picture resized from {SourceFilePath} to {DestinationFilePath} ({Width}x{Height})",
sourceFilePath,
destinationFilePath,
executeContext.Arguments.Width,
executeContext.Arguments.Height);
await processTrackingStore.CompleteStepAsync(processId, stepId, executeContext.CancellationToken);
return executeContext.Completed();
}
catch (Exception exception)
{
logger.LogError(exception, "Error resizing picture {SourceFilePath}", sourceFilePath);
await processTrackingStore.FailStepAsync(processId, stepId, exception.Message, executeContext.CancellationToken);
throw;
}
}
public Task<CompensationResult> Compensate(CompensateContext<ResizePictureLog> compensateContext)
{
try
{
if (File.Exists(compensateContext.Log.DestinationFilePath))
{
File.Delete(compensateContext.Log.DestinationFilePath);
}
return Task.FromResult(compensateContext.Compensated());
}
catch (Exception exception)
{
logger.LogError(exception, "Error compensating resized file {DestinationFilePath}", compensateContext.Log.DestinationFilePath);
return Task.FromResult(compensateContext.Failed(exception));
}
}
}
}
@@ -0,0 +1,107 @@
using MassTransit;
using Microsoft.Extensions.Logging;
using Musify.Application.Contracts.Infrastructure;
using Musify.Domain.Entities;
using Musify.Infrastructure.Messaging.Activities.Arguments;
using Musify.Infrastructure.Messaging.Activities.Logs;
namespace Musify.Infrastructure.Messaging.Activities
{
public class UpdatePlayListPictureActivity(
IDatabase database,
ILogger<UpdatePlayListPictureActivity> logger,
IProcessTrackingStore processTrackingStore)
: IActivity<UpdatePlayListPictureArguments, UpdatePlayListPictureLog>
{
public const string ExecuteEndpointName = "Update-PlayList-Picture";
public async Task<ExecutionResult> Execute(ExecuteContext<UpdatePlayListPictureArguments> executeContext)
{
var processId = await processTrackingStore.GetOrCreateProcessAsync(
"RoutingSlip",
executeContext.CorrelationId ?? executeContext.TrackingNumber,
executeContext.ConversationId,
executeContext.MessageId,
executeContext.CancellationToken);
var stepId = await processTrackingStore.StartStepAsync(
processId,
nameof(UpdatePlayListPictureActivity),
ProcessStepComponentType.Activity,
0,
executeContext.CancellationToken);
try
{
var playList = await database.PlayLists.FindAsync(
[executeContext.Arguments.PlayListId],
cancellationToken: executeContext.CancellationToken);
if (playList is null)
{
logger.LogWarning("PlayList with id {PlayListId} not found",
executeContext.Arguments.PlayListId);
throw new InvalidOperationException($"PlayList with id {executeContext.Arguments.PlayListId} not found");
}
var log = new UpdatePlayListPictureLog(
playList.Id,
playList.OriginalPictureKeyName,
playList.SmallPictureKeyName,
playList.MediumPictureKeyName,
playList.LargePictureKeyName);
playList.OriginalPictureKeyName = executeContext.Arguments.OriginalPictureKeyName;
playList.SmallPictureKeyName = executeContext.Arguments.SmallPictureKeyName;
playList.MediumPictureKeyName = executeContext.Arguments.MediumPictureKeyName;
playList.LargePictureKeyName = executeContext.Arguments.LargePictureKeyName;
database.PlayLists.Update(playList);
await database.SaveChangesAsync(executeContext.CancellationToken);
logger.LogInformation("Updated PlayList {PlayListId} pictures",
executeContext.Arguments.PlayListId);
await processTrackingStore.CompleteStepAsync(processId, stepId, executeContext.CancellationToken);
return executeContext.Completed();
}
catch (Exception exception)
{
logger.LogError(exception, "Error updating PlayList {PlayListId} pictures",
executeContext.Arguments.PlayListId);
await processTrackingStore.FailStepAsync(processId, stepId, exception.Message, executeContext.CancellationToken);
throw;
}
}
public async Task<CompensationResult> Compensate(CompensateContext<UpdatePlayListPictureLog> compensateContext)
{
try
{
var playList = await database.PlayLists.FindAsync(
[compensateContext.Log.PlayListId],
cancellationToken: compensateContext.CancellationToken);
if (playList is null)
{
return compensateContext.Compensated();
}
playList.OriginalPictureKeyName = compensateContext.Log.PreviousOriginalPictureKeyName;
playList.SmallPictureKeyName = compensateContext.Log.PreviousSmallPictureKeyName;
playList.MediumPictureKeyName = compensateContext.Log.PreviousMediumPictureKeyName;
playList.LargePictureKeyName = compensateContext.Log.PreviousLargePictureKeyName;
database.PlayLists.Update(playList);
await database.SaveChangesAsync(compensateContext.CancellationToken);
return compensateContext.Compensated();
}
catch (Exception exception)
{
logger.LogError(exception, "Error compensating PlayList {PlayListId} pictures", compensateContext.Log.PlayListId);
return compensateContext.Failed(exception);
}
}
}
}
@@ -0,0 +1,107 @@
using MassTransit;
using Microsoft.Extensions.Logging;
using Musify.Application.Contracts.Infrastructure;
using Musify.Domain.Entities;
using Musify.Infrastructure.Messaging.Activities.Arguments;
using Musify.Infrastructure.Messaging.Activities.Logs;
namespace Musify.Infrastructure.Messaging.Activities
{
public class UpdateTrackPictureActivity(
IDatabase database,
ILogger<UpdateTrackPictureActivity> logger,
IProcessTrackingStore processTrackingStore)
: IActivity<UpdateTrackPictureArguments, UpdateTrackPictureLog>
{
public const string ExecuteEndpointName = "Update-Track-Picture";
public async Task<ExecutionResult> Execute(ExecuteContext<UpdateTrackPictureArguments> executeContext)
{
var processId = await processTrackingStore.GetOrCreateProcessAsync(
"RoutingSlip",
executeContext.CorrelationId ?? executeContext.TrackingNumber,
executeContext.ConversationId,
executeContext.MessageId,
executeContext.CancellationToken);
var stepId = await processTrackingStore.StartStepAsync(
processId,
nameof(UpdateTrackPictureActivity),
ProcessStepComponentType.Activity,
0,
executeContext.CancellationToken);
try
{
var track = await database.Tracks.FindAsync(
[executeContext.Arguments.TrackId],
cancellationToken: executeContext.CancellationToken);
if (track is null)
{
logger.LogWarning("Track with id {TrackId} not found",
executeContext.Arguments.TrackId);
throw new InvalidOperationException($"Track with id {executeContext.Arguments.TrackId} not found");
}
var log = new UpdateTrackPictureLog(
track.Id,
track.OriginalPictureKeyName,
track.SmallPictureKeyName,
track.MediumPictureKeyName,
track.LargePictureKeyName);
track.OriginalPictureKeyName = executeContext.Arguments.OriginalPictureKeyName;
track.SmallPictureKeyName = executeContext.Arguments.SmallPictureKeyName;
track.MediumPictureKeyName = executeContext.Arguments.MediumPictureKeyName;
track.LargePictureKeyName = executeContext.Arguments.LargePictureKeyName;
database.Tracks.Update(track);
await database.SaveChangesAsync(executeContext.CancellationToken);
logger.LogInformation("Updated Track {TrackId} pictures",
executeContext.Arguments.TrackId);
await processTrackingStore.CompleteStepAsync(processId, stepId, executeContext.CancellationToken);
return executeContext.Completed();
}
catch (Exception exception)
{
logger.LogError(exception, "Error updating Track {TrackId} pictures",
executeContext.Arguments.TrackId);
await processTrackingStore.FailStepAsync(processId, stepId, exception.Message, executeContext.CancellationToken);
throw;
}
}
public async Task<CompensationResult> Compensate(CompensateContext<UpdateTrackPictureLog> compensateContext)
{
try
{
var track = await database.Tracks.FindAsync(
[compensateContext.Log.TrackId],
cancellationToken: compensateContext.CancellationToken);
if (track is null)
{
return compensateContext.Compensated();
}
track.OriginalPictureKeyName = compensateContext.Log.PreviousOriginalPictureKeyName;
track.SmallPictureKeyName = compensateContext.Log.PreviousSmallPictureKeyName;
track.MediumPictureKeyName = compensateContext.Log.PreviousMediumPictureKeyName;
track.LargePictureKeyName = compensateContext.Log.PreviousLargePictureKeyName;
database.Tracks.Update(track);
await database.SaveChangesAsync(compensateContext.CancellationToken);
return compensateContext.Compensated();
}
catch (Exception exception)
{
logger.LogError(exception, "Error compensating Track {TrackId} pictures", compensateContext.Log.TrackId);
return compensateContext.Failed(exception);
}
}
}
}