remove "with app.app_context():"

This commit is contained in:
Son 2021-10-12 14:47:01 +02:00
parent 074dd875dc
commit eb0e327402
8 changed files with 117 additions and 135 deletions

69
cron.py
View file

@ -849,39 +849,36 @@ if __name__ == "__main__":
) )
args = parser.parse_args() args = parser.parse_args()
app = create_app() if args.job == "stats":
LOG.d("Compute Stats")
with app.app_context(): stats()
if args.job == "stats": elif args.job == "notify_trial_end":
LOG.d("Compute Stats") LOG.d("Notify users with trial ending soon")
stats() notify_trial_end()
elif args.job == "notify_trial_end": elif args.job == "notify_manual_subscription_end":
LOG.d("Notify users with trial ending soon") LOG.d("Notify users with manual subscription ending soon")
notify_trial_end() notify_manual_sub_end()
elif args.job == "notify_manual_subscription_end": elif args.job == "notify_premium_end":
LOG.d("Notify users with manual subscription ending soon") LOG.d("Notify users with premium ending soon")
notify_manual_sub_end() notify_premium_end()
elif args.job == "notify_premium_end": elif args.job == "delete_logs":
LOG.d("Notify users with premium ending soon") LOG.d("Deleted Logs")
notify_premium_end() delete_logs()
elif args.job == "delete_logs": elif args.job == "poll_apple_subscription":
LOG.d("Deleted Logs") LOG.d("Poll Apple Subscriptions")
delete_logs() poll_apple_subscription()
elif args.job == "poll_apple_subscription": elif args.job == "sanity_check":
LOG.d("Poll Apple Subscriptions") LOG.d("Check data consistency")
poll_apple_subscription() sanity_check()
elif args.job == "sanity_check": elif args.job == "delete_old_monitoring":
LOG.d("Check data consistency") LOG.d("Delete old monitoring records")
sanity_check() delete_old_monitoring()
elif args.job == "delete_old_monitoring": elif args.job == "check_custom_domain":
LOG.d("Delete old monitoring records") LOG.d("Check custom domain")
delete_old_monitoring() check_custom_domain()
elif args.job == "check_custom_domain": elif args.job == "check_hibp":
LOG.d("Check custom domain") LOG.d("Check HIBP")
check_custom_domain() asyncio.run(check_hibp())
elif args.job == "check_hibp": elif args.job == "notify_hibp":
LOG.d("Check HIBP") LOG.d("Notify users about HIBP breaches")
asyncio.run(check_hibp()) notify_hibp()
elif args.job == "notify_hibp":
LOG.d("Notify users about HIBP breaches")
notify_hibp()

View file

@ -1986,9 +1986,7 @@ def main(port: int):
if LOAD_PGP_EMAIL_HANDLER: if LOAD_PGP_EMAIL_HANDLER:
LOG.w("LOAD PGP keys") LOG.w("LOAD PGP keys")
app = create_app() load_pgp_public_keys()
with app.app_context():
load_pgp_public_keys()
while True: while True:
time.sleep(2) time.sleep(2)

View file

@ -51,8 +51,5 @@ def add_sl_domains():
if __name__ == "__main__": if __name__ == "__main__":
app = create_app() load_pgp_public_keys()
add_sl_domains()
with app.app_context():
load_pgp_public_keys()
add_sl_domains()

View file

@ -103,70 +103,67 @@ if __name__ == "__main__":
min_dt = arrow.now().shift(hours=-1) min_dt = arrow.now().shift(hours=-1)
max_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(): # mark the job as taken, whether it will be executed successfully or not
for job in Job.filter( job.taken = True
Job.taken.is_(False), Job.run_at > min_dt, Job.run_at <= max_dt Session.commit()
).all():
LOG.d("Take job %s", job)
# mark the job as taken, whether it will be executed successfully or not if job.name == JOB_ONBOARDING_1:
job.taken = True 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() Session.commit()
if job.name == JOB_ONBOARDING_1: send_email(
user_id = job.payload.get("user_id") user_email,
user = User.get(user_id) "Your SimpleLogin account has been deleted",
render("transactional/account-delete.txt"),
# user might delete their account in the meantime render("transactional/account-delete.html"),
# or disable the notification )
if user and user.notification and user.activated: else:
LOG.d("send onboarding send-from-alias email to user %s", user) LOG.e("Unknown job name %s", job.name)
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)
time.sleep(10) time.sleep(10)

View file

@ -59,9 +59,7 @@ def nb_files(directory) -> int:
if __name__ == "__main__": if __name__ == "__main__":
while True: while True:
app = create_app() get_stats()
with app.app_context():
get_stats()
# 1 min # 1 min
sleep(60) sleep(60)

View file

@ -924,9 +924,8 @@ def register_custom_commands(app):
from init_app import add_sl_domains from init_app import add_sl_domains
LOG.w("reset db, add fake data") LOG.w("reset db, add fake data")
with app.app_context(): fake_data()
fake_data() add_sl_domains()
add_sl_domains()
def setup_do_not_track(app): def setup_do_not_track(app):

View file

@ -137,9 +137,7 @@ def send_onboarding_emails(user):
onboarding_pgp(user) onboarding_pgp(user)
app = create_app() if __name__ == "__main__":
with app.app_context():
# to test email template # to test email template
# with open("/tmp/email.html", "w") as f: # with open("/tmp/email.html", "w") as f:
# user = User.first() # user = User.first()

View file

@ -24,20 +24,19 @@ app.config["TESTING"] = True
app.config["WTF_CSRF_ENABLED"] = False app.config["WTF_CSRF_ENABLED"] = False
app.config["SERVER_NAME"] = "sl.test" app.config["SERVER_NAME"] = "sl.test"
with app.app_context(): # enable pg_trgm extension
# enable pg_trgm extension with engine.connect() as conn:
with engine.connect() as conn: try:
try: conn.execute("DROP EXTENSION if exists pg_trgm")
conn.execute("DROP EXTENSION if exists pg_trgm") conn.execute("CREATE EXTENSION pg_trgm")
conn.execute("CREATE EXTENSION pg_trgm") except sqlalchemy.exc.InternalError as e:
except sqlalchemy.exc.InternalError as e: if isinstance(e.orig, errors.lookup(DEPENDENT_OBJECTS_STILL_EXIST)):
if isinstance(e.orig, errors.lookup(DEPENDENT_OBJECTS_STILL_EXIST)): print(">>> pg_trgm can't be dropped, ignore")
print(">>> pg_trgm can't be dropped, ignore") conn.execute("Rollback")
conn.execute("Rollback")
Base.metadata.create_all(engine) Base.metadata.create_all(engine)
add_sl_domains() add_sl_domains()
@pytest.fixture @pytest.fixture
@ -49,12 +48,11 @@ def flask_app():
def flask_client(): def flask_client():
transaction = connection.begin() transaction = connection.begin()
with app.app_context(): try:
try: client = app.test_client()
client = app.test_client() yield client
yield client finally:
finally: # roll back all commits made during a test
# roll back all commits made during a test transaction.rollback()
transaction.rollback() Session.rollback()
Session.rollback() Session.close()
Session.close()