Simplify infrastructure services and move all recurring jobs to Hangfire

This commit is contained in:
2026-09-24 12:18:57 +02:00
parent 0fa033faff
commit 0cba32f776
24 changed files with 361 additions and 413 deletions
@@ -7,53 +7,41 @@ using Musify.Infrastructure.Configuration;
namespace Musify.Infrastructure.Services;
public partial class AudioService(ILogger<AudioService> logger,
public sealed partial class AudioTranscoderService(
ILogger<AudioTranscoderService> logger,
AudioConfiguration audioConfiguration)
: IAudioTranscoderService
{
private record FfmpegExecutionResult(int ExitCode, string StandardOutput, string StandardError);
private sealed record FfmpegExecutionResult(int ExitCode, string StandardOutput, string StandardError);
[GeneratedRegex(@"Duration:\s*(\d+):(\d{2}):(\d{2}(?:\.\d+)?)", RegexOptions.IgnoreCase)]
private static partial Regex DurationRegex();
public async Task<AudioTranscodeResult> TranscodeToAudioFileAsync(Stream audioStream, string destinationPath, CancellationToken cancellationToken)
{
try
Directory.CreateDirectory(destinationPath);
logger.LogInformation("Starting audio transcoding for {DestinationPath}", destinationPath);
var executionResult = await ExecuteFfmpegAsync(
BuildAudioFileArguments(destinationPath, audioConfiguration.Ffmpeg),
destinationPath,
audioStream,
"TranscodeToAudioFile",
cancellationToken);
if (executionResult.ExitCode != 0)
{
Directory.CreateDirectory(destinationPath);
logger.LogInformation("Starting audio transcoding for {DestinationPath}", destinationPath);
var executionResult = await ExecuteFfmpegWithInputAsync(
BuildAudioFileArguments(destinationPath, audioConfiguration.Ffmpeg),
destinationPath,
audioStream,
"TranscodeToAudioFile",
cancellationToken);
if (executionResult.ExitCode != 0)
{
logger.LogError("Audio transcoding failed with exit code {ExitCode}. Output: {StandardOutput}, Error: {StandardError}",
executionResult.ExitCode, executionResult.StandardOutput, executionResult.StandardError);
return new AudioTranscodeResult(executionResult.ExitCode, TimeSpan.Zero);
}
logger.LogInformation("Audio transcoding completed for {DestinationPath}", destinationPath);
var outputPath = Path.Combine(destinationPath, audioConfiguration.Ffmpeg.OutputFileName);
var duration = await GetAudioDurationAsync(outputPath, cancellationToken);
return new AudioTranscodeResult(executionResult.ExitCode, duration);
}
catch (TimeoutException timeoutException)
{
logger.LogError(timeoutException, "Audio transcoding timed out after {Timeout} for {DestinationPath}", audioConfiguration.TranscodingTimeout, destinationPath);
throw;
}
catch (Exception exception)
{
logger.LogError(exception, "Failed to transcode audio for {DestinationPath}", destinationPath);
throw;
logger.LogError("Audio transcoding failed with exit code {ExitCode}. Output: {StandardOutput}, Error: {StandardError}",
executionResult.ExitCode, executionResult.StandardOutput, executionResult.StandardError);
return new AudioTranscodeResult(executionResult.ExitCode, TimeSpan.Zero);
}
logger.LogInformation("Audio transcoding completed for {DestinationPath}", destinationPath);
var outputPath = Path.Combine(destinationPath, audioConfiguration.Ffmpeg.OutputFileName);
var duration = await GetAudioDurationAsync(outputPath, cancellationToken);
return new AudioTranscodeResult(executionResult.ExitCode, duration);
}
private async Task<TimeSpan> GetAudioDurationAsync(string filePath, CancellationToken cancellationToken)
@@ -61,6 +49,7 @@ public partial class AudioService(ILogger<AudioService> logger,
var executionResult = await ExecuteFfmpegAsync(
BuildProbeDurationArguments(filePath),
Path.GetDirectoryName(filePath) ?? Environment.CurrentDirectory,
input: null,
"ProbeAudioDuration",
cancellationToken);
@@ -75,37 +64,14 @@ public partial class AudioService(ILogger<AudioService> logger,
return new TimeSpan(hours, minutes, 0) + TimeSpan.FromSeconds(seconds);
}
private async Task<FfmpegExecutionResult> ExecuteFfmpegWithInputAsync(
string arguments,
string workingDirectory,
Stream inputStream,
string operationName,
CancellationToken cancellationToken)
{
using var process = BuildProcess(arguments, workingDirectory, redirectStandardInput: true);
process.Start();
logger.LogDebug("[{Operation}] ffmpeg started (PID: {ProcessId}) in {WorkingDirectory}", operationName, process.Id, workingDirectory);
var outputTask = process.StandardOutput.ReadToEndAsync(cancellationToken);
var errorTask = process.StandardError.ReadToEndAsync(cancellationToken);
if (inputStream.CanSeek)
inputStream.Position = 0;
await inputStream.CopyToAsync(process.StandardInput.BaseStream, cancellationToken);
process.StandardInput.Close();
return await WaitForProcessResultAsync(process, outputTask, errorTask, operationName, cancellationToken);
}
private async Task<FfmpegExecutionResult> ExecuteFfmpegAsync(
string arguments,
string workingDirectory,
Stream? input,
string operationName,
CancellationToken cancellationToken)
{
using var process = BuildProcess(arguments, workingDirectory, redirectStandardInput: false);
using var process = BuildProcess(arguments, workingDirectory, redirectStandardInput: input != null);
process.Start();
logger.LogDebug("[{Operation}] ffmpeg started (PID: {ProcessId}) in {WorkingDirectory}", operationName, process.Id, workingDirectory);
@@ -113,6 +79,15 @@ public partial class AudioService(ILogger<AudioService> logger,
var outputTask = process.StandardOutput.ReadToEndAsync(cancellationToken);
var errorTask = process.StandardError.ReadToEndAsync(cancellationToken);
if (input != null)
{
if (input.CanSeek)
input.Position = 0;
await input.CopyToAsync(process.StandardInput.BaseStream, cancellationToken);
process.StandardInput.Close();
}
return await WaitForProcessResultAsync(process, outputTask, errorTask, operationName, cancellationToken);
}
@@ -148,11 +123,8 @@ public partial class AudioService(ILogger<AudioService> logger,
}
catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested)
{
logger.LogError("[{Operation}] ffmpeg timed out after {Timeout} (PID: {ProcessId})",
operationName, audioConfiguration.TranscodingTimeout, process.Id);
TryKillProcess(process);
throw new TimeoutException($"ffmpeg execution exceeded timeout {audioConfiguration.TranscodingTimeout}.");
throw new TimeoutException($"ffmpeg execution exceeded timeout {audioConfiguration.TranscodingTimeout} (PID: {process.Id}).");
}
var standardError = await errorTask;
@@ -171,9 +143,9 @@ public partial class AudioService(ILogger<AudioService> logger,
if (!process.HasExited)
process.Kill();
}
catch (Exception exception)
catch (InvalidOperationException exception)
{
logger.LogError(exception, "Failed to kill ffmpeg process (PID: {ProcessId})", process.Id);
logger.LogWarning(exception, "Could not kill ffmpeg process (PID: {ProcessId}); it had already exited", process.Id);
}
}
@@ -10,30 +10,22 @@ public class PictureService(ILogger<PictureService> logger)
{
public async Task<Stream> ResizePictureAsWebpAsync(Stream pictureStream, int width, int height, CancellationToken cancellationToken)
{
try
logger.LogDebug("Resizing picture to {Width}x{Height}", width, height);
if (pictureStream.CanSeek && pictureStream.Position > 0)
pictureStream.Position = 0;
using var picture = await Image.LoadAsync(pictureStream, cancellationToken);
picture.Mutate(options => options.Resize(new ResizeOptions
{
logger.LogDebug("Resizing picture to {Width}x{Height}", width, height);
if (pictureStream.CanSeek && pictureStream.Position > 0)
pictureStream.Position = 0;
Size = new Size(width, height),
Mode = ResizeMode.Max
}));
using var picture = await Image.LoadAsync(pictureStream, cancellationToken);
picture.Mutate(options => options.Resize(new ResizeOptions
{
Size = new Size(width, height),
Mode = ResizeMode.Max
}));
var memoryStream = new MemoryStream();
await picture.SaveAsWebpAsync(memoryStream, cancellationToken);
memoryStream.Position = 0;
var memoryStream = new MemoryStream();
await picture.SaveAsWebpAsync(memoryStream, cancellationToken);
memoryStream.Position = 0;
logger.LogDebug("Picture resized to {Width}x{Height}, length: {Length}", width, height, memoryStream.Length);
return memoryStream;
}
catch (Exception exception)
{
logger.LogError(exception, "Failed to resize picture to {Width}x{Height}", width, height);
throw;
}
logger.LogDebug("Picture resized to {Width}x{Height}, length: {Length}", width, height, memoryStream.Length);
return memoryStream;
}
}
@@ -28,7 +28,7 @@ public static class ServicesDependencyInjection
return new AmazonS3Client(configuration.AccessKey, configuration.SecretAccessKey, s3Configuration);
});
services.AddScoped<IStorageService, StorageService>();
services.AddSingleton<IStorageService, StorageService>();
return services;
}
@@ -45,17 +45,7 @@ public static class ServicesDependencyInjection
return services;
}
public static IServiceCollection AddUploadIntentJobs(this IServiceCollection services, IConfiguration configuration)
{
services.AddUploadIntentConfiguration(configuration);
services.AddHostedService<UploadIntentExpirationJob>();
services.AddHostedService<TemporalUploadsCleanUpJob>();
return services;
}
public static IServiceCollection AddDailyMixGenerationJob(this IServiceCollection services, IConfiguration configuration)
public static IServiceCollection AddJobs(this IServiceCollection services)
{
services.AddHangfire((serviceProvider, hangfireConfiguration) =>
{
@@ -67,6 +57,9 @@ public static class ServicesDependencyInjection
services.AddHangfireServer();
services.AddScoped<DailyMixGenerationJob>();
services.AddScoped<ListeningHistoryCleanupJob>();
services.AddScoped<UploadIntentExpirationJob>();
services.AddScoped<TempUploadsCleanupJob>();
services.AddHostedService<RecurringJobsRegistrar>();
return services;
}
@@ -83,7 +76,7 @@ public static class ServicesDependencyInjection
{
services.AddValidatedOptions<AudioConfiguration>(configuration);
services.AddScoped<IAudioTranscoderService, AudioService>();
services.AddScoped<IAudioTranscoderService, AudioTranscoderService>();
return services;
}
}
@@ -1,29 +1,27 @@
using System.Runtime.CompilerServices;
using Amazon.S3;
using Amazon.S3.Model;
using Amazon.S3.Transfer;
using Microsoft.Extensions.Logging;
using MimeMapping;
using Musify.Application.Contracts;
using Musify.Application.Shared;
using Musify.Infrastructure.Configuration;
using ObjectMetaData = Musify.Application.Contracts.ObjectMetaData;
namespace Musify.Infrastructure.Services;
public class StorageService(IAmazonS3 amazonS3, ILogger<StorageService> logger,
public sealed class StorageService(
IAmazonS3 amazonS3,
ILogger<StorageService> logger,
InfrastructureStorageConfiguration storageClientConfiguration)
: IStorageService
{
public async Task<Stream?> GetFileAsync(string bucket, string key, CancellationToken cancellationToken)
{
var request = new GetObjectRequest
{
BucketName = bucket,
Key = key,
};
try
{
var response = await amazonS3.GetObjectAsync(request, cancellationToken);
var response = await amazonS3.GetObjectAsync(
new GetObjectRequest { BucketName = bucket, Key = key }, cancellationToken);
logger.LogDebug("Retrieved file from S3 {Bucket}/{Key}", bucket, key);
return response.ResponseStream;
}
@@ -32,11 +30,6 @@ public class StorageService(IAmazonS3 amazonS3, ILogger<StorageService> logger,
logger.LogDebug("File not found in S3 {Bucket}/{Key}", bucket, key);
return null;
}
catch (Exception exception)
{
logger.LogError(exception, "Failed to get file from S3 {Bucket}/{Key}", bucket, key);
throw;
}
}
public async Task<string> GetUploadUrlAsync(
@@ -44,8 +37,8 @@ public class StorageService(IAmazonS3 amazonS3, ILogger<StorageService> logger,
string key,
string contentType,
TimeSpan expirationTime,
CancellationToken cancellationToken,
bool preventOverwrite = true)
bool preventOverwrite = true,
CancellationToken cancellationToken = default)
{
var request = new GetPreSignedUrlRequest
{
@@ -71,140 +64,81 @@ public class StorageService(IAmazonS3 amazonS3, ILogger<StorageService> logger,
var response = await amazonS3.GetObjectMetadataAsync(bucket, key, cancellationToken);
return new ObjectMetaData(response.Headers.ContentType, response.ContentLength);
}
catch (AmazonS3Exception ex) when (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
catch (AmazonS3Exception exception) when (exception.StatusCode == System.Net.HttpStatusCode.NotFound)
{
return null;
}
catch (Exception exception)
{
logger.LogError(exception, "HEAD failed for {Bucket}/{Key}", bucket, key);
throw;
}
}
public async Task RemoveFileAsync(string bucket, string key, CancellationToken cancellationToken)
{
var request = new DeleteObjectRequest
{
BucketName = bucket,
Key = key,
};
logger.LogDebug("Removing file from S3 {Bucket}/{Key}", bucket, key);
await amazonS3.DeleteObjectAsync(request, cancellationToken);
await amazonS3.DeleteObjectAsync(new DeleteObjectRequest { BucketName = bucket, Key = key }, cancellationToken);
}
public async Task<IReadOnlyList<string>> TransferFilesAsync(string sourceDirectory, string bucket, string route, CancellationToken cancellationToken)
public async Task<IReadOnlyList<string>> TransferFilesAsync(
string sourceDirectory, string bucket, string route, CancellationToken cancellationToken)
{
var trasnsferUtility = new TransferUtility(amazonS3);
var files = Directory.GetFiles(sourceDirectory, "*");
using var transferUtility = new TransferUtility(amazonS3);
var uploadedKeys = new List<string>();
var successCount = 0;
var failedCount = 0;
foreach (var file in files)
foreach (var filePath in Directory.GetFiles(sourceDirectory, "*"))
{
string fileName = Path.GetFileName(file);
string filePath = Path.Combine(sourceDirectory, file);
var key = StorageKey.Combine(route, Path.GetFileName(filePath));
var key = string.Join('/', new[] { route, fileName }
.Where(static s => !string.IsNullOrWhiteSpace(s))
.Select(static s => s.Trim().Trim('/', '\\')));
var contentType = MimeUtility.GetMimeMapping(filePath);
try
{
await trasnsferUtility.UploadAsync(
new TransferUtilityUploadRequest
{
FilePath = filePath,
BucketName = bucket,
Key = key,
ContentType = contentType
},
cancellationToken);
uploadedKeys.Add(key);
successCount++;
logger.LogDebug("Transferred file to S3 {Bucket}/{Key}", bucket, key);
}
catch (Exception exception)
{
failedCount++;
logger.LogError(exception, "Failed to transfer file to S3 {Bucket}/{Key}", bucket, key);
throw;
}
await transferUtility.UploadAsync(
new TransferUtilityUploadRequest
{
FilePath = filePath,
BucketName = bucket,
Key = key,
ContentType = MimeUtility.GetMimeMapping(filePath)
},
cancellationToken);
uploadedKeys.Add(key);
logger.LogDebug("Transferred file to S3 {Bucket}/{Key}", bucket, key);
}
logger.LogInformation("File transfer completed to {Bucket}/{Route}. Success: {SuccessCount}, Failed: {FailedCount}", bucket, route, successCount, failedCount);
logger.LogInformation("Transferred {Count} files to {Bucket}/{Route}", uploadedKeys.Count, bucket, route);
return uploadedKeys;
}
public async Task UploadFileAsync(string filePath, string bucket, string key, CancellationToken cancellationToken)
{
try
{
using var fileStream = File.OpenRead(filePath);
var contentType = MimeUtility.GetMimeMapping(filePath);
await UploadFileAsync(fileStream, contentType, bucket, key, cancellationToken);
}
catch (Exception exception)
{
logger.LogError(exception, "Failed to upload file to S3 {Bucket}/{Key}", bucket, key);
throw;
}
await using var fileStream = File.OpenRead(filePath);
await UploadFileAsync(fileStream, MimeUtility.GetMimeMapping(filePath), bucket, key, cancellationToken);
}
public async Task UploadFileAsync(Stream sourceStream, string contentType, string bucket, string key, CancellationToken cancellationToken)
{
var request = new PutObjectRequest
{
BucketName = bucket,
ContentType = contentType,
InputStream = sourceStream,
Key = key
};
try
{
await amazonS3.PutObjectAsync(request, cancellationToken);
logger.LogDebug("Uploaded file to S3 {Bucket}/{Key}", bucket, key);
}
catch (Exception exception)
{
logger.LogError(exception, "Failed to upload file to S3 {Bucket}/{Key}", bucket, key);
throw;
}
await amazonS3.PutObjectAsync(
new PutObjectRequest { BucketName = bucket, ContentType = contentType, InputStream = sourceStream, Key = key },
cancellationToken);
logger.LogDebug("Uploaded file to S3 {Bucket}/{Key}", bucket, key);
}
public async Task CopyFileAsync(string sourceBucket, string sourceKey, string destinationBucket, string destinationKey,
CancellationToken cancellationToken)
public async Task CopyFileAsync(
string sourceBucket, string sourceKey, string destinationBucket, string destinationKey, CancellationToken cancellationToken)
{
var request = new CopyObjectRequest
{
SourceBucket = sourceBucket,
SourceKey = sourceKey,
DestinationBucket = destinationBucket,
DestinationKey = destinationKey
};
await amazonS3.CopyObjectAsync(
new CopyObjectRequest
{
SourceBucket = sourceBucket,
SourceKey = sourceKey,
DestinationBucket = destinationBucket,
DestinationKey = destinationKey
},
cancellationToken);
try
{
await amazonS3.CopyObjectAsync(request, cancellationToken);
logger.LogInformation("Copied file from {SourceBucket}/{SourceKey} to {DestinationBucket}/{DestinationKey}",
sourceBucket, sourceKey, destinationBucket, destinationKey);
}
catch (Exception exception)
{
logger.LogError(exception, "Failed to copy file from {SourceBucket}/{SourceKey} to {DestinationBucket}/{DestinationKey}",
sourceBucket, sourceKey, destinationBucket, destinationKey);
throw;
}
logger.LogDebug("Copied file from {SourceBucket}/{SourceKey} to {DestinationBucket}/{DestinationKey}",
sourceBucket, sourceKey, destinationBucket, destinationKey);
}
public async IAsyncEnumerable<StorageObject> ListObjectsAsync(
string bucket, string prefix, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken)
string bucket, string prefix, [EnumeratorCancellation] CancellationToken cancellationToken)
{
var listRequest = new ListObjectsV2Request
{
@@ -221,6 +155,6 @@ public class StorageService(IAmazonS3 amazonS3, ILogger<StorageService> logger,
listRequest.ContinuationToken = listResponse.NextContinuationToken;
}
while (listResponse.IsTruncated.HasValue && listResponse.IsTruncated.Value);
while (listResponse.IsTruncated == true);
}
}