-
Notifications
You must be signed in to change notification settings - Fork 2
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 #451 from geoadmin/feat-PB-848-delete-expired-items
PB-848: Delete expired items
- Loading branch information
Showing
8 changed files
with
256 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from datetime import timedelta | ||
|
||
from django.conf import settings | ||
from django.core.management.base import CommandParser | ||
from django.utils import timezone | ||
|
||
from stac_api.models import Item | ||
from stac_api.utils import CommandHandler | ||
from stac_api.utils import CustomBaseCommand | ||
|
||
|
||
class Handler(CommandHandler): | ||
|
||
def delete(self, instance, object_type): | ||
if self.options['dry_run']: | ||
self.print_success(f'skipping deletion of {object_type} {instance}') | ||
else: | ||
instance.delete() | ||
|
||
def run(self): | ||
self.print_success('running command to remove expired items') | ||
min_age_hours = self.options['min_age_hours'] | ||
self.print_warning(f"deleting all items expired longer than {min_age_hours} hours") | ||
items = Item.objects.filter( | ||
properties_expires__lte=timezone.now() - timedelta(hours=min_age_hours) | ||
).all() | ||
for item in items: | ||
assets = item.assets.all() | ||
assets_length = len(assets) | ||
self.delete(assets, 'assets') | ||
self.delete(item, 'item') | ||
if not self.options['dry_run']: | ||
self.print_success( | ||
f"deleted item {item.name} and {assets_length}" + " assets belonging to it.", | ||
extra={"item": item.name} | ||
) | ||
|
||
if self.options['dry_run']: | ||
self.print_success(f'[dry run] would have removed {len(items)} expired items') | ||
else: | ||
self.print_success(f'successfully removed {len(items)} expired items') | ||
|
||
|
||
class Command(CustomBaseCommand): | ||
help = """Remove items and their assets that have expired more than | ||
DELETE_EXPIRED_ITEMS_OLDER_THAN_HOURS hours ago. | ||
This command is thought to be scheduled as cron job. | ||
""" | ||
|
||
def add_arguments(self, parser: CommandParser) -> None: | ||
super().add_arguments(parser) | ||
parser.add_argument( | ||
'--dry-run', | ||
action='store_true', | ||
help='Simulate deleting items, without actually deleting them' | ||
) | ||
default_min_age = settings.DELETE_EXPIRED_ITEMS_OLDER_THAN_HOURS | ||
parser.add_argument( | ||
'--min-age-hours', | ||
type=int, | ||
default=default_min_age, | ||
help=f"Minimum hours the item must have been expired for (default {default_min_age})" | ||
) | ||
|
||
def handle(self, *args, **options): | ||
Handler(self, options).run() |
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
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,121 @@ | ||
from datetime import timedelta | ||
from io import StringIO | ||
|
||
from django.core.management import call_command | ||
from django.test import TestCase | ||
from django.utils import timezone | ||
|
||
from stac_api.models import Asset | ||
from stac_api.models import Item | ||
|
||
from tests.tests_10.data_factory import Factory | ||
from tests.utils import mock_s3_asset_file | ||
|
||
|
||
class RemoveExpiredItems(TestCase): | ||
|
||
@classmethod | ||
def setUpTestData(cls): | ||
cls.factory = Factory() | ||
cls.collection = cls.factory.create_collection_sample().model | ||
|
||
def _call_command(self, *args, **kwargs): | ||
out = StringIO() | ||
call_command( | ||
"remove_expired_items", | ||
*args, | ||
stdout=out, | ||
stderr=StringIO(), | ||
**kwargs, | ||
) | ||
return out.getvalue() | ||
|
||
@mock_s3_asset_file | ||
def test_remove_item_dry_run(self): | ||
item_0 = self.factory.create_item_sample( | ||
self.collection, | ||
name='item-0', | ||
db_create=True, | ||
properties_expires=timezone.now() - timedelta(hours=50) | ||
) | ||
assets = self.factory.create_asset_samples( | ||
2, item_0.model, name=['asset-0.tiff', 'asset-1.tiff'], db_create=True | ||
) | ||
|
||
out = self._call_command("--dry-run", "--no-color") | ||
self.assertEqual( | ||
out, | ||
"""running command to remove expired items | ||
deleting all items expired longer than 24 hours | ||
skipping deletion of assets <QuerySet [<Asset: asset-0.tiff>, <Asset: asset-1.tiff>]> | ||
skipping deletion of item collection-1/item-0 | ||
[dry run] would have removed 1 expired items | ||
""" | ||
) | ||
|
||
self.assertTrue( | ||
Item.objects.filter(name=item_0['name']).exists(), | ||
msg="Item has been deleted by dry run" | ||
) | ||
self.assertTrue( | ||
Asset.objects.filter(name=assets[0]['name']).exists(), | ||
msg="Asset has been deleted by dry run" | ||
) | ||
self.assertTrue( | ||
Asset.objects.filter(name=assets[1]['name']).exists(), | ||
msg="Asset has been deleted by dry run" | ||
) | ||
|
||
@mock_s3_asset_file | ||
def test_remove_item(self): | ||
item_1 = self.factory.create_item_sample( | ||
self.collection, | ||
name='item-1', | ||
db_create=True, | ||
properties_expires=timezone.now() - timedelta(hours=10) | ||
) | ||
assets = self.factory.create_asset_samples( | ||
2, item_1.model, name=['asset-2.tiff', 'asset-3.tiff'], db_create=True | ||
) | ||
out = self._call_command("--no-color") | ||
self.assertEqual( | ||
out, | ||
"""running command to remove expired items | ||
deleting all items expired longer than 24 hours | ||
successfully removed 0 expired items | ||
""" | ||
) | ||
|
||
self.assertTrue( | ||
Item.objects.filter(name=item_1['name']).exists(), | ||
msg="not expired item has been deleted" | ||
) | ||
self.assertTrue( | ||
Asset.objects.filter(name=assets[0]['name']).exists(), | ||
msg="not expired asset has been deleted" | ||
) | ||
self.assertTrue( | ||
Asset.objects.filter(name=assets[1]['name']).exists(), | ||
msg="not expired asset has been deleted" | ||
) | ||
|
||
out = self._call_command("--min-age-hours=9", "--no-color") | ||
self.assertEqual( | ||
out, | ||
"""running command to remove expired items | ||
deleting all items expired longer than 9 hours | ||
deleted item item-1 and 2 assets belonging to it. extra={'item': 'item-1'} | ||
successfully removed 1 expired items | ||
""" | ||
) | ||
self.assertFalse( | ||
Item.objects.filter(name=item_1['name']).exists(), msg="Expired item was not deleted" | ||
) | ||
self.assertFalse( | ||
Asset.objects.filter(name=assets[0]['name']).exists(), | ||
msg="Asset of expired item was not deleted" | ||
) | ||
self.assertFalse( | ||
Asset.objects.filter(name=assets[1]['name']).exists(), | ||
msg="Asset of expired item was not deleted" | ||
) |
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