This commit is contained in:
2026-03-25 02:13:47 +01:00
parent 6e06d7673d
commit 3dea06b0cc
23 changed files with 260 additions and 79 deletions
@@ -39,21 +39,23 @@ namespace Musify.Infrastructure.Messaging.Activities
try
{
await using var fileStream = File.OpenRead(sourceFilePath);
var result = await audioTranscoder.TranscodeToDashAsync(
fileStream,
workingDirectory,
executeContext.CancellationToken);
if (!result.IsSuccess)
await using (var fileStream = File.OpenRead(sourceFilePath))
{
var errorMessage = string.Join("; ", result.Errors);
logger.LogError("Transcoding failed for file {SourceFilePath}. Errors: {ErrorMessage}",
sourceFilePath, errorMessage);
throw new Exception(errorMessage);
var result = await audioTranscoder.TranscodeToDashAsync(
fileStream,
workingDirectory,
executeContext.CancellationToken);
if (!result.IsSuccess)
{
var errorMessage = string.Join("; ", result.Errors);
logger.LogError("Transcoding failed for file {SourceFilePath}. Errors: {ErrorMessage}",
sourceFilePath, errorMessage);
throw new Exception(errorMessage);
}
}
File.Delete(sourceFilePath);
await processTrackingStore.CompleteStepAsync(processId, stepId, executeContext.CancellationToken);
return executeContext.Completed();
}
@@ -0,0 +1,99 @@
using MassTransit;
using Microsoft.Extensions.Logging;
using Musify.Application.Contracts.Infrastructure;
using Musify.Domain.Entities;
using Musify.Infrastructure.MassTransit.Arguments;
using Musify.Infrastructure.MassTransit.Logs;
using Musify.Infrastructure.Messaging.Activities;
namespace Musify.Infrastructure.MassTransit.Activities.Audio
{
public class UpdateTrackAudioActivity(
IDatabase database,
ILogger<TranscodeDashAudioActivity> logger,
IProcessTrackingStore processTrackingStore)
: 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);
var stepId = await processTrackingStore.StartStepAsync(
processId,
nameof(UpdateTrackAudioActivity),
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 UpdateTrackAudioLog(
track.Id,
track.AudioFolderKeyName);
track.AudioFolderKeyName = Path.GetFileName(executeContext.Arguments.AudioFolderKeyName);
database.Tracks.Update(track);
await database.SaveChangesAsync(executeContext.CancellationToken);
logger.LogInformation("Updated Track {TrackId} audio",
executeContext.Arguments.TrackId);
await processTrackingStore.CompleteStepAsync(processId, stepId, executeContext.CancellationToken);
return executeContext.Completed();
}
catch (Exception exception)
{
logger.LogError(exception, "Error updating Track {TrackId} audio",
executeContext.Arguments.TrackId);
await processTrackingStore.FailStepAsync(processId, stepId, exception.Message, executeContext.CancellationToken);
throw;
}
}
public async Task<CompensationResult> Compensate(CompensateContext<UpdateTrackAudioLog> compensateContext)
{
try
{
var track = await database.Tracks.FindAsync(
[compensateContext.Log.TrackId],
cancellationToken: compensateContext.CancellationToken);
if (track is null)
{
return compensateContext.Compensated();
}
track.AudioFolderKeyName = compensateContext.Log.PreviousFolderAudioKeyName;
database.Tracks.Update(track);
await database.SaveChangesAsync(compensateContext.CancellationToken);
return compensateContext.Compensated();
}
catch (Exception exception)
{
logger.LogError(exception, "Error compensating Track {TrackId} audio", compensateContext.Log.TrackId);
return compensateContext.Failed(exception);
}
}
}
}
@@ -46,7 +46,7 @@ namespace Musify.Infrastructure.Messaging.Activities
try
{
var uploadedKeys = Directory.GetFiles(folderPath)
.Select(file => $"{destinationKeyName}/{Path.GetFileName(file) }")
.Select(file => Path.Combine(destinationKeyName, Path.GetFileName(file)))
.ToArray();
var transferFilesResult = await storageHandler.TransferFilesAsync(
@@ -51,10 +51,10 @@ namespace Musify.Infrastructure.Messaging.Activities
playList.MediumPictureKeyName,
playList.LargePictureKeyName);
playList.OriginalPictureKeyName = executeContext.Arguments.OriginalPictureKeyName;
playList.SmallPictureKeyName = executeContext.Arguments.SmallPictureKeyName;
playList.MediumPictureKeyName = executeContext.Arguments.MediumPictureKeyName;
playList.LargePictureKeyName = executeContext.Arguments.LargePictureKeyName;
playList.OriginalPictureKeyName = Path.GetFileName(executeContext.Arguments.OriginalPictureKeyName);
playList.SmallPictureKeyName = Path.GetFileName(executeContext.Arguments.SmallPictureKeyName);
playList.MediumPictureKeyName = Path.GetFileName(executeContext.Arguments.MediumPictureKeyName);
playList.LargePictureKeyName = Path.GetFileName(executeContext.Arguments.LargePictureKeyName);
database.PlayLists.Update(playList);
await database.SaveChangesAsync(executeContext.CancellationToken);
@@ -51,10 +51,10 @@ namespace Musify.Infrastructure.Messaging.Activities
track.MediumPictureKeyName,
track.LargePictureKeyName);
track.OriginalPictureKeyName = executeContext.Arguments.OriginalPictureKeyName;
track.SmallPictureKeyName = executeContext.Arguments.SmallPictureKeyName;
track.MediumPictureKeyName = executeContext.Arguments.MediumPictureKeyName;
track.LargePictureKeyName = executeContext.Arguments.LargePictureKeyName;
track.OriginalPictureKeyName = Path.GetFileName(executeContext.Arguments.OriginalPictureKeyName);
track.SmallPictureKeyName = Path.GetFileName(executeContext.Arguments.SmallPictureKeyName);
track.MediumPictureKeyName = Path.GetFileName(executeContext.Arguments.MediumPictureKeyName);
track.LargePictureKeyName = Path.GetFileName(executeContext.Arguments.LargePictureKeyName);
database.Tracks.Update(track);
await database.SaveChangesAsync(executeContext.CancellationToken);