Fix (recoverability bug): - TransferFilesToBucketActivity returned Completed() with no log, so its compensation could never delete the transferred files (orphaned objects on fault). IStorageService.TransferFilesAsync now returns the real uploaded keys; the activity logs them so Compensate can remove them. Also drops the Path.Combine key recomputation (produced backslash keys on Windows). Consistency / DRY: - Normalize all courier activities to internal + folder-based namespaces (several were public and in the flat .Activities namespace) - Extract the duplicated exponential retry into UseStandardRetry - Unify endpoint-name logic into a single EndpointHelper (remove the parallel MassTransitEndpointNames helper) Build: 0 warnings, 0 errors.
66 lines
2.6 KiB
C#
66 lines
2.6 KiB
C#
using MassTransit;
|
|
using Microsoft.Extensions.Logging;
|
|
using Musify.Application.Contracts;
|
|
using Musify.Infrastructure.MassTransit.Arguments;
|
|
using Musify.Infrastructure.MassTransit.Logs;
|
|
|
|
namespace Musify.Infrastructure.MassTransit.Activities.Files
|
|
{
|
|
internal class TransferFilesToBucketActivity(
|
|
IStorageService storageHandler,
|
|
ILogger<TransferFilesToBucketActivity> logger)
|
|
: IActivity<TransferFilesToBucketArguments, TransferFilesToBucketLog>
|
|
{
|
|
public const string ExecuteEndpointName = "transfer-files-to-bucket";
|
|
|
|
public async Task<ExecutionResult> Execute(ExecuteContext<TransferFilesToBucketArguments> executeContext)
|
|
{
|
|
var folderPath = executeContext.GetVariable<string>(executeContext.Arguments.SourceDirectoryVariable);
|
|
ArgumentNullException.ThrowIfNull(folderPath, nameof(folderPath));
|
|
var destinationKey = executeContext.Arguments.DestinationKey;
|
|
ArgumentNullException.ThrowIfNull(destinationKey, nameof(destinationKey));
|
|
|
|
try
|
|
{
|
|
var uploadedKeys = await storageHandler.TransferFilesAsync(
|
|
folderPath,
|
|
executeContext.Arguments.DestinationBucket,
|
|
destinationKey,
|
|
executeContext.CancellationToken);
|
|
|
|
return executeContext.Completed(new TransferFilesToBucketLog(
|
|
executeContext.Arguments.DestinationBucket,
|
|
uploadedKeys.ToArray()));
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
logger.LogError(exception, "Failed to transfer files from {FolderPath} to {DestinationBucket}",
|
|
folderPath,
|
|
executeContext.Arguments.DestinationBucket);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<CompensationResult> Compensate(CompensateContext<TransferFilesToBucketLog> compensateContext)
|
|
{
|
|
try
|
|
{
|
|
foreach (var uploadedKey in compensateContext.Log.UploadedKeys)
|
|
{
|
|
await storageHandler.RemoveFileAsync(
|
|
compensateContext.Log.DestinationBucket,
|
|
uploadedKey,
|
|
compensateContext.CancellationToken);
|
|
}
|
|
|
|
return compensateContext.Compensated();
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
logger.LogError(exception, "Failed to compensate file transfers to {DestinationBucket}", compensateContext.Log.DestinationBucket);
|
|
return compensateContext.Failed(exception);
|
|
}
|
|
}
|
|
}
|
|
}
|