M lib/postgres.rb => lib/postgres.rb +7 -0
@@ 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
M sgx_jmp.rb => sgx_jmp.rb +10 -11
@@ 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!