From 6741eba404e700adc02993dddb98a91061675607 Mon Sep 17 00:00:00 2001 From: forest Date: Wed, 30 Mar 2022 12:10:55 -0500 Subject: [PATCH] add notification email for accounts with extreme negative balances --- capsulflask/cli.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/capsulflask/cli.py b/capsulflask/cli.py index 9b17c57..cd2dcc7 100644 --- a/capsulflask/cli.py +++ b/capsulflask/cli.py @@ -235,6 +235,9 @@ def get_warnings_list(): def notify_users_about_account_balance(): accounts = get_model().all_accounts() + + out_of_bounds_accounts = dict() + for account in accounts: vms = get_model().list_vms_for_account(account['email']) payments = get_model().list_payments_for_account(account['email']) @@ -243,6 +246,9 @@ def notify_users_about_account_balance(): balance_now = get_account_balance(vms, payments, datetime.utcnow()) current_warning = account['account_balance_warning'] + if balance_now < -11: + out_of_bounds_accounts[account['email']] = balance_now + longterm_vms = list(filter(lambda vm: vm['shortterm'] == False, vms)) if len(longterm_vms) == 0: @@ -280,6 +286,28 @@ def notify_users_about_account_balance(): current_app.logger.warning(f"cron_task: deleting {vm['id']} ( {account['email']} ) due to negative account balance.") current_app.config["HUB_MODEL"].destroy(email=account["email"], id=vm['id']) get_model().delete_vm(email=account["email"], id=vm['id']) + + if len(out_of_bounds_accounts) > 0: + lines = ["The following accounts have out-of-bounds account balances:", ""] + + for email, balance in out_of_bounds_accounts.items(): + lines.append(f"{email}: ${format(balance, '.2f')}") + + email_addresses_raw = current_app.config['ADMIN_EMAIL_ADDRESSES'].split(",") + email_addresses = list(filter(lambda x: len(x) > 6, map(lambda x: x.strip(), email_addresses_raw ) )) + + current_app.logger.info(f"notify_users_about_account_balance: sending out of bounds account balances email to {','.join(email_addresses)}:") + for line in lines: + current_app.logger.info(f"notify_users_about_account_balance: {line}.") + + current_app.config["FLASK_MAIL_INSTANCE"].send( + Message( + "Capsul Out Of Bounds Account Balance Notification", + sender=current_app.config["MAIL_DEFAULT_SENDER"], + body="\n".join(lines), + recipients=email_addresses + ) + ) def delete_shortterm_vms_if_account_is_empty():