Don't use pg pool, too flaky for now

This commit is contained in:
2024-08-21 18:18:35 -03:00
parent dbbf7224b3
commit 9190060c52
5 changed files with 70 additions and 86 deletions

View File

@ -11,11 +11,6 @@ DBHOST = File.read("/secrets/dbhost").strip
DBURL = "postgres://#{USER}:#{PASS}@#{DBHOST}:5432/nombres"
puts "Connnecting to #{DBURL}"
# Create a connection pool to the database
pg = ConnectionPool.new(capacity: 25, timeout: 1.seconds) do
PG.connect(DBURL)
end
# Connect to the database and get information about
# the requested names
get "/" do |env|
@ -31,7 +26,7 @@ get "/" do |env|
results << [anio.to_s]
end
# Connect using credentials provided
pg.connection do |cursor|
db = DB.open DBURL
# Get the information for each name
names.map do |name|
# Normalize: remove diacritics etc.
@ -41,7 +36,7 @@ get "/" do |env|
}.join("").downcase
counter_per_year = {} of Int32 => Int32
cursor.query("
db.query("
SELECT anio::integer, contador::integer
FROM nombres WHERE nombre = $1", name) do |result_set|
result_set.each do
@ -52,15 +47,15 @@ get "/" do |env|
results[anio - 1921] << counter_per_year.fetch(anio, 0).to_s
end
end
end
results.to_json
ensure
db.try &.close
end
# The `/ping/` endpoint is configured in the container as a healthcheck
# You can make it better by checking that your database is responding
# or whatever checks you think are important
#
get "/ping/" do
pg.connection.exec("SELECT 42")
db = DB.open DBURL
db.exec("SELECT 42")
"OK"
ensure
db.try &.close
end