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 GenerateAudioWorkflowPathsActivity( ILogger logger, IProcessTrackingStore processTrackingStore) : IExecuteActivity { public const string ExecuteEndpointName = "generate-audio-workflow-paths"; public async Task Execute(ExecuteContext 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); try { var folderName = Guid.NewGuid().ToString(); var sourceFileName = Path.GetFileName(executeContext.Arguments.SourceKeyName); var workingDirectory = Path.Combine(executeContext.Arguments.TemporaryRootDirectory, folderName); var sourceFilePath = Path.Combine(workingDirectory, sourceFileName); Directory.CreateDirectory(workingDirectory); logger.LogDebug("Generated audio workflow paths. WorkingDirectory: {WorkingDirectory}, SourceFilePath: {SourceFilePath}", workingDirectory, sourceFilePath); await processTrackingStore.CompleteStepAsync(processId, stepId, executeContext.CancellationToken); return executeContext.CompletedWithVariables(new Dictionary { [RoutingSlipVariableNames.Workflow.TemporalDirectory] = workingDirectory, [RoutingSlipVariableNames.Audio.WorkingDirectory] = workingDirectory, [RoutingSlipVariableNames.Audio.SourceFilePath] = sourceFilePath, [RoutingSlipVariableNames.Audio.TranscodedDirectory] = workingDirectory } ); } catch (Exception exception) { logger.LogError(exception, "Error generating audio workflow paths for key {SourceKeyName}", executeContext.Arguments.SourceKeyName); await processTrackingStore.FailStepAsync(processId, stepId, exception.Message, executeContext.CancellationToken); throw; } } } }