Skip to content

Commit

Permalink
Add pip Nexus cleanup task
Browse files Browse the repository at this point in the history
Remove temporary Nexus data for stale or failed requests that used "pip"
as a package manager.

Signed-off-by: Athos Ribeiro <athos@redhat.com>
  • Loading branch information
Athos Ribeiro authored and athos-ribeiro committed Aug 28, 2020
1 parent 8be43c9 commit 925bcf6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
20 changes: 12 additions & 8 deletions cachito/web/api_v1.py
Expand Up @@ -336,14 +336,16 @@ def patch_request(request_id):
request = Request.query.get_or_404(request_id)
delete_bundle = False
delete_bundle_temp = False
cleanup_nexus = False
cleanup_nexus = []
delete_logs = False
if "state" in payload and "state_reason" in payload:
new_state = payload["state"]
delete_bundle = new_state == "stale" and request.state.state_name != "failed"
cleanup_nexus = new_state in ("stale", "failed") and any(
p.name == "npm" for p in request.pkg_managers
)
if new_state in ("stale", "failed"):
if any(p.name == "npm" for p in request.pkg_managers):
cleanup_nexus.append("npm")
if any(p.name == "pip" for p in request.pkg_managers):
cleanup_nexus.append("pip")
delete_bundle_temp = new_state in ("complete", "failed")
delete_logs = new_state == "stale"
new_state_reason = payload["state_reason"]
Expand Down Expand Up @@ -413,16 +415,18 @@ def patch_request(request_id):
except: # noqa E722
flask.current_app.logger.exception("Failed to delete the log file %s", path_to_file)

if cleanup_nexus:
for pkg_mgr in cleanup_nexus:
flask.current_app.logger.info(
"Cleaning up the Nexus npm content for request %d", request_id
"Cleaning up the Nexus %s content for request %d", pkg_mgr, request_id
)
cleanup_task = getattr(tasks, f"cleanup_{pkg_mgr}_request")
try:
tasks.cleanup_npm_request.delay(request_id)
cleanup_task.delay(request_id)
except kombu.exceptions.OperationalError:
flask.current_app.logger.exception(
"Failed to schedule the cleanup_npm_request task for request %d. An administrator "
"Failed to schedule the cleanup_%s_request task for request %d. An administrator "
"must clean this up manually.",
pkg_mgr,
request.id,
)

Expand Down
19 changes: 19 additions & 0 deletions cachito/workers/tasks/pip.py
@@ -0,0 +1,19 @@
# SPDX-License-Identifier: GPL-3.0-or-later
from cachito.workers import nexus
from cachito.workers.pkg_managers.pip import (
get_pypi_hosted_repo_name,
get_raw_hosted_repo_name,
get_hosted_repositories_username,
)
from cachito.workers.tasks.celery import app


@app.task
def cleanup_pip_request(request_id):
"""Clean up the Nexus Python content for the Cachito request."""
payload = {
"pip_repository_name": get_pypi_hosted_repo_name(request_id),
"raw_repository_name": get_raw_hosted_repo_name(request_id),
"username": get_hosted_repositories_username(request_id),
}
nexus.execute_script("pip_cleanup", payload)
16 changes: 16 additions & 0 deletions tests/test_workers/test_tasks/test_pip.py
@@ -0,0 +1,16 @@
# SPDX-License-Identifier: GPL-3.0-or-later
from unittest import mock

from cachito.workers.tasks import pip


@mock.patch("cachito.workers.tasks.pip.nexus.execute_script")
def test_cleanup_pip_request(mock_exec_script):
pip.cleanup_pip_request(42)

expected_payload = {
"pip_repository_name": "cachito-pip-hosted-42",
"raw_repository_name": "cachito-pip-raw-42",
"username": "cachito-pip-42",
}
mock_exec_script.assert_called_once_with("pip_cleanup", expected_payload)

0 comments on commit 925bcf6

Please sign in to comment.