-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #518 from i-dot-ai/feature/REDBOX-234-deletion-man…
…agement-command Delete expired files with management command
- Loading branch information
Showing
5 changed files
with
183 additions
and
6 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
django_app/redbox_app/redbox_core/management/commands/delete_expired_data.py
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,53 @@ | ||
import logging | ||
from datetime import timedelta | ||
|
||
from botocore.exceptions import BotoCoreError | ||
from django.conf import settings | ||
from django.core.management import BaseCommand | ||
from django.utils import timezone | ||
from redbox_app.redbox_core.client import CoreApiClient | ||
from redbox_app.redbox_core.models import File, StatusEnum | ||
from requests.exceptions import RequestException | ||
|
||
logger = logging.getLogger(__name__) | ||
core_api = CoreApiClient(host=settings.CORE_API_HOST, port=settings.CORE_API_PORT) | ||
|
||
|
||
class Command(BaseCommand): | ||
help = """This should be run daily per environment to remove expired data. | ||
It removes Files that have exceeded their expiry date. | ||
""" | ||
|
||
def handle(self, *_args, **_kwargs): | ||
cutoff_date = timezone.now() - timedelta(seconds=settings.FILE_EXPIRY_IN_SECONDS) | ||
|
||
self.stdout.write(self.style.NOTICE(f"Deleting Files expired before {cutoff_date}")) | ||
counter = 0 | ||
|
||
for file in File.objects.filter(last_referenced__lt=cutoff_date): | ||
logger.debug( | ||
"Deleting file object %s, last_referenced %s", | ||
file, | ||
file.last_referenced, | ||
) | ||
|
||
try: | ||
core_api.delete_file(file.core_file_uuid, file.user) | ||
file.delete_from_s3() | ||
|
||
except RequestException as e: | ||
logger.exception("Error deleting file object %s using core-api", file, exc_info=e) | ||
file.status = StatusEnum.errored | ||
file.save() | ||
except BotoCoreError as e: | ||
logger.exception("Error deleting file object %s from storage", file, exc_info=e) | ||
file.status = StatusEnum.errored | ||
file.save() | ||
else: | ||
file.status = StatusEnum.deleted | ||
file.save() | ||
logger.debug("File object %s deleted", file) | ||
|
||
counter += 1 | ||
|
||
self.stdout.write(self.style.SUCCESS(f"Successfully deleted {counter} file objects")) |
29 changes: 29 additions & 0 deletions
29
django_app/redbox_app/redbox_core/migrations/0012_alter_file_status.py
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,29 @@ | ||
# Generated by Django 5.0.6 on 2024-06-05 06:46 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("redbox_core", "0011_alter_file_processing_status"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="file", | ||
name="status", | ||
field=models.CharField( | ||
choices=[ | ||
("uploaded", "Uploaded"), | ||
("parsing", "Parsing"), | ||
("chunking", "Chunking"), | ||
("embedding", "Embedding"), | ||
("indexing", "Indexing"), | ||
("complete", "Complete"), | ||
("unknown", "Unknown"), | ||
("deleted", "Deleted"), | ||
("errored", "Errored"), | ||
] | ||
), | ||
), | ||
] |
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