Remove files consumer

This commit is contained in:
2026-03-19 01:20:05 +01:00
parent 34c213ef5a
commit eb9bac6cf9
5 changed files with 78 additions and 13 deletions
@@ -1,14 +1,50 @@
using MassTransit;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Musify.Application.Contracts.Infrastructure;
using Musify.Application.Events;
using Musify.Domain.Entities;
using System.Text.Json;
namespace Musify.Infrastructure.Messaging.Consumers
{
public class RemoveFileConsumer
public class RemoveFileConsumer(IDatabase database, IStorageHandler storageHandler, ILogger<RemoveFileConsumer> logger)
: IConsumer<RemoveFileEvent>
{
public Task Consume(ConsumeContext<RemoveFileEvent> context)
public const string QueueName = "file-remove-queue";
public async Task Consume(ConsumeContext<RemoveFileEvent> consumeContext)
{
throw new NotImplementedException();
var payload = JsonSerializer.Serialize(consumeContext.Message);
var job = await database.Jobs
.SingleOrDefaultAsync(j => j.JobState == JobState.Failed && j.Payload == payload);
if (job is null)
{
job = new Job
{
JobType = JobType.RemoveFile,
Payload = payload
};
await database.Jobs.AddAsync(job, consumeContext.CancellationToken);
}
else
{
job.RetryCount++;
job.JobState = JobState.Pending;
database.Jobs.Update(job);
}
var removeFile = await storageHandler.RemoveFileAsync(
consumeContext.Message.BucketName,
consumeContext.Message.KeyName,
consumeContext.CancellationToken);
if (!removeFile.IsSuccess)
job.JobState = JobState.Failed;
await database.SaveChangesAsync(consumeContext.CancellationToken);
}
}
}
@@ -38,6 +38,7 @@ namespace Musify.Infrastructure.Messaging
serviceDescriptors.AddMassTransit(options =>
{
options.AddConsumer<ResizePictureConsumer>();
options.AddConsumer<RemoveFileConsumer>();
options.UsingRabbitMq((busRegistrationContext, busFactoryConfigurator) =>
{
@@ -62,6 +63,10 @@ namespace Musify.Infrastructure.Messaging
{
endpointConfigurator.ConfigureConsumer<ResizePictureConsumer>(busRegistrationContext);
} );
busFactoryConfigurator.ReceiveEndpoint(RemoveFileConsumer.QueueName, endpointConfigurator =>
{
endpointConfigurator.ConfigureConsumer<RemoveFileConsumer>(busRegistrationContext);
} );
} );
} );
}