38 lines
1.1 KiB
C#
38 lines
1.1 KiB
C#
using LiteCharms.Features.Abstractions;
|
|
using LiteCharms.Features.Mediator;
|
|
|
|
namespace LiteCharms.Features.Quartz;
|
|
|
|
[DisallowConcurrentExecution]
|
|
public class MediatorJob<TNotification>(IMediator mediator) : IJob where TNotification : IEvent
|
|
{
|
|
public async Task Execute(IJobExecutionContext context)
|
|
{
|
|
var data = context.MergedJobDataMap["Payload"] as string;
|
|
|
|
if (string.IsNullOrWhiteSpace(data))
|
|
{
|
|
Trace.WriteLine("Job Payload missing, job ended");
|
|
|
|
return;
|
|
}
|
|
|
|
var notification = JsonSerializer.Deserialize<TNotification>(data);
|
|
|
|
if (notification is null)
|
|
{
|
|
Trace.WriteLine("Notification could not be JSon converted from data string, job ended");
|
|
|
|
return;
|
|
}
|
|
|
|
using var activity = MediatorTelemetry.Source.StartActivity($"Quartz: {typeof(TNotification).Name}");
|
|
|
|
activity?.SetTag("event.correlation_id", notification.CorrelationId);
|
|
|
|
await mediator.Publish(notification, context.CancellationToken);
|
|
|
|
Trace.WriteLine("Job published");
|
|
}
|
|
}
|