Files
Khwezi Mngoma 961f03c1c7
continuous-integration/drone/pr Build is passing
Added guardrails around the cluster as well as software level
2026-06-03 11:11:22 +02:00

52 lines
1.6 KiB
C#

using LiteCharms.Features.Abstractions;
using LiteCharms.Features.Mediator;
namespace LiteCharms.Features.Quartz;
[DisallowConcurrentExecution]
public sealed class MediatorJob<TNotification>(IMediator mediator) : IJob where TNotification : IEvent
{
public async Task Execute(IJobExecutionContext context)
{
if (context.Recovering)
Trace.WriteLine($"CRITICAL RECOVERY: Resurrecting job '{typeof(TNotification).Name}' after a previous cluster node crashed mid-execution.");
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(typeof(TNotification).Name);
activity?.SetTag("event.correlation_id", notification.CorrelationId);
try
{
await mediator.Publish(notification, context.CancellationToken);
Trace.WriteLine("Job published successfully");
}
catch (OperationCanceledException) when (context.CancellationToken.IsCancellationRequested)
{
Trace.WriteLine($"Job '{typeof(TNotification).Name}' was gracefully interrupted by the cluster control plane.");
activity?.SetStatus(ActivityStatusCode.Ok);
return;
}
}
}