2020-02-03 07:11:11 +01:00
|
|
|
"""
|
|
|
|
Run scheduled jobs.
|
|
|
|
Not meant for running job at precise time (+- 1h)
|
|
|
|
"""
|
|
|
|
import time
|
|
|
|
|
|
|
|
import arrow
|
|
|
|
|
2020-03-24 21:19:45 +01:00
|
|
|
from app.config import JOB_ONBOARDING_1, JOB_ONBOARDING_2, JOB_ONBOARDING_3
|
2020-03-05 11:00:58 +01:00
|
|
|
from app.email_utils import send_email, render
|
2020-02-03 07:11:11 +01:00
|
|
|
from app.extensions import db
|
|
|
|
from app.log import LOG
|
2020-03-05 11:00:58 +01:00
|
|
|
from app.models import User, Job
|
2020-02-03 07:11:11 +01:00
|
|
|
from server import create_app
|
|
|
|
|
|
|
|
|
|
|
|
# fix the database connection leak issue
|
|
|
|
# use this method instead of create_app
|
|
|
|
def new_app():
|
|
|
|
app = create_app()
|
|
|
|
|
|
|
|
@app.teardown_appcontext
|
|
|
|
def shutdown_session(response_or_exc):
|
|
|
|
# same as shutdown_session() in flask-sqlalchemy but this is not enough
|
|
|
|
db.session.remove()
|
|
|
|
|
|
|
|
# dispose the engine too
|
|
|
|
db.engine.dispose()
|
|
|
|
|
|
|
|
return app
|
|
|
|
|
|
|
|
|
2020-03-24 21:01:38 +01:00
|
|
|
def onboarding_send_from_alias(user):
|
2020-02-03 07:11:11 +01:00
|
|
|
send_email(
|
|
|
|
user.email,
|
|
|
|
f"Do you know you can send emails to anyone from your alias?",
|
2020-03-24 20:55:50 +01:00
|
|
|
render("com/onboarding/send-from-alias.txt", user=user),
|
|
|
|
render("com/onboarding/send-from-alias.html", user=user),
|
2020-02-03 07:11:11 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-03-24 21:01:38 +01:00
|
|
|
def onboarding_pgp(user):
|
|
|
|
send_email(
|
|
|
|
user.email,
|
2020-03-24 21:19:45 +01:00
|
|
|
f"Do you know you can encrypt your emails so only you can read them?",
|
2020-03-24 21:01:38 +01:00
|
|
|
render("com/onboarding/pgp.txt", user=user),
|
|
|
|
render("com/onboarding/pgp.html", user=user),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-03-24 21:19:45 +01:00
|
|
|
def onboarding_mailbox(user):
|
|
|
|
send_email(
|
|
|
|
user.email,
|
|
|
|
f"Do you know SimpleLogin can manage several email addresses?",
|
|
|
|
render("com/onboarding/mailbox.txt", user=user),
|
|
|
|
render("com/onboarding/mailbox.html", user=user),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-02-03 07:11:11 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
while True:
|
|
|
|
# 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)
|
|
|
|
|
|
|
|
app = new_app()
|
|
|
|
|
|
|
|
with app.app_context():
|
|
|
|
for job in Job.query.filter(
|
|
|
|
Job.taken == 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
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
if job.name == JOB_ONBOARDING_1:
|
|
|
|
user_id = job.payload.get("user_id")
|
|
|
|
user = User.get(user_id)
|
|
|
|
|
2020-02-15 11:10:49 +01:00
|
|
|
# user might delete their account in the meantime
|
2020-03-24 21:01:38 +01:00
|
|
|
# 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:
|
2020-03-24 21:23:26 +01:00
|
|
|
LOG.d("send onboarding mailbox email to user %s", user)
|
|
|
|
onboarding_mailbox(user)
|
2020-03-24 21:19:45 +01:00
|
|
|
elif job.name == JOB_ONBOARDING_3:
|
|
|
|
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:
|
2020-03-24 21:23:26 +01:00
|
|
|
LOG.d("send onboarding pgp email to user %s", user)
|
|
|
|
onboarding_pgp(user)
|
|
|
|
|
2020-02-03 07:11:11 +01:00
|
|
|
else:
|
|
|
|
LOG.error("Unknown job name %s", job.name)
|
|
|
|
|
|
|
|
time.sleep(10)
|