| | | 1 | | using Quartz; |
| | | 2 | | using System.Diagnostics; |
| | | 3 | | using System.Collections.Concurrent; |
| | | 4 | | |
| | | 5 | | namespace Syki.Back.Webhooks; |
| | | 6 | | |
| | 0 | 7 | | public class ReceivedWebhookEventsProcessor(IServiceScopeFactory serviceScopeFactory) : IJob |
| | | 8 | | { |
| | 0 | 9 | | private static readonly ActivitySource _activitySource = new(OpenTelemetryConfigs.WebhookEventsProcessing); |
| | | 10 | | |
| | | 11 | | public async Task Execute(IJobExecutionContext context) |
| | | 12 | | { |
| | 0 | 13 | | using var scope = serviceScopeFactory.CreateScope(); |
| | 0 | 14 | | var ctx = scope.ServiceProvider.GetRequiredService<SykiDbContext>(); |
| | | 15 | | |
| | 0 | 16 | | await Process(scope, ctx); |
| | 0 | 17 | | } |
| | | 18 | | |
| | | 19 | | private static async Task Process(IServiceScope scope, SykiDbContext ctx) |
| | | 20 | | { |
| | 0 | 21 | | var processorId = Guid.NewGuid(); |
| | 0 | 22 | | var commandsCreated = false; |
| | | 23 | | |
| | 0 | 24 | | while (true) |
| | | 25 | | { |
| | 0 | 26 | | var events = await ctx.ReceivedWebhookEvents.FromSqlRaw(Sql, processorId).ToListAsync(); |
| | 0 | 27 | | if (events.Count == 0) break; |
| | | 28 | | |
| | 0 | 29 | | foreach (var evt in events) |
| | | 30 | | { |
| | 0 | 31 | | using var activity = _activitySource.StartActivity($"Process {evt.Type}", ActivityKind.Consumer); |
| | 0 | 32 | | activity?.SetTag("webhook_event.id", evt.Id); |
| | 0 | 33 | | activity?.SetTag("webhook_event.type", evt.Type); |
| | 0 | 34 | | activity?.SetTag("webhook_event.source", evt.Source.ToString()); |
| | | 35 | | |
| | | 36 | | try |
| | | 37 | | { |
| | 0 | 38 | | ctx.ChangeTracker.Clear(); |
| | 0 | 39 | | ctx.Attach(evt); |
| | | 40 | | |
| | 0 | 41 | | await ctx.Database.BeginTransactionAsync(); |
| | | 42 | | |
| | 0 | 43 | | if (WebhookConfigs.TryGetEventType(evt.Type, out _)) |
| | | 44 | | { |
| | 0 | 45 | | var invoker = GetInvoker(evt.Type); |
| | 0 | 46 | | await invoker.Invoke(scope.ServiceProvider, evt); |
| | 0 | 47 | | commandsCreated = commandsCreated || evt.CommandId != null; |
| | | 48 | | } |
| | | 49 | | else |
| | | 50 | | { |
| | 0 | 51 | | evt.Status = ReceivedWebhookEventStatus.Ignored; |
| | 0 | 52 | | evt.ProcessedAt = DateTime.UtcNow; |
| | | 53 | | } |
| | | 54 | | |
| | 0 | 55 | | await ctx.SaveChangesAsync(); |
| | 0 | 56 | | await ctx.Database.CommitTransactionAsync(); |
| | 0 | 57 | | } |
| | 0 | 58 | | catch (Exception ex) |
| | | 59 | | { |
| | 0 | 60 | | if (ctx.Database.CurrentTransaction is not null) |
| | 0 | 61 | | await ctx.Database.RollbackTransactionAsync(); |
| | | 62 | | |
| | 0 | 63 | | ctx.ChangeTracker.Clear(); |
| | 0 | 64 | | ctx.Attach(evt); |
| | 0 | 65 | | evt.Status = ReceivedWebhookEventStatus.Error; |
| | 0 | 66 | | evt.Error = ex.Message + ex.InnerException?.Message; |
| | 0 | 67 | | evt.ProcessedAt = DateTime.UtcNow; |
| | 0 | 68 | | await ctx.SaveChangesAsync(); |
| | | 69 | | |
| | 0 | 70 | | activity?.SetStatus(ActivityStatusCode.Error, ex.Message); |
| | 0 | 71 | | activity?.AddException(ex); |
| | 0 | 72 | | } |
| | 0 | 73 | | } |
| | | 74 | | } |
| | | 75 | | |
| | 0 | 76 | | if (commandsCreated) |
| | | 77 | | { |
| | 0 | 78 | | var scheduler = await scope.ServiceProvider.GetRequiredService<ISchedulerFactory>().GetScheduler(); |
| | 0 | 79 | | await scheduler.TriggerCommandsProcessorJob(); |
| | | 80 | | } |
| | 0 | 81 | | } |
| | | 82 | | |
| | 0 | 83 | | private static readonly ConcurrentDictionary<string, IWebhookEventInvoker> _invokers = new(); |
| | | 84 | | private static IWebhookEventInvoker GetInvoker(string eventType) |
| | | 85 | | { |
| | 0 | 86 | | return _invokers.GetOrAdd(eventType, type => |
| | 0 | 87 | | { |
| | 0 | 88 | | WebhookConfigs.TryGetEventType(type, out var webhookEventType); |
| | 0 | 89 | | var invokerType = typeof(WebhookEventInvoker<>).MakeGenericType(webhookEventType); |
| | 0 | 90 | | return (IWebhookEventInvoker)Activator.CreateInstance(invokerType)!; |
| | 0 | 91 | | }); |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// See <see cref="ReceivedWebhookEventStatus"/> for status mapping |
| | | 96 | | /// </summary> |
| | 0 | 97 | | private static readonly string Sql = @" |
| | 0 | 98 | | UPDATE web.received_webhook_events |
| | 0 | 99 | | SET processor_id = {0}, status = 1 |
| | 0 | 100 | | WHERE ctid IN ( |
| | 0 | 101 | | SELECT ctid |
| | 0 | 102 | | FROM web.received_webhook_events |
| | 0 | 103 | | WHERE processor_id IS NULL AND status = 0 |
| | 0 | 104 | | ORDER BY created_at |
| | 0 | 105 | | FOR UPDATE SKIP LOCKED |
| | 0 | 106 | | LIMIT 10 |
| | 0 | 107 | | ) |
| | 0 | 108 | | RETURNING *; |
| | 0 | 109 | | "; |
| | | 110 | | } |