diff --git a/AIPscan/Aggregator/tasks.py b/AIPscan/Aggregator/tasks.py index b52e579c..6344d9eb 100644 --- a/AIPscan/Aggregator/tasks.py +++ b/AIPscan/Aggregator/tasks.py @@ -114,6 +114,10 @@ def parse_packages_and_load_mets( for package_obj in package_list.get(OBJECTS, []): package = process_package_object(package_obj) packages.append(package) + + if package.is_deleted(): + delete_aip(package.uuid) + if not package.is_aip(): continue start_mets_task( @@ -131,6 +135,16 @@ def parse_packages_and_load_mets( return packages +def delete_aip(uuid): + logger.warning("Package deleted from SS: '%s'", uuid) + + deleted_aip = AIP.query.filter_by(uuid=uuid).first() + + if deleted_aip is not None: + logger.warning("Deleting AIP: %s", uuid) + database_helpers.delete_aip_object(deleted_aip) + + @celery.task(bind=True) def workflow_coordinator( self, api_url, timestamp, storage_service_id, fetch_job_id, packages_directory diff --git a/AIPscan/Aggregator/tests/test_tasks.py b/AIPscan/Aggregator/tests/test_tasks.py index f18080bc..23f0e14e 100644 --- a/AIPscan/Aggregator/tests/test_tasks.py +++ b/AIPscan/Aggregator/tests/test_tasks.py @@ -1,6 +1,7 @@ import json import logging import os +import uuid from datetime import datetime from io import StringIO @@ -9,6 +10,7 @@ from AIPscan import test_helpers from AIPscan.Aggregator.tasks import ( TaskError, + delete_aip, get_mets, make_request, parse_packages_and_load_mets, @@ -208,3 +210,18 @@ def test_parse_packages_and_load_mets(app_instance, tmpdir, mocker): parse_packages_and_load_mets(json_file_path, {}, str(datetime.now()), 1, 1, 1) delete_package_json.assert_called_with(json_file_path) + + +def test_delete_aip(app_instance): + """Test that SS deleted AIPs gets deleted in aipscan.db.""" + PACKAGE_UUID = str(uuid.uuid4()) + + test_helpers.create_test_aip(uuid=PACKAGE_UUID) + + deleted_aip = AIP.query.filter_by(uuid=PACKAGE_UUID).first() + assert deleted_aip is not None + + delete_aip(PACKAGE_UUID) + + deleted_aip = AIP.query.filter_by(uuid=PACKAGE_UUID).first() + assert deleted_aip is None