From fed130f9dd480f1d4a9cb90ef2b2f387ceee47cf Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Sat, 16 Mar 2024 18:15:37 -0500 Subject: [PATCH] Make sure we hold the same conn for listen forever hold by itself will never cut it, because we use EM timers inside to break the connection even to the promise, so we can't sync on that. Since we really do want to essentially leak this connection for the life of the process, then let's do that directly. --- lib/postgres.rb | 7 +++++++ sgx_jmp.rb | 21 ++++++++++----------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/postgres.rb b/lib/postgres.rb index ba7af8b4935947e558d7b709357013c40c0e343e..603d9b8342ec7e3528f239a4f3a827e73f98dddd 100644 --- a/lib/postgres.rb +++ b/lib/postgres.rb @@ -10,6 +10,13 @@ class Postgres < SimpleDelegator }) end + # WARNING: this uses up a connection from the pool which never releases + # Use it only if you need the same connection for the life of the process + def acquire + conn = __getobj__.send(:acquire, Fiber.current) until conn + conn + end + def query_one(sql, *args, field_names_as: :symbol, default: nil) query_defer(sql, args).then do |rows| rows.field_names_as(field_names_as)&.first || default diff --git a/sgx_jmp.rb b/sgx_jmp.rb index 3e2f22d9693ff1e6a9fef50c8aa67b5324f11fd6..df7870ad28bc95918171cafef0723012096cc15e 100644 --- a/sgx_jmp.rb +++ b/sgx_jmp.rb @@ -223,17 +223,16 @@ when_ready do DB = Postgres.connect(dbname: "jmp", size: 5) TEL_SELECTIONS = TelSelections.new - EMPromise.resolve(nil).then do - DB.hold do |conn| - conn.query("LISTEN low_balance") - conn.query("LISTEN possible_renewal") - catchup_notify_low_balance(conn) - catchup_notify_possible_renewal(conn) - - repo = CustomerRepo.new(sgx_repo: Bwmsgsv2Repo.new) - poll_for_notify(conn, repo).sync - end - end + EMPromise.resolve(nil).then { + conn = DB.acquire + conn.query("LISTEN low_balance") + conn.query("LISTEN possible_renewal") + catchup_notify_low_balance(conn) + catchup_notify_possible_renewal(conn) + + repo = CustomerRepo.new(sgx_repo: Bwmsgsv2Repo.new) + poll_for_notify(conn, repo) + }.catch(&method(:panic)) load_plans_to_db!