-
-
Notifications
You must be signed in to change notification settings - Fork 404
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Cleanup tasks * Update * Added tests * Create cron job * Delete old data cron * Fix import * import fix * Added delete + script to disable pgp for proton mboxes
- Loading branch information
Showing
13 changed files
with
262 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import arrow | ||
|
||
from app import s3 | ||
from app.log import LOG | ||
from app.models import BatchImport | ||
|
||
|
||
def cleanup_old_imports(oldest_allowed: arrow.Arrow): | ||
LOG.i(f"Deleting imports older than {oldest_allowed}") | ||
for batch_import in ( | ||
BatchImport.filter(BatchImport.created_at < oldest_allowed).yield_per(500).all() | ||
): | ||
LOG.i( | ||
f"Deleting batch import {batch_import} with file {batch_import.file.path}" | ||
) | ||
file = batch_import.file | ||
if file is not None: | ||
s3.delete(file.path) | ||
BatchImport.delete(batch_import.id, commit=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import arrow | ||
from sqlalchemy import or_, and_ | ||
|
||
from app import config | ||
from app.db import Session | ||
from app.log import LOG | ||
from app.models import Job, JobState | ||
|
||
|
||
def cleanup_old_jobs(oldest_allowed: arrow.Arrow): | ||
LOG.i(f"Deleting jobs older than {oldest_allowed}") | ||
count = Job.filter( | ||
or_( | ||
Job.state == JobState.done.value, | ||
Job.state == JobState.error.value, | ||
and_( | ||
Job.state == JobState.taken.value, | ||
Job.attempts >= config.JOB_MAX_ATTEMPTS, | ||
), | ||
), | ||
Job.updated_at < oldest_allowed, | ||
).delete() | ||
Session.commit() | ||
LOG.i(f"Deleted {count} jobs") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import arrow | ||
|
||
from app.db import Session | ||
from app.log import LOG | ||
from app.models import Notification | ||
|
||
|
||
def cleanup_old_notifications(oldest_allowed: arrow.Arrow): | ||
LOG.i(f"Deleting notifications older than {oldest_allowed}") | ||
count = Notification.filter(Notification.created_at < oldest_allowed).delete() | ||
Session.commit() | ||
LOG.i(f"Deleted {count} notifications") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import tempfile | ||
from io import BytesIO | ||
|
||
import arrow | ||
|
||
from app import s3, config | ||
from app.models import File, BatchImport | ||
from tasks.cleanup_old_imports import cleanup_old_imports | ||
from tests.utils import random_token, create_new_user | ||
|
||
|
||
def test_cleanup_old_imports(): | ||
BatchImport.filter().delete() | ||
with tempfile.TemporaryDirectory() as tmpdir: | ||
config.UPLOAD_DIR = tmpdir | ||
user = create_new_user() | ||
path = random_token() | ||
s3.upload_from_bytesio(path, BytesIO("data".encode("utf-8"))) | ||
file = File.create(path=path, commit=True) # noqa: F821 | ||
now = arrow.now() | ||
delete_batch_import_id = BatchImport.create( | ||
user_id=user.id, | ||
file_id=file.id, | ||
created_at=now.shift(minutes=-1), | ||
flush=True, | ||
).id | ||
keep_batch_import_id = BatchImport.create( | ||
user_id=user.id, | ||
file_id=file.id, | ||
created_at=now.shift(minutes=+1), | ||
commit=True, | ||
).id | ||
cleanup_old_imports(now) | ||
assert BatchImport.get(id=delete_batch_import_id) is None | ||
assert BatchImport.get(id=keep_batch_import_id) is not None |
Oops, something went wrong.