diff --git a/pulp_file/app/tasks/__init__.py b/pulp_file/app/tasks/__init__.py index 359f38bcd..c0972874a 100644 --- a/pulp_file/app/tasks/__init__.py +++ b/pulp_file/app/tasks/__init__.py @@ -1,5 +1,2 @@ from .publishing import publish # noqa from .synchronizing import synchronize # noqa - -# file_acs_refresh must be imported after synchronize to avoid circular import -from .acs import file_acs_refresh # noqa diff --git a/pulp_file/app/tasks/acs.py b/pulp_file/app/tasks/acs.py deleted file mode 100644 index dc5387d32..000000000 --- a/pulp_file/app/tasks/acs.py +++ /dev/null @@ -1,55 +0,0 @@ -import os - -from datetime import datetime - -from pulpcore.plugin.models import ( - AlternateContentSource, - AlternateContentSourcePath, - ProgressReport, - TaskGroup, -) -from pulpcore.plugin.tasking import dispatch - -from pulp_file.app.tasks import synchronize -from pulp_file.app.models import FileRepository - -metadata_files = [] - - -def file_acs_refresh(acs_pk): - """ - Refresh alternative content sources. - """ - acs = AlternateContentSource.objects.get(pk=acs_pk) - acs_paths = AlternateContentSourcePath.objects.filter(alternate_content_source=acs_pk) - with ProgressReport( - message="Refreshing ACS metadata", code="acs.refreshing.metadata", total=len(acs_paths) - ) as pb: - for acs_path in acs_paths: - # Create or get repository for each path - repo_data = { - "name": f"{acs.name}--{acs_path.pk}--repository", - "retain_repo_versions": 1, - "user_hidden": True, - } - repo, created = FileRepository.objects.get_or_create(**repo_data) - if created: - acs_path.repository = repo - acs_path.save() - acs_url = os.path.join(acs.remote.url, acs_path.path) - # Dispatch each ACS path to own task and assign it to common TaskGroup - task_group = TaskGroup.objects.create(description=f"Refresh of {acs_path.path}") - dispatch( - synchronize, - shared_resources=[acs.remote], - exclusive_resources=[acs], - task_group=task_group, - kwargs={ - "remote_pk": str(acs.remote.pk), - "repository_pk": str(acs_path.repository.pk), - "mirror": False, - "url": acs_url, - }, - ) - pb.increment() - acs.last_refreshed = datetime.now() diff --git a/pulp_file/app/viewsets.py b/pulp_file/app/viewsets.py index e7f66b2b8..ebc7e2d44 100644 --- a/pulp_file/app/viewsets.py +++ b/pulp_file/app/viewsets.py @@ -1,8 +1,16 @@ +import os + from django_filters import CharFilter from drf_spectacular.utils import extend_schema from rest_framework.decorators import action from pulpcore.plugin.actions import ModifyRepositoryActionMixin +from pulpcore.plugin.models import ( + AlternateContentSource, + AlternateContentSourcePath, + CreatedResource, + TaskGroup, +) from pulpcore.plugin.serializers import ( AsyncOperationResponseSerializer, RepositorySyncURLSerializer, @@ -18,6 +26,7 @@ RepositoryViewSet, RepositoryVersionViewSet, SingleArtifactContentUploadViewSet, + TaskGroupResponse, ) from . import tasks @@ -204,12 +213,39 @@ def refresh(self, request, pk): """ Refresh ACS metadata. """ - acs = self.get_object() - result = dispatch( - tasks.file_acs_refresh, - [acs], - kwargs={ - "acs_pk": pk, - }, + acs = AlternateContentSource.objects.get(pk=pk) + acs_paths = AlternateContentSourcePath.objects.filter(alternate_content_source=pk) + task_group = TaskGroup.objects.create( + description=f"Refreshing {acs_paths.count()} alternate content source paths." ) - return OperationPostponedResponse(result, request) + + for acs_path in acs_paths: + # Create or get repository for the path + repo_data = { + "name": f"{acs.name}--{acs_path.pk}--repository", + "retain_repo_versions": 1, + "user_hidden": True, + } + repo, created = FileRepository.objects.get_or_create(**repo_data) + if created: + acs_path.repository = repo + acs_path.save() + acs_url = os.path.join(acs.remote.url, acs_path.path) + + # Dispatching ACS path to own task and assign it to common TaskGroup + task = dispatch( + tasks.synchronize, + shared_resources=[acs.remote], + exclusive_resources=[acs], + task_group=task_group, + kwargs={ + "remote_pk": str(acs.remote.pk), + "repository_pk": str(acs_path.repository.pk), + "mirror": False, + "url": acs_url, + }, + ) + CreatedResource(content_object=task) # No effect on http responses + CreatedResource(content_object=task_group) # No effect on http responses + + return TaskGroupResponse(task_group, request)