diff --git a/Application/Contracts/Infrastructure/IDatabase.cs b/Application/Contracts/Infrastructure/IDatabase.cs index 16534e2..5b3cdf2 100644 --- a/Application/Contracts/Infrastructure/IDatabase.cs +++ b/Application/Contracts/Infrastructure/IDatabase.cs @@ -17,7 +17,7 @@ namespace Musify.Application.Contracts.Infrastructure DbSet Jobs { get; } - DbSet JobExecutions { get; } + DbSet JobOperations { get; } Task SaveChangesAsync(CancellationToken cancellationToken); } diff --git a/Domain/Entities/JobExecution.cs b/Domain/Entities/JobOperation.cs similarity index 96% rename from Domain/Entities/JobExecution.cs rename to Domain/Entities/JobOperation.cs index ad20805..096ea08 100644 --- a/Domain/Entities/JobExecution.cs +++ b/Domain/Entities/JobOperation.cs @@ -5,7 +5,7 @@ namespace Musify.Domain.Entities { #pragma warning disable CS8618 [Table("JobExecution")] - public class JobExecution + public class JobOperation { [Key] public Guid Id { get; set; } = Guid.NewGuid(); diff --git a/Infrastructure/Messaging/Consumers/RemoveFileConsumer.cs b/Infrastructure/Messaging/Consumers/RemoveFileConsumer.cs index ed0741a..1f71d37 100644 --- a/Infrastructure/Messaging/Consumers/RemoveFileConsumer.cs +++ b/Infrastructure/Messaging/Consumers/RemoveFileConsumer.cs @@ -31,11 +31,11 @@ namespace Musify.Infrastructure.Messaging.Consumers logger.LogInformation("Created new job with id {JobId} for removing file {BucketName}/{KeyName}", job.Id, consumeContext.Message.BucketName, consumeContext.Message.KeyName); } - var jobExecution = new JobExecution + var jobExecution = new JobOperation { JobId = job.Id }; - await database.JobExecutions.AddAsync(jobExecution, consumeContext.CancellationToken); + await database.JobOperations.AddAsync(jobExecution, consumeContext.CancellationToken); var removeFile = await storageHandler.RemoveFileAsync( consumeContext.Message.BucketName, diff --git a/Infrastructure/Messaging/Consumers/ResizePictureConsumer.cs b/Infrastructure/Messaging/Consumers/ResizePictureConsumer.cs index da3f850..f7a2619 100644 --- a/Infrastructure/Messaging/Consumers/ResizePictureConsumer.cs +++ b/Infrastructure/Messaging/Consumers/ResizePictureConsumer.cs @@ -12,7 +12,7 @@ namespace Musify.Infrastructure.Messaging.Consumers public class ResizePictureConsumer(IStorageHandler storageHandler, IPictureHandler pictureHandler, IDatabase database, ILogger logger) : IConsumer { - private record JobExecutionItem(ResizePictureItems Item, JobExecution JobExecution); + private record JobOperationItem(ResizePictureItems Item, JobOperation JobOperation); public const string QueueName = "picture-resize-queue"; @@ -24,24 +24,24 @@ namespace Musify.Infrastructure.Messaging.Consumers var cancellationToken = consumeContext.CancellationToken; var job = await GetOrCreateJobAsync(message, cancellationToken); - var jobExecutions = await CreateJobExecutionsAsync(message.Items, job.Id, cancellationToken); + var jobOperations = await CreateJobOperationAsync(message.Items, job.Id, cancellationToken); await database.SaveChangesAsync(cancellationToken); var sourceImageResult = await GetSourceImageAsync(message.BucketName, message.KeyName, cancellationToken); if (!sourceImageResult.IsSuccess) { - MarkAllAsFailed(jobExecutions); + MarkAllAsFailed(jobOperations); await database.SaveChangesAsync(cancellationToken); return; } using var sourceImage = sourceImageResult.Value; - foreach (var executionItem in jobExecutions) + foreach (var operationItem in jobOperations) { - await ProcessExecutionAsync( - executionItem, + await ProcessOperationAsync( + operationItem, message.BucketName, sourceImage, cancellationToken); @@ -78,25 +78,25 @@ namespace Musify.Infrastructure.Messaging.Consumers return job; } - private async Task> CreateJobExecutionsAsync( - IReadOnlyCollection items, + private async Task> CreateJobOperationAsync( + IReadOnlyCollection resizeItems, Guid jobId, CancellationToken cancellationToken) { - var jobExecutions = new List(items.Count); + var jobOperations = new List(resizeItems.Count); - foreach (var item in items) + foreach (var resizeItem in resizeItems) { - var jobExecution = new JobExecution + var jobOperation = new JobOperation { JobId = jobId }; - await database.JobExecutions.AddAsync(jobExecution, cancellationToken); - jobExecutions.Add(new JobExecutionItem(item, jobExecution)); + await database.JobOperations.AddAsync(jobOperation, cancellationToken); + jobOperations.Add(new JobOperationItem(resizeItem, jobOperation)); } - return jobExecutions; + return jobOperations; } private async Task> GetSourceImageAsync(string bucketName, string keyName, CancellationToken cancellationToken) @@ -126,32 +126,32 @@ namespace Musify.Infrastructure.Messaging.Consumers return Result.Success(memoryStream); } - private async Task ProcessExecutionAsync( - JobExecutionItem executionItem, + private async Task ProcessOperationAsync( + JobOperationItem operationItem, string bucketName, MemoryStream sourceImage, CancellationToken cancellationToken) { - var jobExecution = executionItem.JobExecution; + var jobOperation = operationItem.JobOperation; try { - jobExecution.JobState = JobState.InProgress; + jobOperation.JobState = JobState.InProgress; sourceImage.Position = 0; var resizedPictureResult = await pictureHandler.ResizePictureAsync( sourceImage, - executionItem.Item.Width, - executionItem.Item.Height, + operationItem.Item.Width, + operationItem.Item.Height, cancellationToken); if (!resizedPictureResult.IsSuccess) { - logger.LogError("Failed to resize picture for JobExecutionId={JobExecutionId}. Error: {ErrorMessage}", - jobExecution.Id, + logger.LogError("Failed to resize picture for JobOperationId={JobOperationId}. Error: {ErrorMessage}", + jobOperation.Id, string.Join("; ", resizedPictureResult.Errors)); - MarkAsFailed(jobExecution); + MarkAsFailed(jobOperation); return; } @@ -159,47 +159,47 @@ namespace Musify.Infrastructure.Messaging.Consumers resizedPictureResult.Value, "image/webp", bucketName, - $"{executionItem.Item.SaveOnRoute}/{Guid.NewGuid()}.webp", + $"{operationItem.Item.SaveOnRoute}/{Guid.NewGuid()}.webp", cancellationToken); if (!saveResult.IsSuccess) { - logger.LogError("Failed to upload resized picture for JobExecutionId={JobExecutionId}. Error: {ErrorMessage}", - jobExecution.Id, + logger.LogError("Failed to upload resized picture for JobOperationId={JobOperationId}. Error: {ErrorMessage}", + jobOperation.Id, string.Join("; ", saveResult.Errors)); - MarkAsFailed(jobExecution); + MarkAsFailed(jobOperation); return; } - jobExecution.JobState = JobState.Completed; - jobExecution.FinishedAt = DateTime.UtcNow; + jobOperation.JobState = JobState.Completed; + jobOperation.FinishedAt = DateTime.UtcNow; } catch (Exception exception) { logger.LogError(exception, - "An error occurred while processing resize for JobExecutionId={JobExecutionId}, Width={Width}, Height={Height}, SaveOnRoute={SaveOnRoute}", - jobExecution.Id, - executionItem.Item.Width, - executionItem.Item.Height, - executionItem.Item.SaveOnRoute); + "An error occurred while processing resize for JobOperationId={JobOperationId}, Width={Width}, Height={Height}, SaveOnRoute={SaveOnRoute}", + jobOperation.Id, + operationItem.Item.Width, + operationItem.Item.Height, + operationItem.Item.SaveOnRoute); - MarkAsFailed(jobExecution); + MarkAsFailed(jobOperation); } } - private static void MarkAllAsFailed(IEnumerable jobExecutions) + private static void MarkAllAsFailed(IEnumerable jobOperations) { - foreach (var executionItem in jobExecutions) + foreach (var operationItem in jobOperations) { - MarkAsFailed(executionItem.JobExecution); + MarkAsFailed(operationItem.JobOperation); } } - private static void MarkAsFailed(JobExecution jobExecution) + private static void MarkAsFailed(JobOperation jobOperation) { - jobExecution.JobState = JobState.Failed; - jobExecution.FinishedAt = DateTime.UtcNow; + jobOperation.JobState = JobState.Failed; + jobOperation.FinishedAt = DateTime.UtcNow; } } } \ No newline at end of file diff --git a/Infrastructure/Persistence/Database.cs b/Infrastructure/Persistence/Database.cs index be2c9d1..4dd8f61 100644 --- a/Infrastructure/Persistence/Database.cs +++ b/Infrastructure/Persistence/Database.cs @@ -25,6 +25,6 @@ namespace Musify.Infrastructure.Persistence public DbSet Jobs => Set(); - public DbSet JobExecutions => Set(); + public DbSet JobOperations => Set(); } } \ No newline at end of file