Actions
Refactor #3038
closedDRY up asynchronous update and delete tasks
Status:
CLOSED - CURRENTRELEASE
Priority:
Normal
Assignee:
Category:
-
Sprint/Milestone:
Start date:
Due date:
% Done:
100%
Estimated time:
Platform Release:
Groomed:
Yes
Sprint Candidate:
Yes
Tags:
Sprint:
Sprint 29
Quarter:
Description
Currently, each object related to repos (Repositories, Importers, Publishers, Distributions) carries its own task for update and delete. Each ViewSet overrides update and destroy to call these tasks.
Instead of doing this for each object, I hope that we can consolidate this into 1 general update task and 1 general delete task. Using async mixins for update and delete, the ViewSets will no longer override each view.
Here's a POC:
class AsyncUpdateMixin(object):
"""
Update a model instance.
"""
def update(self, request, repository_name, *args, **kwargs):
partial = kwargs.pop('partial', False)
instance = self.get_object()
serializer = self.get_serializer(instance, data=request.data, partial=partial)
serializer.is_valid(raise_exception=True)
app_label = instance._meta.app_label
# ret = tasks.importer.general_update(instance.id, app_label, serializer.__class__.__name__,
# data=request.data, partial=partial)
async_result = tasks.importer.general_update.apply_async_with_reservation(
tags.RESOURCE_REPOSITORY_TYPE, repository_name,
args=(instance.id, app_label, serializer.__class__.__name__),
kwargs={'data': request.data, 'partial': partial}
)
return OperationPostponedResponse([async_result], request)
def partial_update(self, request, *args, **kwargs):
kwargs['partial'] = True
return self.update(request, *args, **kwargs)
@shared_task(base=UserFacingTask)
def general_update(instance_id, app_label, serializer_name, *args, **kwargs):
data = kwargs.pop('data', None)
partial = kwargs.pop('partial', False)
serializer_class = get_plugin_config(app_label).named_serializers[serializer_name]
# TODO cast if master/detail
instance = serializer_class.Meta.model.objects.get(id=instance_id)
data_querydict = QueryDict('', mutable=True)
data_querydict.update(data)
serializer = serializer_class(instance, data=data_querydict, partial=partial)
serializer.is_valid(raise_exception=True)
serializer.save()
Related issues
Actions
Problem: async update and delete code is duplicated
Solution: add general update and delete tasks and viewsets
This patch creates two new tasks that are used by both the publisher viewset and the importer viewset. This patch also introduces a new base viewset. Both the importer and publisher viewset inherit from it.
closes #3038 https://pulp.plan.io/issues/3038