-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Delete expired files with management command #518
Merged
rachaelcodes
merged 3 commits into
main
from
feature/REDBOX-234-deletion-management-command
Jun 5, 2024
+185
−8
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this could be part of the
File.delete
method 🤔 , probably doesnt add much and just over-complicates things?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We currently have a File.delete that actually deletes the file, whereas this just removes the file contents but keeps a record of the File in Django (for auditing processes). I think this was a product decision a few weeks ago.