-
-
Notifications
You must be signed in to change notification settings - Fork 205
feat(exports): add celery task to delete old anonymous exports DEV-1358 #6509
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
Open
rajpatel24
wants to merge
2
commits into
release/2.025.43
Choose a base branch
from
dev-1358-erase-anonymous-exports
base: release/2.025.43
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+159
−1
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
This file contains hidden or 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 hidden or 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 hidden or 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,116 @@ | ||
| import os | ||
| from datetime import timedelta | ||
|
|
||
| from django.core.cache import cache | ||
| from django.core.files.base import ContentFile | ||
| from django.utils import timezone | ||
| from django.test import TestCase | ||
|
|
||
| from kpi.models.import_export_task import ( | ||
| ImportExportStatusChoices, | ||
| SubmissionExportTask | ||
| ) | ||
| from kpi.tasks import cleanup_anonymous_exports | ||
| from kpi.utils.object_permission import get_anonymous_user | ||
|
|
||
|
|
||
| class AnonymousExportCleanupTestCase(TestCase): | ||
| def _create_export_task( | ||
| self, status=ImportExportStatusChoices.COMPLETE, minutes_old=60 | ||
| ): | ||
| export = SubmissionExportTask() | ||
| export.user = get_anonymous_user() | ||
| export.status = status | ||
| export.data = {'type': 'xls', 'source': 'test'} | ||
| export.save() | ||
|
|
||
| if minutes_old > 0: | ||
| past_time = timezone.now() - timedelta(minutes=minutes_old) | ||
| SubmissionExportTask.objects.filter(uid=export.uid).update( | ||
| date_created=past_time | ||
| ) | ||
| export.refresh_from_db() | ||
| return export | ||
|
|
||
| def test_exports_older_than_30_minutes_are_deleted(self): | ||
| # Export older than 30 min - should be deleted | ||
| old_export = self._create_export_task(minutes_old=31) | ||
|
|
||
| # Export newer than 30 min - should be kept | ||
| recent_export = self._create_export_task(minutes_old=29) | ||
|
|
||
| cleanup_anonymous_exports() | ||
| self.assertFalse( | ||
| SubmissionExportTask.objects.filter(uid=old_export.uid).exists() | ||
| ) | ||
| self.assertTrue( | ||
| SubmissionExportTask.objects.filter(uid=recent_export.uid).exists() | ||
| ) | ||
|
|
||
| def test_export_result_file_is_deleted_from_storage(self): | ||
| """ | ||
| Test that export files are deleted from storage | ||
| """ | ||
| export = self._create_export_task(minutes_old=60) | ||
|
|
||
| # Create actual file in storage | ||
| file_content = ContentFile( | ||
| b'PK\x03\x04' + | ||
| b'{"data": "export"}' * 100, | ||
| name='test_export.xlsx' | ||
| ) | ||
| export.result.save(f'test_export_{export.uid}.xlsx', file_content, save=True) | ||
| export.refresh_from_db() | ||
|
|
||
| storage = export.result.storage | ||
| file_path = storage.path(export.result.name) | ||
| self.assertTrue(os.path.exists(file_path)) | ||
| self.assertTrue(SubmissionExportTask.objects.filter(uid=export.uid).exists()) | ||
|
|
||
| cleanup_anonymous_exports() | ||
|
|
||
| self.assertFalse(os.path.exists(file_path)) | ||
| self.assertFalse(SubmissionExportTask.objects.filter(uid=export.uid).exists()) | ||
|
|
||
| def test_processing_exports_are_not_deleted(self): | ||
| """ | ||
| Test that exports with PROCESSING status are never deleted | ||
| """ | ||
| processing_export = self._create_export_task( | ||
| status=ImportExportStatusChoices.PROCESSING, | ||
| minutes_old=100 | ||
| ) | ||
|
|
||
| cleanup_anonymous_exports() | ||
| self.assertTrue( | ||
| SubmissionExportTask.objects.filter( | ||
| uid=processing_export.uid | ||
| ).exists() | ||
| ) | ||
|
|
||
| def test_cache_lock_prevents_concurrent_execution(self): | ||
| """ | ||
| Test that cache lock prevents concurrent task execution | ||
| """ | ||
| for i in range(5): | ||
| self._create_export_task(minutes_old=60) | ||
|
|
||
| cache_key = 'cleanup_anonymous_exports:lock' | ||
| lock_timeout = 15 * 60 | ||
|
|
||
| # Acquire lock manually (simulate first task running) | ||
| lock = cache.lock(cache_key, timeout=lock_timeout + 60) | ||
| lock.acquire(blocking=False) | ||
|
|
||
| try: | ||
| # Task should return early without deleting | ||
| cleanup_anonymous_exports() | ||
|
|
||
| # Verify no exports were deleted | ||
| remaining = SubmissionExportTask.objects.filter( | ||
| user__username='AnonymousUser' | ||
| ).count() | ||
| self.assertEqual(remaining, 5) | ||
|
|
||
| finally: | ||
| lock.release() |
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.
Uh oh!
There was an error while loading. Please reload this page.