Skip to content

Commit

Permalink
Merge pull request #69 from MarcosCostaDev/fix/add-try-catch
Browse files Browse the repository at this point in the history
Add try catch
  • Loading branch information
MarcosCostaDev committed Aug 22, 2023
2 parents 69fa558 + 6b2bd18 commit b93bf4a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
41 changes: 25 additions & 16 deletions src/RinhaBackEnd/HostedServices/QueueConsumerHostedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,33 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
continue;
}
NpgsqlConnection connection = null!;
connection = scope.ServiceProvider.GetRequiredService<NpgsqlConnection>();
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<NpgsqlConnection>();
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();
}
}
}
4 changes: 0 additions & 4 deletions src/RinhaBackEnd/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit b93bf4a

Please sign in to comment.