This commit is contained in:
2026-05-13 02:20:22 +02:00
parent 57a5358701
commit fa6eaf5a2b
27 changed files with 445 additions and 227 deletions
@@ -16,5 +16,7 @@
Task CopyFileAsync(string sourceBucket, string sourceKey, string destinationBucket, string destinationKey,
CancellationToken cancellationToken);
Task RemoveFolderAsync(string bucket, string folderKey, CancellationToken cancellationToken);
}
}
+6
View File
@@ -0,0 +1,6 @@
namespace Musify.Application.Events
{
public record DeleteTrackEvent(
Guid TrackId,
Guid UserId);
}
@@ -2,15 +2,17 @@ using Ardalis.Result;
using Mediator;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Musify.Application.Configuration;
using Musify.Application.Contracts.Infrastructure;
using Musify.Application.Tracks.Commands;
using Musify.Domain.Entities;
using Musify.Application.Events;
namespace Musify.Application.Tracks.Handler
{
public class DeleteTrackCommandHandler(IDatabase database, IStorageService storageHandler, ILogger<DeleteTrackCommandHandler> logger,
TrackConfiguration trackConfiguration, ApplicationStorageConfiguration storageConfiguration)
public class DeleteTrackCommandHandler(
IDatabase database,
IEventBus eventBus,
ILogger<DeleteTrackCommandHandler> logger)
: ICommandHandler<DeleteTrackCommand, Result>
{
public async ValueTask<Result> Handle(DeleteTrackCommand request, CancellationToken cancellationToken)
@@ -28,48 +30,22 @@ namespace Musify.Application.Tracks.Handler
return Result.Unauthorized();
}
await RemovePictures(track, cancellationToken);
await RemoveAudios(track, cancellationToken);
if (track.AudioTranscodeProcessingStatus == ProcessingStatus.Processing
|| track.AudioTranscodeProcessingStatus == ProcessingStatus.Pending)
{
logger.LogWarning("Track {TrackId} is currently being processed and cannot be deleted", request.TrackId);
return Result.Conflict("Track is currently being processed and cannot be deleted");
}
track.PicturesProcessingStatus = ProcessingStatus.Processing;
track.AudioTranscodeProcessingStatus = ProcessingStatus.Processing;
await database.SaveChangesAsync(cancellationToken);
await eventBus.PublishAsync(
new DeleteTrackEvent(track.Id, request.UserId),
cancellationToken);
return Result.NoContent();
}
async Task RemovePictures(Track track, CancellationToken cancellationToken)
{
await RemoveFile(trackConfiguration.Routes.BuildOriginalPicturePath(track.OriginalPictureName),
cancellationToken);
if (track.SmallPictureName != trackConfiguration.Routes.PresetSmallPicture)
await RemoveFile(trackConfiguration.Routes.BuildSmallPicturePath(track.SmallPictureName),
cancellationToken);
if (track.MediumPictureName != trackConfiguration.Routes.PresetMediumPicture)
await RemoveFile(trackConfiguration.Routes.BuildMediumPicturePath(track.MediumPictureName),
cancellationToken);
if (track.LargePictureName != trackConfiguration.Routes.PresetLargePicture)
await RemoveFile(trackConfiguration.Routes.BuildLargePicturePath(track.LargePictureName),
cancellationToken);
}
async Task RemoveAudios(Track track, CancellationToken cancellationToken)
{
await RemoveFile(trackConfiguration.Routes.BuildOriginalAudioPath(track.OriginalAudioName),
cancellationToken);
await RemoveFile(trackConfiguration.Routes.BuildProcessedAudioPath(track.AudioFolderName),
cancellationToken);
}
async Task RemoveFile(string path, CancellationToken cancellationToken)
{
try
{
await storageHandler.RemoveFileAsync(storageConfiguration.Bucket, path, cancellationToken);
logger.LogDebug("Removed file {Path} from storage", path);
}
catch (Exception exception)
{
logger.LogError(exception, "Failed to remove file {Path} from storage", path);
}
}
}
}
+10
View File
@@ -0,0 +1,10 @@
namespace Musify.Domain.Entities
{
public enum ProcessingStatus
{
Pending = 0,
Processing = 1,
Completed = 2,
Failed = 3
}
}
+27 -7
View File
@@ -1,4 +1,6 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics.CodeAnalysis;
namespace Musify.Domain.Entities
{
@@ -20,24 +22,32 @@ namespace Musify.Domain.Entities
[MaxLength(64)]
public required string OriginalPictureName { get; set; }
[Required]
[MaxLength(64)]
public required string SmallPictureName { get; set; }
public string? SmallPictureName { get; set; }
[Required]
[MaxLength(64)]
public required string MediumPictureName { get; set; }
public string? MediumPictureName { get; set; }
[Required]
[MaxLength(64)]
public required string LargePictureName { get; set; }
public string? LargePictureName { get; set; }
[Required]
[MaxLength(64)]
public required string OriginalAudioName { get; set; }
[MaxLength(64)]
public string AudioFolderName { get; set; } = string.Empty;
public string? AudioFolderName { get; set; }
[Required]
public ProcessingStatus PicturesProcessingStatus { get; set; } = ProcessingStatus.Pending;
[Required]
public ProcessingStatus AudioTranscodeProcessingStatus { get; set; } = ProcessingStatus.Pending;
[Required]
public int RetryCount { get; set; } = 0;
public DateTime? LastRetryAt { get; set; }
[Required]
public DateTime CreatedDate { get; set; } = DateTime.UtcNow;
@@ -48,5 +58,15 @@ namespace Musify.Domain.Entities
public ICollection<UserHasTrack> UserTracks { get; set; } = new List<UserHasTrack>();
public ICollection<PlayListHasTrack> PlayListTracks { get; set; } = new List<PlayListHasTrack>();
[NotMapped]
[MemberNotNullWhen(true, nameof(SmallPictureName)) ]
[MemberNotNullWhen(true, nameof(MediumPictureName)) ]
[MemberNotNullWhen(true, nameof(LargePictureName)) ]
public bool IsPicturesProcessed => PicturesProcessingStatus == ProcessingStatus.Completed;
[NotMapped]
[MemberNotNullWhen(true, nameof(AudioFolderName)) ]
public bool IsAudioProcessed => AudioTranscodeProcessingStatus == ProcessingStatus.Completed;
}
}
@@ -1,4 +1,5 @@
using MassTransit;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Musify.Application.Contracts.Infrastructure;
using Musify.Domain.Entities;
@@ -6,42 +7,41 @@ using Musify.Infrastructure.MassTransit.Activities.Arguments;
namespace Musify.Infrastructure.MassTransit.Activities
{
public class GenerateAudioWorkflowPathsActivity(
ILogger<GenerateAudioWorkflowPathsActivity> logger,
IProcessTrackingStore processTrackingStore)
internal class GenerateAudioWorkflowPathsActivity(
IDatabase database,
ILogger<GenerateAudioWorkflowPathsActivity> logger)
: IExecuteActivity<GenerateAudioWorkflowPathsArguments>
{
public const string ExecuteEndpointName = "generate-audio-workflow-paths";
public async Task<ExecutionResult> Execute(ExecuteContext<GenerateAudioWorkflowPathsArguments> executeContext)
{
var processId = await processTrackingStore.GetOrCreateProcessAsync(
"RoutingSlip",
executeContext.CorrelationId ?? executeContext.TrackingNumber,
executeContext.ConversationId,
executeContext.MessageId,
executeContext.CancellationToken);
var stepId = await processTrackingStore.StartStepAsync(
processId,
nameof(GenerateAudioWorkflowPathsActivity),
ProcessStepComponentType.Activity,
0,
executeContext.CancellationToken);
Track track;
try
{
var getTrack = await database.Tracks.SingleOrDefaultAsync(p => p.Id == executeContext.Arguments.TrackId, executeContext.CancellationToken);
track = getTrack ?? throw new InvalidOperationException($"Track with id {executeContext.Arguments.TrackId} not found.");
}
catch (Exception exception)
{
logger.LogError(exception, "Failed to retrieve track for TrackId {TrackId}",
executeContext.Arguments.TrackId);
throw;
}
try
{
track.AudioTranscodeProcessingStatus = ProcessingStatus.Processing;
var folderName = Guid.NewGuid().ToString();
var sourceFileName = Path.GetFileName(executeContext.Arguments.SourceKey);
var workingDirectory = Path.Combine(executeContext.Arguments.TemporaryRootDirectory, folderName);
var sourceFilePath = Path.Combine(workingDirectory, sourceFileName);
Directory.CreateDirectory(workingDirectory);
await database.SaveChangesAsync(executeContext.CancellationToken);
logger.LogDebug("Generated audio workflow paths in {WorkingDirectory}",
workingDirectory);
logger.LogDebug("Generated audio workflow paths in {WorkingDirectory}", workingDirectory);
await processTrackingStore.CompleteStepAsync(processId, stepId, executeContext.CancellationToken);
return executeContext.CompletedWithVariables(new Dictionary<string, object>
{
[RoutingSlipVariableNames.Workflow.TemporalDirectory] = workingDirectory,
@@ -54,9 +54,20 @@ namespace Musify.Infrastructure.MassTransit.Activities
{
logger.LogError(exception, "Failed to generate audio workflow paths for {SourceKey}",
executeContext.Arguments.SourceKey);
await processTrackingStore.FailStepAsync(processId, stepId, exception.Message, executeContext.CancellationToken);
try
{
track.AudioTranscodeProcessingStatus = ProcessingStatus.Failed;
await database.SaveChangesAsync(executeContext.CancellationToken);
}
catch (Exception dbException)
{
logger.LogError(dbException, "Failed to update ProcessingStatus to Failed for track {TrackId}",
executeContext.Arguments.TrackId);
}
throw;
}
}
}
}
}
@@ -1,4 +1,5 @@
using MassTransit;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Musify.Application.Contracts.Infrastructure;
using Musify.Domain.Entities;
@@ -7,54 +8,59 @@ using Musify.Infrastructure.MassTransit.Activities.Logs;
namespace Musify.Infrastructure.MassTransit.Activities
{
public class TranscodeDashAudioActivity(
internal class TranscodeDashAudioActivity(
IDatabase database,
IAudioTranscoderService audioTranscoder,
ILogger<TranscodeDashAudioActivity> logger,
IProcessTrackingStore processTrackingStore)
ILogger<TranscodeDashAudioActivity> logger)
: IActivity<TranscodeDashAudioArguments, TranscodeDashAudioLog>
{
public const string ExecuteEndpointName = "transcode-dash-audio";
public async Task<ExecutionResult> Execute(ExecuteContext<TranscodeDashAudioArguments> executeContext)
{
var processId = await processTrackingStore.GetOrCreateProcessAsync(
"RoutingSlip",
executeContext.CorrelationId ?? executeContext.TrackingNumber,
executeContext.ConversationId,
executeContext.MessageId,
executeContext.CancellationToken);
var stepId = await processTrackingStore.StartStepAsync(
processId,
nameof(TranscodeDashAudioActivity),
ProcessStepComponentType.Activity,
0,
executeContext.CancellationToken);
var sourceFilePath = executeContext.GetVariable<string>(executeContext.Arguments.SourceFilePathVariable);
ArgumentNullException.ThrowIfNull(sourceFilePath, nameof(sourceFilePath));
var workingDirectory = executeContext.GetVariable<string>(executeContext.Arguments.WorkingDirectoryVariable);
ArgumentNullException.ThrowIfNull(workingDirectory, nameof(workingDirectory));
Track track;
try
{
await using (var fileStream = File.OpenRead(sourceFilePath))
{
await audioTranscoder.TranscodeToDashAsync(
fileStream,
workingDirectory,
executeContext.CancellationToken);
}
var getTrack = await database.Tracks.SingleOrDefaultAsync(t => t.Id == executeContext.Arguments.TrackId, executeContext.CancellationToken);
track = getTrack ?? throw new InvalidOperationException($"Track with Id {executeContext.Arguments.TrackId} not found");
}
catch (Exception exception)
{
logger.LogError(exception, "Failed to retrieve track with Id {TrackId} from database", executeContext.Arguments.TrackId);
throw;
}
try
{
await using var fileStream = File.OpenRead(sourceFilePath);
await audioTranscoder.TranscodeToDashAsync(
fileStream,
workingDirectory,
executeContext.CancellationToken);
File.Delete(sourceFilePath);
await processTrackingStore.CompleteStepAsync(processId, stepId, executeContext.CancellationToken);
return executeContext.Completed();
}
catch (Exception exception)
{
logger.LogError(exception, "Failed to transcode {SourceFilePath}",
sourceFilePath);
await processTrackingStore.FailStepAsync(processId, stepId, exception.Message, executeContext.CancellationToken);
logger.LogError(exception, "Failed to transcode {SourceFilePath}", sourceFilePath);
try
{
track.AudioTranscodeProcessingStatus = ProcessingStatus.Failed;
await database.SaveChangesAsync(executeContext.CancellationToken);
}
catch (Exception dbException)
{
logger.LogError(dbException, "Failed to update ProcessingStatus to Failed for track {TrackId}",
executeContext.Arguments.TrackId);
}
throw;
}
}
@@ -1,4 +1,5 @@
using MassTransit;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Musify.Application.Contracts.Infrastructure;
using Musify.Domain.Entities;
@@ -7,63 +8,76 @@ using Musify.Infrastructure.MassTransit.Logs;
namespace Musify.Infrastructure.MassTransit.Activities.Audio
{
public class UpdateTrackAudioActivity(
internal class UpdateTrackAudioActivity(
IDatabase database,
ILogger<TranscodeDashAudioActivity> logger,
IProcessTrackingStore processTrackingStore)
ILogger<UpdateTrackAudioActivity> logger)
: IActivity<UpdateTrackAudioArguments, UpdateTrackAudioLog>
{
public const string ExecuteEndpointName = "update-track-audio";
public async Task<ExecutionResult> Execute(ExecuteContext<UpdateTrackAudioArguments> executeContext)
{
var processId = await processTrackingStore.GetOrCreateProcessAsync(
"RoutingSlip",
executeContext.CorrelationId ?? executeContext.TrackingNumber,
executeContext.ConversationId,
executeContext.MessageId,
executeContext.CancellationToken);
Track track;
try
{
var getTrack = await database.Tracks.SingleOrDefaultAsync(t => t.Id == executeContext.Arguments.TrackId, executeContext.CancellationToken);
track = getTrack ?? throw new InvalidOperationException($"Track with id {executeContext.Arguments.TrackId} not found");
}
catch (Exception exception)
{
logger.LogError(exception, "Failed to retrieve track {TrackId}",
executeContext.Arguments.TrackId);
throw;
}
var stepId = await processTrackingStore.StartStepAsync(
processId,
nameof(UpdateTrackAudioActivity),
ProcessStepComponentType.Activity,
0,
executeContext.CancellationToken);
string? audioFolderKey;
try
{
audioFolderKey = executeContext.GetVariable<string>(executeContext.Arguments.AudioFolderKeyVariable);
logger.LogDebug("Resolved audio folder key: {AudioFolderKey}", audioFolderKey);
}
catch (Exception exception)
{
logger.LogWarning(exception, "Audio folder key variable could not be resolved for track {TrackId}", track.Id);
return executeContext.Completed();
}
if (string.IsNullOrWhiteSpace(audioFolderKey))
{
logger.LogWarning("Audio folder key is empty for track {TrackId}; skipping update", track.Id);
return executeContext.Completed();
}
try
{
var track = await database.Tracks.FindAsync(
[executeContext.Arguments.TrackId],
cancellationToken: executeContext.CancellationToken);
if (track is null)
{
logger.LogWarning("Track {TrackId} not found",
executeContext.Arguments.TrackId);
throw new InvalidOperationException($"Track with id {executeContext.Arguments.TrackId} not found");
}
var log = new UpdateTrackAudioLog(
track.Id,
track.AudioFolderName);
track.AudioFolderName = Path.GetFileName(executeContext.Arguments.AudioFolderKey);
database.Tracks.Update(track);
track.AudioFolderName = Path.GetFileName(audioFolderKey);
track.AudioTranscodeProcessingStatus = ProcessingStatus.Completed;
await database.SaveChangesAsync(executeContext.CancellationToken);
logger.LogInformation("Updated track {TrackId} audio",
executeContext.Arguments.TrackId);
logger.LogInformation("Updated track {TrackId} audio", executeContext.Arguments.TrackId);
await processTrackingStore.CompleteStepAsync(processId, stepId, executeContext.CancellationToken);
return executeContext.Completed();
return executeContext.Completed(log);
}
catch (Exception exception)
{
logger.LogError(exception, "Failed to update track {TrackId} audio",
executeContext.Arguments.TrackId);
await processTrackingStore.FailStepAsync(processId, stepId, exception.Message, executeContext.CancellationToken);
try
{
track.AudioTranscodeProcessingStatus = ProcessingStatus.Failed;
await database.SaveChangesAsync(executeContext.CancellationToken);
}
catch (Exception dbException)
{
logger.LogError(dbException, "Failed to update ProcessingStatus to Failed for track {TrackId}",
executeContext.Arguments.TrackId);
}
throw;
}
}
@@ -72,25 +86,22 @@ namespace Musify.Infrastructure.MassTransit.Activities.Audio
{
try
{
var track = await database.Tracks.FindAsync(
[compensateContext.Log.TrackId],
cancellationToken: compensateContext.CancellationToken);
var track = await database.Tracks.SingleOrDefaultAsync(t => t.Id == compensateContext.Log.TrackId, compensateContext.CancellationToken);
if (track is null)
{
return compensateContext.Compensated();
}
throw new InvalidOperationException($"Track with id {compensateContext.Log.TrackId} not found");
track.AudioFolderName = compensateContext.Log.PreviousFolderAudioKey;
database.Tracks.Update(track);
track.AudioFolderName = compensateContext.Log.PreviousAudioFolderName;
track.AudioTranscodeProcessingStatus = ProcessingStatus.Failed;
await database.SaveChangesAsync(compensateContext.CancellationToken);
logger.LogInformation("Compensated track {TrackId} audio", compensateContext.Log.TrackId);
return compensateContext.Compensated();
}
catch (Exception exception)
{
logger.LogError(exception, "Failed to compensate track {TrackId} audio", compensateContext.Log.TrackId);
logger.LogError(exception, "Failed to compensate track {TrackId} audio",
compensateContext.Log.TrackId);
return compensateContext.Failed(exception);
}
}
@@ -5,9 +5,9 @@ using Musify.Domain.Entities;
using Musify.Infrastructure.MassTransit.Activities.Arguments;
using Musify.Infrastructure.MassTransit.Activities.Logs;
namespace Musify.Infrastructure.MassTransit.Activities
namespace Musify.Infrastructure.MassTransit.Activities.Files
{
public class DownloadFileFromBucketActivity(
internal class DownloadFileFromBucketActivity(
IStorageService storageHandler,
ILogger<DownloadFileFromBucketActivity> logger,
IProcessTrackingStore processTrackingStore)
@@ -1,43 +1,25 @@
using MassTransit;
using Microsoft.Extensions.Logging;
using Musify.Application.Contracts.Infrastructure;
using Musify.Domain.Entities;
using Musify.Infrastructure.MassTransit.Activities.Arguments;
namespace Musify.Infrastructure.MassTransit.Activities
namespace Musify.Infrastructure.MassTransit.Activities.Files
{
public class RemoveFileFromBucketActivity(
internal class RemoveFileFromBucketActivity(
IStorageService storageHandler,
ILogger<RemoveFileFromBucketActivity> logger,
IProcessTrackingStore processTrackingStore)
ILogger<RemoveFileFromBucketActivity> logger)
: IExecuteActivity<RemoveFileFromBucketArguments>
{
public const string ExecuteEndpointName = "remove-file-from-bucket";
public async Task<ExecutionResult> Execute(ExecuteContext<RemoveFileFromBucketArguments> executeContext)
{
var processId = await processTrackingStore.GetOrCreateProcessAsync(
"RoutingSlip",
executeContext.CorrelationId ?? executeContext.TrackingNumber,
executeContext.ConversationId,
executeContext.MessageId,
executeContext.CancellationToken);
var stepId = await processTrackingStore.StartStepAsync(
processId,
nameof(RemoveFileFromBucketActivity),
ProcessStepComponentType.Activity,
0,
executeContext.CancellationToken);
try
{
await storageHandler.RemoveFileAsync(
executeContext.Arguments.Bucket,
executeContext.Arguments.Key,
executeContext.CancellationToken);
await processTrackingStore.CompleteStepAsync(processId, stepId, executeContext.CancellationToken);
return executeContext.Completed();
}
catch (Exception exception)
@@ -45,7 +27,6 @@ namespace Musify.Infrastructure.MassTransit.Activities
logger.LogError(exception, "Failed to remove {Key} from bucket {Bucket}",
executeContext.Arguments.Key,
executeContext.Arguments.Bucket);
await processTrackingStore.FailStepAsync(processId, stepId, exception.Message, executeContext.CancellationToken);
throw;
}
}
@@ -0,0 +1,42 @@
using MassTransit;
using Microsoft.Extensions.Logging;
using Musify.Application.Contracts.Infrastructure;
using Musify.Infrastructure.MassTransit.Arguments;
namespace Musify.Infrastructure.MassTransit.Activities.Files
{
internal class RemoveFolderFromBucketActivity(
IStorageService storageHandler,
ILogger<RemoveFolderFromBucketActivity> logger)
: IExecuteActivity<RemoveFolderFromBucketArguments>
{
public const string ExecuteEndpointName = "remove-folder-from-bucket";
public async Task<ExecutionResult> Execute(ExecuteContext<RemoveFolderFromBucketArguments> executeContext)
{
var folderKey = executeContext.GetVariable<string>(executeContext.Arguments.FolderKeyVariable);
ArgumentException.ThrowIfNullOrEmpty(folderKey, nameof(folderKey));
try
{
await storageHandler.RemoveFolderAsync(
executeContext.Arguments.Bucket,
folderKey,
executeContext.CancellationToken);
logger.LogInformation("Removed folder {FolderKey} from bucket {Bucket}",
folderKey,
executeContext.Arguments.Bucket);
return executeContext.Completed();
}
catch (Exception exception)
{
logger.LogError(exception, "Failed to remove folder {FolderKey} from bucket {Bucket}",
folderKey,
executeContext.Arguments.Bucket);
throw;
}
}
}
}
@@ -9,28 +9,13 @@ namespace Musify.Infrastructure.MassTransit.Activities
{
public class UpdateTrackPictureActivity(
IDatabase database,
ILogger<UpdateTrackPictureActivity> logger,
IProcessTrackingStore processTrackingStore)
ILogger<UpdateTrackPictureActivity> logger)
: 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);
var smallResizedVariable = executeContext.GetVariable<string>(executeContext.Arguments.SmallPictureVariable);
ArgumentNullException.ThrowIfNull(smallResizedVariable, nameof(smallResizedVariable));
var mediumResizedVariable = executeContext.GetVariable<string>(executeContext.Arguments.MediumPictureVariable);
@@ -69,14 +54,12 @@ namespace Musify.Infrastructure.MassTransit.Activities
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, "Failed to update track {TrackId} pictures",
executeContext.Arguments.TrackId);
await processTrackingStore.FailStepAsync(processId, stepId, exception.Message, executeContext.CancellationToken);
throw;
}
}
@@ -0,0 +1,115 @@
using MassTransit;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Musify.Application.Configuration;
using Musify.Application.Contracts.Infrastructure;
using Musify.Domain.Entities;
namespace Musify.Infrastructure.MassTransit.Activities.Tracks
{
internal class DeleteTrackActivity(
IDatabase database,
IStorageService storageService,
TrackConfiguration trackConfiguration,
ApplicationStorageConfiguration storageConfiguration,
ILogger<DeleteTrackActivity> logger)
: IExecuteActivity<DeleteTrackArguments>
{
public const string ExecuteEndpointName = "delete-track";
public async Task<ExecutionResult> Execute(ExecuteContext<DeleteTrackArguments> executeContext)
{
Track track;
try
{
var getTrack = await database.Tracks.SingleOrDefaultAsync(t => t.Id == executeContext.Arguments.TrackId, executeContext.CancellationToken);
track = getTrack ?? throw new InvalidOperationException($"Track with id {executeContext.Arguments.TrackId} not found");
}
catch (Exception exception)
{
logger.LogError(exception, "Failed to retrieve track {TrackId} for deletion", executeContext.Arguments.TrackId);
throw;
}
try
{
await RemovePicturesAsync(track, executeContext.CancellationToken);
track.PicturesProcessingStatus = ProcessingStatus.Completed;
await database.SaveChangesAsync(executeContext.CancellationToken);
await RemoveAudiosAsync(track, executeContext.CancellationToken);
track.AudioTranscodeProcessingStatus = ProcessingStatus.Completed;
await database.SaveChangesAsync(executeContext.CancellationToken);
database.Tracks.Remove(track);
await database.SaveChangesAsync(executeContext.CancellationToken);
logger.LogInformation("Deleted track {TrackId}", track.Id);
return executeContext.Completed();
}
catch (Exception exception)
{
logger.LogError(exception, "Failed to delete track {TrackId}", executeContext.Arguments.TrackId);
try
{
track.PicturesProcessingStatus = ProcessingStatus.Failed;
track.AudioTranscodeProcessingStatus = ProcessingStatus.Failed;
await database.SaveChangesAsync(executeContext.CancellationToken);
}
catch (Exception dbException)
{
logger.LogError(dbException, "Failed to update processing statuses to Failed for track {TrackId}", executeContext.Arguments.TrackId);
}
throw;
}
}
async Task RemovePicturesAsync(Track track, CancellationToken cancellationToken)
{
await RemoveFileAsync(trackConfiguration.Routes.BuildOriginalPicturePath(track.OriginalPictureName), cancellationToken);
if (!track.IsPicturesProcessed)
{
logger.LogInformation(
"Track {TrackId} pictures are not fully processed, skipping removal of small, medium, and large pictures",
track.Id);
return;
}
await RemoveFileAsync(trackConfiguration.Routes.BuildSmallPicturePath(track.SmallPictureName), cancellationToken);
await RemoveFileAsync(trackConfiguration.Routes.BuildMediumPicturePath(track.MediumPictureName), cancellationToken);
await RemoveFileAsync(trackConfiguration.Routes.BuildLargePicturePath(track.LargePictureName), cancellationToken);
}
async Task RemoveAudiosAsync(Track track, CancellationToken cancellationToken)
{
await RemoveFileAsync(trackConfiguration.Routes.BuildOriginalAudioPath(track.OriginalAudioName), cancellationToken);
if (!track.IsAudioProcessed)
{
logger.LogInformation(
"Track {TrackId} audio is not fully processed, skipping removal of processed audio",
track.Id);
return;
}
await RemoveFileAsync(trackConfiguration.Routes.BuildProcessedAudioPath(track.AudioFolderName), cancellationToken);
}
async Task RemoveFileAsync(string key, CancellationToken cancellationToken)
{
try
{
logger.LogDebug("Removing file {Key} from bucket {Bucket}", key, storageConfiguration.Bucket);
await storageService.RemoveFileAsync(storageConfiguration.Bucket, key, cancellationToken);
}
catch (Exception exception)
{
logger.LogError(exception, "Failed to remove file {Key} from bucket {Bucket}", key, storageConfiguration.Bucket);
throw;
}
}
}
}
@@ -0,0 +1,6 @@
namespace Musify.Infrastructure.MassTransit.Activities.Tracks
{
internal record DeleteTrackArguments(
Guid TrackId,
Guid UserId);
}
@@ -1,6 +1,7 @@
namespace Musify.Infrastructure.MassTransit.Activities.Arguments
{
public record GenerateAudioWorkflowPathsArguments(
Guid TrackId,
string TemporaryRootDirectory,
string SourceKey);
}
@@ -0,0 +1,6 @@
namespace Musify.Infrastructure.MassTransit.Arguments
{
public record RemoveFolderFromBucketArguments(
string Bucket,
string FolderKeyVariable);
}
@@ -1,6 +1,7 @@
namespace Musify.Infrastructure.MassTransit.Activities.Arguments
{
public record TranscodeDashAudioArguments(
Guid TrackId,
string SourceFilePathVariable,
string WorkingDirectoryVariable);
}
@@ -2,5 +2,5 @@
{
public record UpdateTrackAudioArguments(
Guid TrackId,
string AudioFolderKey);
string AudioFolderKeyVariable);
}
@@ -0,0 +1,23 @@
using MassTransit;
using Musify.Application.Events;
using Musify.Infrastructure.MassTransit.RoutingSlip.Builders;
namespace Musify.Infrastructure.MassTransit.Consumers
{
public class DeleteTrackConsumer(
IBus bus,
DeleteTrackRoutingSlipBuilder routingSlipBuilder)
: IConsumer<DeleteTrackEvent>
{
public const string QueueName = "delete-track";
public async Task Consume(ConsumeContext<DeleteTrackEvent> consumeContext)
{
var routingSlip = routingSlipBuilder
.Build(consumeContext.Message.TrackId, consumeContext.Message.UserId, consumeContext.CorrelationId)
.Build();
await bus.Execute(routingSlip);
}
}
}
@@ -1,23 +0,0 @@
using MassTransit;
using Musify.Application.Events;
using Musify.Infrastructure.MassTransit.RoutingSlip.Builders;
namespace Musify.Infrastructure.MassTransit.Consumers
{
public class RemoveFileConsumer(
IBus bus,
RemoveFileFromBucketRoutingSlipBuilder routingSlipBuilder)
: IConsumer<RemoveFileEvent>
{
public const string QueueName = "remove-file";
public async Task Consume(ConsumeContext<RemoveFileEvent> consumeContext)
{
var routingSlip = routingSlipBuilder
.Build(consumeContext.Message, consumeContext.CorrelationId)
.Build();
await bus.Execute(routingSlip);
}
}
}
@@ -14,6 +14,7 @@ using Musify.Infrastructure.MassTransit.Consumers;
using Musify.Infrastructure.MassTransit.Filters;
using Musify.Infrastructure.MassTransit.Logs;
using Musify.Infrastructure.MassTransit.RoutingSlip.Builders;
using Musify.Infrastructure.MassTransit.Activities.Tracks;
namespace Musify.Infrastructure.MassTransit
{
@@ -61,19 +62,20 @@ namespace Musify.Infrastructure.MassTransit
serviceDescriptors.AddScoped<AudioWorkflowRoutingSlipBuilder>();
serviceDescriptors.AddScoped<PictureWorkflowRoutingSlipBuilder>();
serviceDescriptors.AddScoped<RemoveFileFromBucketRoutingSlipBuilder>();
serviceDescriptors.AddScoped<DeleteTrackRoutingSlipBuilder>();
serviceDescriptors.AddMassTransit(options =>
{
options.AddConsumer<UpdatePlayListPictureConsumer>();
options.AddConsumer<RemoveFileConsumer>();
options.AddConsumer<UpdateTrackPictureConsumer>();
options.AddConsumer<RoutingSlipCleanUpConsumer>();
options.AddConsumer<UpdateTrackAudioConsumer>();
options.AddConsumer<DeleteTrackConsumer>();
options.AddExecuteActivity<RemoveFileFromBucketActivity, RemoveFileFromBucketArguments>();
options.AddExecuteActivity<GenerateAudioWorkflowPathsActivity, GenerateAudioWorkflowPathsArguments>();
options.AddExecuteActivity<GeneratePictureWorkflowPathsActivity, GeneratePictureWorkflowPathsArguments>();
options.AddExecuteActivity<DeleteTrackActivity, DeleteTrackArguments>();
options.AddActivity<ResizePictureActivity, ResizePictureLocalArguments, ResizePictureLog>();
options.AddActivity<UpdatePlayListPictureActivity, UpdatePlayListPictureArguments, UpdatePlayListPictureLog>();
@@ -95,15 +97,15 @@ namespace Musify.Infrastructure.MassTransit
busFactoryConfigurator.ReceiveEndpoint(UpdatePlayListPictureConsumer.QueueName, endpointConfigurator =>
endpointConfigurator.ConfigureConsumer<UpdatePlayListPictureConsumer>(busRegistrationContext));
busFactoryConfigurator.ReceiveEndpoint(RemoveFileConsumer.QueueName, endpointConfigurator =>
endpointConfigurator.ConfigureConsumer<RemoveFileConsumer>(busRegistrationContext));
busFactoryConfigurator.ReceiveEndpoint(UpdateTrackAudioConsumer.QueueName, endpointConfigurator =>
endpointConfigurator.ConfigureConsumer<UpdateTrackAudioConsumer>(busRegistrationContext));
busFactoryConfigurator.ReceiveEndpoint(UpdateTrackPictureConsumer.QueueName, endpointConfigurator =>
endpointConfigurator.ConfigureConsumer<UpdateTrackPictureConsumer>(busRegistrationContext));
busFactoryConfigurator.ReceiveEndpoint(DeleteTrackConsumer.QueueName, endpointConfigurator =>
endpointConfigurator.ConfigureConsumer<DeleteTrackConsumer>(busRegistrationContext));
busFactoryConfigurator.ReceiveEndpoint(RoutingSlipCleanUpConsumer.QueueName, endpointConfigurator =>
endpointConfigurator.ConfigureConsumer<RoutingSlipCleanUpConsumer>(busRegistrationContext));
@@ -2,5 +2,5 @@
{
public record UpdateTrackAudioLog(
Guid TrackId,
string PreviousFolderAudioKey);
string? PreviousAudioFolderName);
}
@@ -2,6 +2,7 @@ namespace Musify.Infrastructure.MassTransit.RoutingSlip
{
internal static class ActivityNames
{
internal const string DeleteTrack = "DeleteTrack";
internal const string GenerateAudioWorkflowPaths = "GenerateAudioWorkflowPaths";
internal const string GeneratePictureWorkflowPaths = "GeneratePictureWorkflowPaths";
internal const string DownloadFile = "DownloadFile";
@@ -3,6 +3,7 @@ using MassTransit.Courier.Contracts;
using Musify.Application.Events;
using Musify.Infrastructure.Configuration;
using Musify.Infrastructure.MassTransit.Activities.Audio;
using Musify.Infrastructure.MassTransit.Activities.Files;
using Musify.Infrastructure.MassTransit.Arguments;
using Musify.Infrastructure.MassTransit.Consumers;
using Musify.Infrastructure.MassTransit.Activities;
@@ -25,6 +26,7 @@ namespace Musify.Infrastructure.MassTransit.RoutingSlip.Builders
ActivityNames.GenerateAudioWorkflowPaths,
EndpointHelper.BuildExecuteActivityUri(GenerateAudioWorkflowPathsActivity.ExecuteEndpointName),
new GenerateAudioWorkflowPathsArguments(
message.TrackId,
workerConfiguration.Routes.TemporaryFilesDirectory,
message.SourceKey));
@@ -40,6 +42,7 @@ namespace Musify.Infrastructure.MassTransit.RoutingSlip.Builders
ActivityNames.TranscodeAudio,
EndpointHelper.BuildExecuteActivityUri(TranscodeDashAudioActivity.ExecuteEndpointName),
new TranscodeDashAudioArguments(
message.TrackId,
RoutingSlipVariableNames.Audio.SourceFilePath,
RoutingSlipVariableNames.Workflow.TemporalDirectory));
@@ -56,7 +59,7 @@ namespace Musify.Infrastructure.MassTransit.RoutingSlip.Builders
EndpointHelper.BuildExecuteActivityUri(UpdateTrackAudioActivity.ExecuteEndpointName),
new UpdateTrackAudioArguments(
message.TrackId,
message.DestinationFolderKey));
RoutingSlipVariableNames.Audio.TranscodedDirectory));
return routingSlipBuilder;
}
@@ -1,15 +1,13 @@
using MassTransit;
using MassTransit.Courier.Contracts;
using Musify.Application.Events;
using Musify.Infrastructure.MassTransit.Activities.Tracks;
using Musify.Infrastructure.MassTransit.Consumers;
using Musify.Infrastructure.MassTransit.Activities;
using Musify.Infrastructure.MassTransit.Activities.Arguments;
namespace Musify.Infrastructure.MassTransit.RoutingSlip.Builders
{
public class RemoveFileFromBucketRoutingSlipBuilder
public class DeleteTrackRoutingSlipBuilder
{
public RoutingSlipBuilder Build(RemoveFileEvent message, Guid? correlationId)
public RoutingSlipBuilder Build(Guid trackId, Guid userId, Guid? correlationId)
{
var routingSlipBuilder = new RoutingSlipBuilder(NewId.NextGuid());
routingSlipBuilder.AddVariable(RoutingSlipVariableNames.Workflow.CorrelationId, correlationId ?? Guid.Empty);
@@ -19,11 +17,9 @@ namespace Musify.Infrastructure.MassTransit.RoutingSlip.Builders
RoutingSlipEvents.Completed | RoutingSlipEvents.Faulted);
routingSlipBuilder.AddActivity(
ActivityNames.RemoveFile,
EndpointHelper.BuildExecuteActivityUri(RemoveFileFromBucketActivity.ExecuteEndpointName),
new RemoveFileFromBucketArguments(
message.Bucket,
message.Key));
ActivityNames.DeleteTrack,
EndpointHelper.BuildExecuteActivityUri(DeleteTrackActivity.ExecuteEndpointName),
new DeleteTrackArguments(trackId, userId));
return routingSlipBuilder;
}
@@ -13,6 +13,7 @@ namespace Musify.Infrastructure.MassTransit
internal const string SourceFilePath = "Audio.SourceFilePath";
internal const string WorkingDirectory = "Audio.WorkingDirectory";
internal const string TranscodedDirectory = "Audio.TranscodedDirectory";
internal const string PreviousAudioFolderName = "Audio.PreviousAudioFolderName";
}
internal static class Picture
+38
View File
@@ -180,5 +180,43 @@ namespace Musify.Infrastructure.Services
throw;
}
}
public async Task RemoveFolderAsync(string bucket, string folderKey, CancellationToken cancellationToken)
{
var prefix = folderKey.EndsWith('/') ? folderKey : $"{folderKey}/";
var listRequest = new ListObjectsV2Request
{
BucketName = bucket,
Prefix = prefix
};
ListObjectsV2Response listResponse;
do
{
listResponse = await amazonS3.ListObjectsV2Async(listRequest, cancellationToken);
if (listResponse.S3Objects.Count == 0)
break;
var deleteRequest = new DeleteObjectsRequest
{
BucketName = bucket,
Objects = listResponse.S3Objects
.Select(o => new KeyVersion { Key = o.Key } )
.ToList()
};
var deleteResponse = await amazonS3.DeleteObjectsAsync(deleteRequest, cancellationToken);
if (deleteResponse.DeleteErrors.Count > 0)
{
var errors = string.Join(", ", deleteResponse.DeleteErrors.Select(e => $"{e.Key}: {e.Message}"));
throw new InvalidOperationException($"Failed to delete some objects in folder {folderKey}: {errors}");
}
listRequest.ContinuationToken = listResponse.NextContinuationToken;
} while (listResponse.IsTruncated.HasValue && listResponse.IsTruncated.Value);
}
}
}