mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 22:56:29 +00:00
067f02cc63
rework AIAvailabilityStatus events to mediatr
43 lines
1.3 KiB
C#
43 lines
1.3 KiB
C#
using Azaion.Common.Database;
|
|
using Azaion.Common.DTO;
|
|
using Azaion.Common.Events;
|
|
using MediatR;
|
|
using MessagePack;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Azaion.Common.Services.Inference;
|
|
|
|
public class InferenceServiceEventHandler(IInferenceService inferenceService,
|
|
IAnnotationService annotationService,
|
|
IMediator mediator,
|
|
ILogger<InferenceServiceEventHandler> logger) :
|
|
INotificationHandler<InferenceDataEvent>,
|
|
INotificationHandler<AIAvailabilityStatusEvent>
|
|
{
|
|
public async Task Handle(InferenceDataEvent e, CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
if (e.Command.Message == "DONE")
|
|
{
|
|
await inferenceService.InferenceCancelTokenSource.CancelAsync();
|
|
return;
|
|
}
|
|
|
|
var annImage = MessagePackSerializer.Deserialize<AnnotationImage>(e.Command.Data, cancellationToken: ct);
|
|
var annotation = await annotationService.SaveAnnotation(annImage, ct);
|
|
await mediator.Publish(new AnnotationAddedEvent(annotation), ct);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogError(ex, ex.Message);
|
|
}
|
|
}
|
|
|
|
public async Task Handle(AIAvailabilityStatusEvent e, CancellationToken ct)
|
|
{
|
|
|
|
e.Status = AIAvailabilityEnum.Enabled;
|
|
|
|
}
|
|
} |