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:
parent
a0165d6381
commit
a99ac24b72
4
cron.py
4
cron.py
|
@ -59,6 +59,7 @@ from app.models import (
|
||||||
HibpNotifiedAlias,
|
HibpNotifiedAlias,
|
||||||
)
|
)
|
||||||
from app.utils import sanitize_email
|
from app.utils import sanitize_email
|
||||||
|
from server import create_light_app
|
||||||
|
|
||||||
|
|
||||||
def notify_trial_end():
|
def notify_trial_end():
|
||||||
|
@ -895,7 +896,8 @@ if __name__ == "__main__":
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
args = parser.parse_args()
|
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":
|
if args.job == "stats":
|
||||||
LOG.d("Compute Stats")
|
LOG.d("Compute Stats")
|
||||||
stats()
|
stats()
|
||||||
|
|
|
@ -150,19 +150,6 @@ if NEWRELIC_CONFIG_PATH:
|
||||||
newrelic_app = newrelic.agent.register_application()
|
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:
|
def get_or_create_contact(from_header: str, mail_from: str, alias: Alias) -> Contact:
|
||||||
"""
|
"""
|
||||||
contact_from_header is the RFC 2047 format FROM header
|
contact_from_header is the RFC 2047 format FROM header
|
||||||
|
@ -2059,8 +2046,7 @@ class MailHandler:
|
||||||
envelope.rcpt_tos,
|
envelope.rcpt_tos,
|
||||||
)
|
)
|
||||||
|
|
||||||
app = new_app()
|
with create_light_app().app_context():
|
||||||
with app.app_context():
|
|
||||||
ret = handle(envelope)
|
ret = handle(envelope)
|
||||||
elapsed = time.time() - start
|
elapsed = time.time() - start
|
||||||
LOG.i(
|
LOG.i(
|
||||||
|
|
|
@ -3,6 +3,7 @@ from app.db import Session
|
||||||
from app.log import LOG
|
from app.log import LOG
|
||||||
from app.models import Mailbox, Contact, SLDomain
|
from app.models import Mailbox, Contact, SLDomain
|
||||||
from app.pgp_utils import load_public_key
|
from app.pgp_utils import load_public_key
|
||||||
|
from server import create_light_app
|
||||||
|
|
||||||
|
|
||||||
def load_pgp_public_keys():
|
def load_pgp_public_keys():
|
||||||
|
@ -50,5 +51,7 @@ def add_sl_domains():
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
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()
|
load_pgp_public_keys()
|
||||||
add_sl_domains()
|
add_sl_domains()
|
||||||
|
|
|
@ -26,19 +26,6 @@ from app.models import User, Job, BatchImport, Mailbox, CustomDomain
|
||||||
from server import create_light_app
|
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):
|
def onboarding_send_from_alias(user):
|
||||||
to_email, unsubscribe_link, via_email = user.get_communication_email()
|
to_email, unsubscribe_link, via_email = user.get_communication_email()
|
||||||
if not to_email:
|
if not to_email:
|
||||||
|
@ -101,6 +88,8 @@ def onboarding_mailbox(user):
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
while True:
|
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 ...
|
# run a job 1h earlier or later is not a big deal ...
|
||||||
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)
|
||||||
|
|
|
@ -131,6 +131,10 @@ def create_light_app() -> Flask:
|
||||||
app.config["SQLALCHEMY_DATABASE_URI"] = DB_URI
|
app.config["SQLALCHEMY_DATABASE_URI"] = DB_URI
|
||||||
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
|
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
|
||||||
|
|
||||||
|
@app.teardown_appcontext
|
||||||
|
def shutdown_session(response_or_exc):
|
||||||
|
Session.remove()
|
||||||
|
|
||||||
return app
|
return app
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue