Skip to content

Commit

Permalink
Storage service deletion Celery task. (#204) (#205)
Browse files Browse the repository at this point in the history
Move storage service deletion logic into a Celery task to avoid timeouts.
  • Loading branch information
mcantelon authored Sep 7, 2023
1 parent 172c7a1 commit a315d91
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
15 changes: 14 additions & 1 deletion AIPscan/Aggregator/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
)
from AIPscan.extensions import celery
from AIPscan.helpers import file_sha256_hash
from AIPscan.models import AIP, FetchJob, get_mets_tasks # Custom celery Models.
from AIPscan.models import AIP, FetchJob, StorageService, get_mets_tasks

logger = get_task_logger(__name__)

Expand Down Expand Up @@ -383,3 +383,16 @@ def delete_fetch_job(fetch_job_id):
shutil.rmtree(fetch_job.download_directory)
db.session.delete(fetch_job)
db.session.commit()


@celery.task()
def delete_storage_service(storage_service_id):
storage_service = StorageService.query.get(storage_service_id)
mets_fetch_jobs = FetchJob.query.filter_by(
storage_service_id=storage_service_id
).all()
for mets_fetch_job in mets_fetch_jobs:
if os.path.exists(mets_fetch_job.download_directory):
shutil.rmtree(mets_fetch_job.download_directory)
db.session.delete(storage_service)
db.session.commit()
16 changes: 15 additions & 1 deletion AIPscan/Aggregator/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
TaskError,
delete_aip,
delete_fetch_job,
delete_storage_service,
get_mets,
make_request,
parse_packages_and_load_mets,
Expand All @@ -24,7 +25,7 @@
VALID_JSON,
MockResponse,
)
from AIPscan.models import AIP, FetchJob
from AIPscan.models import AIP, FetchJob, StorageService

SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
FIXTURES_DIR = os.path.join(SCRIPT_DIR, "fixtures")
Expand Down Expand Up @@ -194,6 +195,19 @@ def test_delete_fetch_job_task(app_instance, tmpdir, mocker):
assert FetchJob.query.filter_by(id=fetch_job1.id).first() is None


def test_delete_storage_service_task(app_instance, tmpdir, mocker):
"""Test that storage service gets deleted by delete storage service job task logic."""
storage_service = test_helpers.create_test_storage_service()

deleted_ss = StorageService.query.filter_by(id=storage_service.id).first()
assert deleted_ss is not None

delete_storage_service(storage_service.id)

deleted_ss = StorageService.query.filter_by(id=storage_service.id).first()
assert deleted_ss is None


@pytest.mark.parametrize(
"response, raises_task_error",
[
Expand Down
10 changes: 2 additions & 8 deletions AIPscan/Aggregator/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-

import os
import shutil
from datetime import datetime

from celery.result import AsyncResult
Expand Down Expand Up @@ -151,14 +150,9 @@ def new_storage_service():

@aggregator.route("/delete_storage_service/<id>", methods=["GET"])
def delete_storage_service(id):
tasks.delete_storage_service.delay(id)
storage_service = StorageService.query.get(id)
mets_fetch_jobs = FetchJob.query.filter_by(storage_service_id=id).all()
for mets_fetch_job in mets_fetch_jobs:
if os.path.exists(mets_fetch_job.download_directory):
shutil.rmtree(mets_fetch_job.download_directory)
db.session.delete(storage_service)
db.session.commit()
flash("Storage service '{}' is deleted".format(storage_service.name))
flash("Storage service '{}' is being deleted".format(storage_service.name))
return redirect(url_for("aggregator.storage_services"))


Expand Down

0 comments on commit a315d91

Please sign in to comment.