~singpolyma/sgx-jmp

d0569f45823cf5367ca26cdd244cbf8534484b1a — Stephen Paul Weber 2 years ago 07aca05
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.
1 files changed, 11 insertions(+), 3 deletions(-)

M sgx_jmp.rb
M sgx_jmp.rb => sgx_jmp.rb +11 -3
@@ 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