From eb0e327402a57e808dd4744c03f2071f3b5a2656 Mon Sep 17 00:00:00 2001 From: Son Date: Tue, 12 Oct 2021 14:47:01 +0200 Subject: [PATCH] remove "with app.app_context():" --- cron.py | 69 +++++++++++++-------------- email_handler.py | 4 +- init_app.py | 7 +-- job_runner.py | 119 ++++++++++++++++++++++------------------------ monitoring.py | 4 +- server.py | 5 +- shell.py | 4 +- tests/conftest.py | 40 ++++++++-------- 8 files changed, 117 insertions(+), 135 deletions(-) diff --git a/cron.py b/cron.py index 64db5ec2..1aa36f2a 100644 --- a/cron.py +++ b/cron.py @@ -849,39 +849,36 @@ if __name__ == "__main__": ) args = parser.parse_args() - app = create_app() - - with app.app_context(): - if args.job == "stats": - LOG.d("Compute Stats") - stats() - elif args.job == "notify_trial_end": - LOG.d("Notify users with trial ending soon") - notify_trial_end() - elif args.job == "notify_manual_subscription_end": - LOG.d("Notify users with manual subscription ending soon") - notify_manual_sub_end() - elif args.job == "notify_premium_end": - LOG.d("Notify users with premium ending soon") - notify_premium_end() - elif args.job == "delete_logs": - LOG.d("Deleted Logs") - delete_logs() - elif args.job == "poll_apple_subscription": - LOG.d("Poll Apple Subscriptions") - poll_apple_subscription() - elif args.job == "sanity_check": - LOG.d("Check data consistency") - sanity_check() - elif args.job == "delete_old_monitoring": - LOG.d("Delete old monitoring records") - delete_old_monitoring() - elif args.job == "check_custom_domain": - LOG.d("Check custom domain") - check_custom_domain() - elif args.job == "check_hibp": - LOG.d("Check HIBP") - asyncio.run(check_hibp()) - elif args.job == "notify_hibp": - LOG.d("Notify users about HIBP breaches") - notify_hibp() + if args.job == "stats": + LOG.d("Compute Stats") + stats() + elif args.job == "notify_trial_end": + LOG.d("Notify users with trial ending soon") + notify_trial_end() + elif args.job == "notify_manual_subscription_end": + LOG.d("Notify users with manual subscription ending soon") + notify_manual_sub_end() + elif args.job == "notify_premium_end": + LOG.d("Notify users with premium ending soon") + notify_premium_end() + elif args.job == "delete_logs": + LOG.d("Deleted Logs") + delete_logs() + elif args.job == "poll_apple_subscription": + LOG.d("Poll Apple Subscriptions") + poll_apple_subscription() + elif args.job == "sanity_check": + LOG.d("Check data consistency") + sanity_check() + elif args.job == "delete_old_monitoring": + LOG.d("Delete old monitoring records") + delete_old_monitoring() + elif args.job == "check_custom_domain": + LOG.d("Check custom domain") + check_custom_domain() + elif args.job == "check_hibp": + LOG.d("Check HIBP") + asyncio.run(check_hibp()) + elif args.job == "notify_hibp": + LOG.d("Notify users about HIBP breaches") + notify_hibp() diff --git a/email_handler.py b/email_handler.py index 5dca07fc..9e2a0bb6 100644 --- a/email_handler.py +++ b/email_handler.py @@ -1986,9 +1986,7 @@ def main(port: int): if LOAD_PGP_EMAIL_HANDLER: LOG.w("LOAD PGP keys") - app = create_app() - with app.app_context(): - load_pgp_public_keys() + load_pgp_public_keys() while True: time.sleep(2) diff --git a/init_app.py b/init_app.py index 542f7229..108194e0 100644 --- a/init_app.py +++ b/init_app.py @@ -51,8 +51,5 @@ def add_sl_domains(): if __name__ == "__main__": - app = create_app() - - with app.app_context(): - load_pgp_public_keys() - add_sl_domains() + load_pgp_public_keys() + add_sl_domains() diff --git a/job_runner.py b/job_runner.py index b2f5daec..f0a9fa7e 100644 --- a/job_runner.py +++ b/job_runner.py @@ -103,70 +103,67 @@ if __name__ == "__main__": min_dt = arrow.now().shift(hours=-1) max_dt = arrow.now().shift(hours=1) - app = new_app() + for job in Job.filter( + Job.taken.is_(False), Job.run_at > min_dt, Job.run_at <= max_dt + ).all(): + LOG.d("Take job %s", job) - with app.app_context(): - for job in Job.filter( - Job.taken.is_(False), Job.run_at > min_dt, Job.run_at <= max_dt - ).all(): - LOG.d("Take job %s", job) + # mark the job as taken, whether it will be executed successfully or not + job.taken = True + Session.commit() - # mark the job as taken, whether it will be executed successfully or not - job.taken = True + if job.name == JOB_ONBOARDING_1: + user_id = job.payload.get("user_id") + user = User.get(user_id) + + # user might delete their account in the meantime + # or disable the notification + if user and user.notification and user.activated: + LOG.d("send onboarding send-from-alias email to user %s", user) + onboarding_send_from_alias(user) + elif job.name == JOB_ONBOARDING_2: + user_id = job.payload.get("user_id") + user = User.get(user_id) + + # user might delete their account in the meantime + # or disable the notification + if user and user.notification and user.activated: + LOG.d("send onboarding mailbox email to user %s", user) + onboarding_mailbox(user) + elif job.name == JOB_ONBOARDING_4: + user_id = job.payload.get("user_id") + user = User.get(user_id) + + # user might delete their account in the meantime + # or disable the notification + if user and user.notification and user.activated: + LOG.d("send onboarding pgp email to user %s", user) + onboarding_pgp(user) + + elif job.name == JOB_BATCH_IMPORT: + batch_import_id = job.payload.get("batch_import_id") + batch_import = BatchImport.get(batch_import_id) + handle_batch_import(batch_import) + elif job.name == JOB_DELETE_ACCOUNT: + user_id = job.payload.get("user_id") + user = User.get(user_id) + + if not user: + LOG.i("No user found for %s", user_id) + continue + + user_email = user.email + LOG.w("Delete user %s", user) + User.delete(user.id) Session.commit() - if job.name == JOB_ONBOARDING_1: - user_id = job.payload.get("user_id") - user = User.get(user_id) - - # user might delete their account in the meantime - # or disable the notification - if user and user.notification and user.activated: - LOG.d("send onboarding send-from-alias email to user %s", user) - onboarding_send_from_alias(user) - elif job.name == JOB_ONBOARDING_2: - user_id = job.payload.get("user_id") - user = User.get(user_id) - - # user might delete their account in the meantime - # or disable the notification - if user and user.notification and user.activated: - LOG.d("send onboarding mailbox email to user %s", user) - onboarding_mailbox(user) - elif job.name == JOB_ONBOARDING_4: - user_id = job.payload.get("user_id") - user = User.get(user_id) - - # user might delete their account in the meantime - # or disable the notification - if user and user.notification and user.activated: - LOG.d("send onboarding pgp email to user %s", user) - onboarding_pgp(user) - - elif job.name == JOB_BATCH_IMPORT: - batch_import_id = job.payload.get("batch_import_id") - batch_import = BatchImport.get(batch_import_id) - handle_batch_import(batch_import) - elif job.name == JOB_DELETE_ACCOUNT: - user_id = job.payload.get("user_id") - user = User.get(user_id) - - if not user: - LOG.i("No user found for %s", user_id) - continue - - user_email = user.email - LOG.w("Delete user %s", user) - User.delete(user.id) - Session.commit() - - send_email( - user_email, - "Your SimpleLogin account has been deleted", - render("transactional/account-delete.txt"), - render("transactional/account-delete.html"), - ) - else: - LOG.e("Unknown job name %s", job.name) + send_email( + user_email, + "Your SimpleLogin account has been deleted", + render("transactional/account-delete.txt"), + render("transactional/account-delete.html"), + ) + else: + LOG.e("Unknown job name %s", job.name) time.sleep(10) diff --git a/monitoring.py b/monitoring.py index 631041ad..6a28e5ca 100644 --- a/monitoring.py +++ b/monitoring.py @@ -59,9 +59,7 @@ def nb_files(directory) -> int: if __name__ == "__main__": while True: - app = create_app() - with app.app_context(): - get_stats() + get_stats() # 1 min sleep(60) diff --git a/server.py b/server.py index beea93f0..b2f0c544 100644 --- a/server.py +++ b/server.py @@ -924,9 +924,8 @@ def register_custom_commands(app): from init_app import add_sl_domains LOG.w("reset db, add fake data") - with app.app_context(): - fake_data() - add_sl_domains() + fake_data() + add_sl_domains() def setup_do_not_track(app): diff --git a/shell.py b/shell.py index 2553df68..0207fb59 100644 --- a/shell.py +++ b/shell.py @@ -137,9 +137,7 @@ def send_onboarding_emails(user): onboarding_pgp(user) -app = create_app() - -with app.app_context(): +if __name__ == "__main__": # to test email template # with open("/tmp/email.html", "w") as f: # user = User.first() diff --git a/tests/conftest.py b/tests/conftest.py index 9f909c1f..8aa370c3 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -24,20 +24,19 @@ app.config["TESTING"] = True app.config["WTF_CSRF_ENABLED"] = False app.config["SERVER_NAME"] = "sl.test" -with app.app_context(): - # enable pg_trgm extension - with engine.connect() as conn: - try: - conn.execute("DROP EXTENSION if exists pg_trgm") - conn.execute("CREATE EXTENSION pg_trgm") - except sqlalchemy.exc.InternalError as e: - if isinstance(e.orig, errors.lookup(DEPENDENT_OBJECTS_STILL_EXIST)): - print(">>> pg_trgm can't be dropped, ignore") - conn.execute("Rollback") +# enable pg_trgm extension +with engine.connect() as conn: + try: + conn.execute("DROP EXTENSION if exists pg_trgm") + conn.execute("CREATE EXTENSION pg_trgm") + except sqlalchemy.exc.InternalError as e: + if isinstance(e.orig, errors.lookup(DEPENDENT_OBJECTS_STILL_EXIST)): + print(">>> pg_trgm can't be dropped, ignore") + conn.execute("Rollback") - Base.metadata.create_all(engine) +Base.metadata.create_all(engine) - add_sl_domains() +add_sl_domains() @pytest.fixture @@ -49,12 +48,11 @@ def flask_app(): def flask_client(): transaction = connection.begin() - with app.app_context(): - try: - client = app.test_client() - yield client - finally: - # roll back all commits made during a test - transaction.rollback() - Session.rollback() - Session.close() + try: + client = app.test_client() + yield client + finally: + # roll back all commits made during a test + transaction.rollback() + Session.rollback() + Session.close()