log queue errors

This commit is contained in:
Alex Bezdieniezhnykh
2025-05-17 19:38:07 +03:00
parent dae342b70e
commit cf563571c8
+43 -32
View File
@@ -13,6 +13,7 @@ using LinqToDB;
using LinqToDB.Data; using LinqToDB.Data;
using MediatR; using MediatR;
using MessagePack; using MessagePack;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Newtonsoft.Json; using Newtonsoft.Json;
using RabbitMQ.Stream.Client; using RabbitMQ.Stream.Client;
@@ -27,6 +28,7 @@ public class AnnotationService : IAnnotationService, INotificationHandler<Annota
private readonly IGalleryService _galleryService; private readonly IGalleryService _galleryService;
private readonly IMediator _mediator; private readonly IMediator _mediator;
private readonly IAzaionApi _api; private readonly IAzaionApi _api;
private readonly ILogger<AnnotationService> _logger;
private readonly QueueConfig _queueConfig; private readonly QueueConfig _queueConfig;
private Consumer _consumer = null!; private Consumer _consumer = null!;
private readonly UIConfig _uiConfig; private readonly UIConfig _uiConfig;
@@ -42,13 +44,15 @@ public class AnnotationService : IAnnotationService, INotificationHandler<Annota
IOptions<DirectoriesConfig> directoriesConfig, IOptions<DirectoriesConfig> directoriesConfig,
IGalleryService galleryService, IGalleryService galleryService,
IMediator mediator, IMediator mediator,
IAzaionApi api) IAzaionApi api,
ILogger<AnnotationService> logger)
{ {
_dbFactory = dbFactory; _dbFactory = dbFactory;
_producer = producer; _producer = producer;
_galleryService = galleryService; _galleryService = galleryService;
_mediator = mediator; _mediator = mediator;
_api = api; _api = api;
_logger = logger;
_queueConfig = queueConfig.Value; _queueConfig = queueConfig.Value;
_uiConfig = uiConfig.Value; _uiConfig = uiConfig.Value;
_dirConfig = directoriesConfig.Value; _dirConfig = directoriesConfig.Value;
@@ -76,40 +80,47 @@ public class AnnotationService : IAnnotationService, INotificationHandler<Annota
OffsetSpec = new OffsetTypeOffset(offsets.AnnotationsOffset + 1), OffsetSpec = new OffsetTypeOffset(offsets.AnnotationsOffset + 1),
MessageHandler = async (_, _, context, message) => MessageHandler = async (_, _, context, message) =>
{ {
var email = (string)message.ApplicationProperties[nameof(User.Email)]!; try
if (email == _api.CurrentUser.Email) //Don't process messages by yourself
return;
var annotationStatus = (AnnotationStatus)message.ApplicationProperties[nameof(AnnotationStatus)];
if (annotationStatus.In(AnnotationStatus.Created, AnnotationStatus.ValidatedEdited))
{ {
var msg = MessagePackSerializer.Deserialize<AnnotationMessage>(message.Data.Contents); var email = (string)message.ApplicationProperties[nameof(User.Email)]!;
await SaveAnnotationInner( if (email == _api.CurrentUser.Email) //Don't process messages by yourself
msg.CreatedDate, return;
msg.OriginalMediaName, var annotationStatus = (AnnotationStatus)message.ApplicationProperties[nameof(AnnotationStatus)];
msg.Time, if (annotationStatus.In(AnnotationStatus.Created, AnnotationStatus.ValidatedEdited))
JsonConvert.DeserializeObject<List<Detection>>(msg.Detections) ?? [], {
msg.Source, var msg = MessagePackSerializer.Deserialize<AnnotationMessage>(message.Data.Contents);
new MemoryStream(msg.Image), await SaveAnnotationInner(
msg.Role, msg.CreatedDate,
msg.Email, msg.OriginalMediaName,
fromQueue: true, msg.Time,
token: cancellationToken); JsonConvert.DeserializeObject<List<Detection>>(msg.Detections) ?? [],
} msg.Source,
else new MemoryStream(msg.Image),
{ msg.Role,
var msg = MessagePackSerializer.Deserialize<AnnotationBulkMessage>(message.Data.Contents); msg.Email,
if (annotationStatus == AnnotationStatus.Validated) fromQueue: true,
await ValidateAnnotations(msg.AnnotationNames.ToList(), true, cancellationToken); token: cancellationToken);
if (annotationStatus == AnnotationStatus.Deleted) }
await _mediator.Publish(new AnnotationsDeletedEvent(msg.AnnotationNames.ToList(), fromQueue:true), cancellationToken); else
} {
var msg = MessagePackSerializer.Deserialize<AnnotationBulkMessage>(message.Data.Contents);
if (annotationStatus == AnnotationStatus.Validated)
await ValidateAnnotations(msg.AnnotationNames.ToList(), true, cancellationToken);
if (annotationStatus == AnnotationStatus.Deleted)
await _mediator.Publish(new AnnotationsDeletedEvent(msg.AnnotationNames.ToList(), fromQueue:true), cancellationToken);
}
offsets.AnnotationsOffset = context.Offset; offsets.AnnotationsOffset = context.Offset;
ThrottleExt.Throttle(() => ThrottleExt.Throttle(() =>
{
_api.UpdateOffsets(offsets);
return Task.CompletedTask;
}, SaveQueueOffsetTaskId, TimeSpan.FromSeconds(10), scheduleCallAfterCooldown: true);
}
catch (Exception e)
{ {
_api.UpdateOffsets(offsets); _logger.LogError(e, e.Message);
return Task.CompletedTask; }
}, SaveQueueOffsetTaskId, TimeSpan.FromSeconds(10), scheduleCallAfterCooldown: true);
} }
}); });
} }