From d0569f45823cf5367ca26cdd244cbf8534484b1a Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Wed, 8 Jun 2022 10:06:16 -0500 Subject: [PATCH] Do not catchup low-balance notifications for expired customers Makes the number of users to do on startup much smaller and slower-growing. Expired users have been told about their low balance quite a bit already, and will be notified by billing cronjob etc from here out. --- sgx_jmp.rb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/sgx_jmp.rb b/sgx_jmp.rb index d347337..b6a3b93 100644 --- a/sgx_jmp.rb +++ b/sgx_jmp.rb @@ -183,10 +183,17 @@ end EM.error_handler(&method(:panic)) # Infer anything we might have been notified about while we were down -def catchup_notify(db) - db.query("SELECT customer_id FROM balances WHERE balance < 5").each do |c| +def catchup_notify_low_balance(db) + db.query(<<~SQL).each do |c| + SELECT customer_id + FROM balances INNER JOIN customer_plans USING (customer_id) + WHERE balance < 5 AND expires_at > LOCALTIMESTAMP + SQL db.query("SELECT pg_notify('low_balance', $1)", c.values) end +end + +def catchup_notify_possible_renewal db.query(<<~SQL).each do |c| SELECT customer_id FROM customer_plans INNER JOIN balances USING (customer_id) @@ -227,7 +234,8 @@ when_ready do DB.hold do |conn| conn.query("LISTEN low_balance") conn.query("LISTEN possible_renewal") - catchup_notify(conn) + catchup_notify_low_balance(conn) + catchup_notify_possible_renewal(conn) poll_for_notify(conn) end -- 2.45.2