public IActionResult Ping(PingWebhookEvent @event) { try { _logger.LogInformation($"Received event '{nameof(PingWebhookEvent)}' with payload '{JsonConvert.SerializeObject(@event)}'."); if (@event.IsException) { throw new InvalidOperationException("This webhook is misbehaving."); } return(WebhookOk()); } catch (Exception ex) { return(WebhookException(ex)); } }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { async Task PublishPingAsync(bool isException = false) { var @event = new PingWebhookEvent { IsException = isException }; _logger.LogInformation($"Publishing event '{@event.GetType().FullName}' with payload '{JsonConvert.SerializeObject(@event)}'."); await _webhooksClient.PublishAsync(@event); } try { await Task.Delay(3000, stoppingToken); while (!stoppingToken.IsCancellationRequested) { // If we aren't publishing any events then we are just a subscriber and can "delay" infinitely. var delay = 0; if (Options.Role == WebhooksWorkerRole.Pub || Options.Role == WebhooksWorkerRole.PubSub) { // Publish a ping every 1 to 60 seconds. delay = _random.Next(1, 61); await PublishPingAsync(); // Let's randomly simulate a problematic webhook. if (!stoppingToken.IsCancellationRequested && _random.Next(1, 1001) == 1) { await PublishPingAsync(true); } _logger.LogInformation($"Next publish at '{DateTime.Now.AddSeconds(delay)}'."); } // publish events? await Task.Delay(delay * 1_000, stoppingToken); } } catch (TaskCanceledException) { } catch (Exception ex) { _logger.LogError(ex, ex.Message); } }