Skip to content
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

Add Celery task for file cleanup after fetch job #187

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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)
Loading