Add cronjob to delete refused emails

This commit is contained in:
Son NK 2020-03-15 11:11:16 +01:00
parent a923d9ad6a
commit 71a9fc38a9
3 changed files with 35 additions and 0 deletions

View file

@ -53,3 +53,11 @@ def get_url(key: str, expires_in=3600) -> str:
ClientMethod="get_object", ClientMethod="get_object",
Params={"Bucket": BUCKET, "Key": key}, Params={"Bucket": BUCKET, "Key": key},
) )
def delete(path: str):
if LOCAL_FILE_UPLOAD:
os.remove(os.path.join(UPLOAD_DIR, path))
else:
o = _session.resource("s3").Bucket(BUCKET).Object(path)
o.delete()

21
cron.py
View file

@ -2,6 +2,7 @@ import argparse
import arrow import arrow
from app import s3
from app.config import IGNORED_EMAILS, ADMIN_EMAIL from app.config import IGNORED_EMAILS, ADMIN_EMAIL
from app.email_utils import send_email, send_trial_end_soon_email, render from app.email_utils import send_email, send_trial_end_soon_email, render
from app.extensions import db from app.extensions import db
@ -15,6 +16,7 @@ from app.models import (
CustomDomain, CustomDomain,
Client, Client,
ManualSubscription, ManualSubscription,
RefusedEmail,
) )
from server import create_app from server import create_app
@ -30,6 +32,21 @@ def notify_trial_end():
send_trial_end_soon_email(user) send_trial_end_soon_email(user)
def delete_refused_emails():
for refused_email in RefusedEmail.query.filter(RefusedEmail.deleted == False).all():
if arrow.now().shift(days=1) > refused_email.deleted_at >= arrow.now():
LOG.d("Delete refused email %s", refused_email)
s3.delete(refused_email.path)
s3.delete(refused_email.full_report_path)
# do not set path and full_report_path to null
# so we can check later that the files are indeed deleted
refused_email.deleted = True
db.session.commit()
LOG.d("Finish delete_refused_emails")
def notify_premium_end(): def notify_premium_end():
"""sent to user who has canceled their subscription and who has their subscription ending soon""" """sent to user who has canceled their subscription and who has their subscription ending soon"""
for sub in Subscription.query.filter(Subscription.cancelled == True).all(): for sub in Subscription.query.filter(Subscription.cancelled == True).all():
@ -172,6 +189,7 @@ if __name__ == "__main__":
"notify_trial_end", "notify_trial_end",
"notify_manual_subscription_end", "notify_manual_subscription_end",
"notify_premium_end", "notify_premium_end",
"delete_refused_emails"
], ],
) )
args = parser.parse_args() args = parser.parse_args()
@ -191,3 +209,6 @@ if __name__ == "__main__":
elif args.job == "notify_premium_end": elif args.job == "notify_premium_end":
LOG.d("Notify users with premium ending soon") LOG.d("Notify users with premium ending soon")
notify_premium_end() notify_premium_end()
elif args.job == "delete_refused_emails":
LOG.d("Deleted refused emails")
delete_refused_emails()

View file

@ -22,3 +22,9 @@ jobs:
shell: /bin/bash shell: /bin/bash
schedule: "0 10 * * *" schedule: "0 10 * * *"
captureStderr: true captureStderr: true
- name: SimpleLogin Delete Refused Emails
command: python /code/cron.py -j delete_refused_emails
shell: /bin/bash
schedule: "0 11 * * *"
captureStderr: true