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()
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()

View File

@ -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)

View File

@ -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()

View File

@ -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)

View File

@ -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)

View File

@ -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):

View File

@ -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()

View File

@ -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()