private async Task ProcessEvent(WebhookEventWrapper e) { try { using var scope = _scopeFactory.CreateScope(); var dbContext = scope.ServiceProvider.GetRequiredService <VmContext>(); // Don't process if event is expired if (!e.IsExpired()) { switch (e.WebhookEvent.Type) { case EventType.ViewCreated: // Since payload field is of type object, the json serialization in player makes it into string containing json var payloadCreate = JsonConvert.DeserializeObject <ViewCreated>(e.WebhookEvent.Payload.ToString()); await CloneMaps(payloadCreate, dbContext); break; case EventType.ViewDeleted: var payloadDelete = JsonConvert.DeserializeObject <ViewDeleted>(e.WebhookEvent.Payload.ToString()); await DeleteClonedMaps(payloadDelete.ViewId, dbContext); break; } } // if no exceptions, remove this event from the db so we don't process it again dbContext.WebhookEvents.Remove(e.WebhookEvent); await dbContext.SaveChangesAsync(); } catch (Exception ex) { _logger.LogError(ex, $"Exception processing event with Id = {e.WebhookEvent?.Id}"); // Add back to queue after a delay to retry _ = Task.Run(async() => { await Task.Delay(new TimeSpan(0, 0, e.GetRetryDelaySeconds())); await AddEvent(e); }); } }
private async Task AddEvent(WebhookEventWrapper e) { e.Attempts++; await _eventQueue.SendAsync(e); }