using MassTransit; using Microsoft.Extensions.Logging; using Musify.Application.Contracts.Infrastructure; using Musify.Infrastructure.MassTransit.Arguments; using Musify.Infrastructure.MassTransit.Logs; namespace Musify.Infrastructure.MassTransit.Activities.Files { internal class CopyFileInBucketActivity( IStorageService storageHandler, ILogger logger) : IActivity { public const string ExecuteEndpointName = "copy-file-in-bucket"; public async Task Execute(ExecuteContext executeContext) { try { await storageHandler.CopyFileAsync( executeContext.Arguments.SourceBucket, executeContext.Arguments.SourceKey, executeContext.Arguments.DestinationBucket, executeContext.Arguments.DestinationKey, executeContext.CancellationToken); return executeContext.Completed(new CopyFileInBucketLog( executeContext.Arguments.DestinationBucket, executeContext.Arguments.DestinationKey)); } catch (Exception exception) { logger.LogError(exception, "Failed to copy {SourceBucket}/{SourceKey} to {DestinationBucket}/{DestinationKey}", executeContext.Arguments.SourceBucket, executeContext.Arguments.SourceKey, executeContext.Arguments.DestinationBucket, executeContext.Arguments.DestinationKey); throw; } } public async Task Compensate(CompensateContext compensateContext) { try { await storageHandler.RemoveFileAsync( compensateContext.Log.DestinationBucket, compensateContext.Log.DestinationKey, compensateContext.CancellationToken); return compensateContext.Compensated(); } catch (Exception exception) { logger.LogError(exception, "Failed to compensate copied file {Bucket}/{Key}", compensateContext.Log.DestinationBucket, compensateContext.Log.DestinationKey); return compensateContext.Failed(exception); } } } }