Skip to content

Commit

Permalink
Add Celery task for file cleanup after fetch job
Browse files Browse the repository at this point in the history
Adds a small Celery task to delete the directory in which files related
to a fetch job are stored.
  • Loading branch information
mcantelon committed Dec 19, 2023
1 parent a16acd8 commit 8235dcc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions AIPscan/Aggregator/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,16 @@ def package_lists_request(self, storage_service_id, timestamp, packages_director
}


@celery.task()
def fetch_job_file_cleanup(fetch_job_id):
obj = FetchJob.query.filter_by(id=fetch_job_id).first()

if os.path.isdir(obj.download_directory):
shutil.rmtree(obj.download_directory)

logger.info("Cleaned up after fetch job {}".format(fetch_job_id))


@celery.task()
def get_mets(
package_uuid,
Expand Down
17 changes: 17 additions & 0 deletions AIPscan/Aggregator/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
delete_aip,
delete_fetch_job,
delete_storage_service,
fetch_job_file_cleanup,
get_mets,
make_request,
parse_package_list_file,
Expand Down Expand Up @@ -212,6 +213,22 @@ def test_delete_storage_service_task(app_instance, tmpdir, mocker):
assert Agent.query.filter_by(storage_service_id=storage_service.id).count() == 0


def test_fetch_job_file_cleanup_task(app_instance, tmpdir, mocker):
test_downloads_dir = os.path.join(tmpdir, "downloads")
os.mkdir(test_downloads_dir)

# Mock download directory should be created
assert os.path.isdir(test_downloads_dir)

fetch_job1 = test_helpers.create_test_fetch_job(
download_directory=test_downloads_dir
)
fetch_job_file_cleanup(fetch_job1.id)

# Mock download directory should be removed
assert not os.path.isdir(test_downloads_dir)


@pytest.mark.parametrize(
"response, raises_task_error",
[
Expand Down
3 changes: 3 additions & 0 deletions AIPscan/Aggregator/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ def get_mets_task_status(coordinatorid):
downloadStart = _format_date(start)
obj.download_end = downloadEnd
db.session.commit()

tasks.fetch_job_file_cleanup.delay(fetchJobId)

Check warning on line 291 in AIPscan/Aggregator/views.py

View check run for this annotation

Codecov / codecov/patch

AIPscan/Aggregator/views.py#L291

Added line #L291 was not covered by tests

response = {"state": "COMPLETED"}
flash("Fetch Job {} completed".format(downloadStart))
return jsonify(response)

0 comments on commit 8235dcc

Please sign in to comment.