Refactor #3038
closedDRY up asynchronous update and delete tasks
100%
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
Updated by amacdona@redhat.com about 7 years ago
- Related to Story #3044: Distribution create/update operations should be asynchronous added
Updated by amacdona@redhat.com about 7 years ago
- Sprint Candidate changed from No to Yes
Updated by bmbouter about 7 years ago
- Tags Pulp 3 added
+1 to this. We have it in at least 4 places so it would be good DRY savings, and the refactor looks straightforward. I think this is good to be groomed.
Updated by dkliban@redhat.com about 7 years ago
- Status changed from NEW to ASSIGNED
- Assignee set to dkliban@redhat.com
Updated by dkliban@redhat.com about 7 years ago
- Status changed from ASSIGNED to POST
Added by dkliban@redhat.com almost 7 years ago
Added by dkliban@redhat.com almost 7 years ago
Revision 95574fb5 | View on GitHub
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.
Updated by dkliban@redhat.com almost 7 years ago
- Status changed from POST to MODIFIED
- % Done changed from 0 to 100
Applied in changeset pulp|95574fb5427ddde36d4a463b82ec927b590044a2.
Updated by bmbouter almost 5 years ago
- Status changed from MODIFIED to CLOSED - CURRENTRELEASE
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