From 0b8257a3466968ec8f151de4f0a8774f89f366ea Mon Sep 17 00:00:00 2001 From: XShine555 Date: Fri, 20 Mar 2026 10:43:52 +0100 Subject: [PATCH] Fix job --- .../CreatePlayListCommandHandler.cs | 1 - .../Consumers/ResizePictureConsumer.cs | 35 +++++++++---------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/Application/PlayLists/Commands/CreatePlayList/CreatePlayListCommandHandler.cs b/Application/PlayLists/Commands/CreatePlayList/CreatePlayListCommandHandler.cs index 793fd87..5ba20da 100644 --- a/Application/PlayLists/Commands/CreatePlayList/CreatePlayListCommandHandler.cs +++ b/Application/PlayLists/Commands/CreatePlayList/CreatePlayListCommandHandler.cs @@ -1,6 +1,5 @@ using Ardalis.Result; using DispatchR.Abstractions.Send; -using MassTransit; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using Musify.Application.Configuration; diff --git a/Infrastructure/Messaging/Consumers/ResizePictureConsumer.cs b/Infrastructure/Messaging/Consumers/ResizePictureConsumer.cs index bdbb334..d164b55 100644 --- a/Infrastructure/Messaging/Consumers/ResizePictureConsumer.cs +++ b/Infrastructure/Messaging/Consumers/ResizePictureConsumer.cs @@ -19,30 +19,29 @@ namespace Musify.Infrastructure.Messaging.Consumers try { Dictionary jobsExecutions = new Dictionary(); + var job = await database.Jobs + .SingleOrDefaultAsync(job => job.Id == consumeContext.Message.JobId); + + if (job is null) + { + var payloadJson = JsonSerializer.Serialize(consumeContext.Message.Items); + job = new Job + { + Id = consumeContext.Message.JobId, + JobType = JobType.ResizePicture, + Payload = payloadJson, + }; + await database.Jobs.AddAsync(job, consumeContext.CancellationToken); + await database.SaveChangesAsync(consumeContext.CancellationToken); + } foreach (var item in consumeContext.Message.Items) { - var payloadJson = JsonSerializer.Serialize(item); - - var job = await database.Jobs - .SingleOrDefaultAsync(job => job.Id == consumeContext.Message.JobId); - - if (job is null) - { - job = new Job - { - JobType = JobType.ResizePicture, - Payload = payloadJson, - }; - await database.Jobs.AddAsync(job, consumeContext.CancellationToken); - } - var jobExecution = new JobExecution { JobId = consumeContext.Message.JobId }; await database.JobExecutions.AddAsync(jobExecution, consumeContext.CancellationToken); - jobsExecutions.Add(item, jobExecution); } @@ -56,9 +55,9 @@ namespace Musify.Infrastructure.Messaging.Consumers logger.LogWarning("File not found in storage: BucketName={BucketName}, KeyName={KeyName}", consumeContext.Message.BucketName, consumeContext.Message.KeyName); - foreach (var job in jobsExecutions) + foreach (var jobExecution in jobsExecutions.Values) { - job.Value.JobState = JobState.Failed; + jobExecution.JobState = JobState.Failed; } await database.SaveChangesAsync(consumeContext.CancellationToken); return;