cron, init app, job runner: wrap in an app context to benefit from app setup like database cleanup, sentry integration, etc

This commit is contained in:
Son 2021-10-26 10:52:28 +02:00
parent a0165d6381
commit a99ac24b72
5 changed files with 147 additions and 163 deletions

View File

@ -59,6 +59,7 @@ from app.models import (
HibpNotifiedAlias,
)
from app.utils import sanitize_email
from server import create_light_app
def notify_trial_end():
@ -895,7 +896,8 @@ if __name__ == "__main__":
],
)
args = parser.parse_args()
# wrap in an app context to benefit from app setup like database cleanup, sentry integration, etc
with create_light_app().app_context():
if args.job == "stats":
LOG.d("Compute Stats")
stats()

View File

@ -150,19 +150,6 @@ if NEWRELIC_CONFIG_PATH:
newrelic_app = newrelic.agent.register_application()
# fix the database connection leak issue
# use this method instead of create_app
def new_app():
app = create_light_app()
@app.teardown_appcontext
def shutdown_session(response_or_exc):
# same as shutdown_session() in flask-sqlalchemy but this is not enough
Session.remove()
return app
def get_or_create_contact(from_header: str, mail_from: str, alias: Alias) -> Contact:
"""
contact_from_header is the RFC 2047 format FROM header
@ -2059,8 +2046,7 @@ class MailHandler:
envelope.rcpt_tos,
)
app = new_app()
with app.app_context():
with create_light_app().app_context():
ret = handle(envelope)
elapsed = time.time() - start
LOG.i(

View File

@ -3,6 +3,7 @@ from app.db import Session
from app.log import LOG
from app.models import Mailbox, Contact, SLDomain
from app.pgp_utils import load_public_key
from server import create_light_app
def load_pgp_public_keys():
@ -50,5 +51,7 @@ def add_sl_domains():
if __name__ == "__main__":
# wrap in an app context to benefit from app setup like database cleanup, sentry integration, etc
with create_light_app().app_context():
load_pgp_public_keys()
add_sl_domains()

View File

@ -26,19 +26,6 @@ from app.models import User, Job, BatchImport, Mailbox, CustomDomain
from server import create_light_app
# fix the database connection leak issue
# use this method instead of create_app
def new_app():
app = create_light_app()
@app.teardown_appcontext
def shutdown_session(response_or_exc):
# same as shutdown_session() in flask-sqlalchemy but this is not enough
Session.remove()
return app
def onboarding_send_from_alias(user):
to_email, unsubscribe_link, via_email = user.get_communication_email()
if not to_email:
@ -101,6 +88,8 @@ def onboarding_mailbox(user):
if __name__ == "__main__":
while True:
# wrap in an app context to benefit from app setup like database cleanup, sentry integration, etc
with create_light_app().app_context():
# run a job 1h earlier or later is not a big deal ...
min_dt = arrow.now().shift(hours=-1)
max_dt = arrow.now().shift(hours=1)

View File

@ -131,6 +131,10 @@ def create_light_app() -> Flask:
app.config["SQLALCHEMY_DATABASE_URI"] = DB_URI
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
@app.teardown_appcontext
def shutdown_session(response_or_exc):
Session.remove()
return app