From 6b2bd18912a539083d3514da06a5510323424534 Mon Sep 17 00:00:00 2001 From: Marcos Costa Date: Mon, 21 Aug 2023 22:19:52 -0400 Subject: [PATCH] Add try catch --- .../QueueConsumerHostedService.cs | 41 +++++++++++-------- src/RinhaBackEnd/Program.cs | 4 -- src/docker-compose.yml | 4 +- 3 files changed, 27 insertions(+), 22 deletions(-) diff --git a/src/RinhaBackEnd/HostedServices/QueueConsumerHostedService.cs b/src/RinhaBackEnd/HostedServices/QueueConsumerHostedService.cs index 9c5c6f0..c95b3f8 100644 --- a/src/RinhaBackEnd/HostedServices/QueueConsumerHostedService.cs +++ b/src/RinhaBackEnd/HostedServices/QueueConsumerHostedService.cs @@ -27,24 +27,33 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) continue; } NpgsqlConnection connection = null!; - connection = scope.ServiceProvider.GetRequiredService(); - await connection.OpenAsync(stoppingToken); - - await using var batch = new NpgsqlBatch(connection); - - for (int i = 0; i < peopleInQueue.Length; i++) + try { - var cmd = new NpgsqlBatchCommand("INSERT INTO PESSOAS (ID, APELIDO, NOME, NASCIMENTO, STACK) VALUES ($1, $2, $3, $4, $5)"); - cmd.Parameters.AddWithValue(peopleInQueue[i].Id); - cmd.Parameters.AddWithValue(peopleInQueue[i].Apelido); - cmd.Parameters.AddWithValue(peopleInQueue[i].Nome); - cmd.Parameters.AddWithValue(peopleInQueue[i].Nascimento); - cmd.Parameters.AddWithValue(peopleInQueue[i].Stack); - batch.BatchCommands.Add(cmd); + connection = scope.ServiceProvider.GetRequiredService(); + await connection.OpenAsync(stoppingToken); + + await using var batch = new NpgsqlBatch(connection); + + for (int i = 0; i < peopleInQueue.Length; i++) + { + var cmd = new NpgsqlBatchCommand("INSERT INTO PESSOAS (ID, APELIDO, NOME, NASCIMENTO, STACK) VALUES ($1, $2, $3, $4, $5)"); + cmd.Parameters.AddWithValue(peopleInQueue[i].Id); + cmd.Parameters.AddWithValue(peopleInQueue[i].Apelido); + cmd.Parameters.AddWithValue(peopleInQueue[i].Nome); + cmd.Parameters.AddWithValue(peopleInQueue[i].Nascimento); + cmd.Parameters.AddWithValue(peopleInQueue[i].Stack); + batch.BatchCommands.Add(cmd); + } + + await batch.ExecuteNonQueryAsync(stoppingToken); + } + catch + { + } + finally + { + connection?.Close(); } - - await batch.ExecuteNonQueryAsync(stoppingToken); - connection?.Close(); } } } diff --git a/src/RinhaBackEnd/Program.cs b/src/RinhaBackEnd/Program.cs index 4d58199..a4cadff 100644 --- a/src/RinhaBackEnd/Program.cs +++ b/src/RinhaBackEnd/Program.cs @@ -3,8 +3,6 @@ using RinhaBackEnd.Dtos.Response; using RinhaBackEnd.Extensions; using RinhaBackEnd.HostedServices; -using StackExchange.Redis; -using System.Collections.Concurrent; var builder = WebApplication.CreateBuilder(args); @@ -59,8 +57,6 @@ await db.StringSetAsync($"personApelido:{person.Apelido}", ".", TimeSpan.FromMinutes(10)); - //localRecords.TryAdd(result.Id, result); - await sub.PublishAsync("added-record", jsonResult); return Results.Created(new Uri($"/pessoas/{person.Id}", uriKind: UriKind.Relative), result); diff --git a/src/docker-compose.yml b/src/docker-compose.yml index 474eea9..9c851f3 100644 --- a/src/docker-compose.yml +++ b/src/docker-compose.yml @@ -55,13 +55,13 @@ services: POSTGRES_DB: rinhadb volumes: - ./create-script.sql:/docker-entrypoint-initdb.d/create-script.sql - command: postgres -c 'max_connections=1000' + command: postgres -c 'max_connections=1000' -c work_mem=800MB -c shared_buffers=1GB redis: image: redis container_name: cache ports: - "6379:6379" - command: redis-server --save "" --appendonly no + command: redis-server --save "" --appendonly no --maxclients 20000 deploy: resources: limits: