Project

Profile

Help

Refactor #3038

closed

DRY up asynchronous update and delete tasks

Added by amacdona@redhat.com about 7 years ago. Updated almost 5 years ago.

Status:
CLOSED - CURRENTRELEASE
Priority:
Normal
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

Related to Pulp - Story #3044: Distribution create/update operations should be asynchronousCLOSED - CURRENTRELEASECodeHeeler

Actions
Actions #1

Updated by amacdona@redhat.com about 7 years ago

  • Related to Story #3044: Distribution create/update operations should be asynchronous added
Actions #2

Updated by amacdona@redhat.com about 7 years ago

  • Sprint Candidate changed from No to Yes
Actions #3

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.

Actions #4

Updated by mhrivnak about 7 years ago

+1

Actions #5

Updated by dkliban@redhat.com about 7 years ago

  • Groomed changed from No to Yes
Actions #6

Updated by jortel@redhat.com about 7 years ago

  • Sprint/Milestone set to 46
Actions #7

Updated by dkliban@redhat.com about 7 years ago

  • Status changed from NEW to ASSIGNED
  • Assignee set to dkliban@redhat.com
Actions #8

Updated by mhrivnak about 7 years ago

  • Sprint/Milestone changed from 46 to 47
Actions #9

Updated by dkliban@redhat.com about 7 years ago

  • Status changed from ASSIGNED to POST
Actions #10

Updated by rchan almost 7 years ago

  • Sprint/Milestone changed from 47 to 48
Actions #11

Updated by daviddavis almost 7 years ago

  • Tags Pulp 3 MVP added

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.

closes #3038 https://pulp.plan.io/issues/3038

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.

closes #3038 https://pulp.plan.io/issues/3038

Actions #12

Updated by dkliban@redhat.com almost 7 years ago

  • Status changed from POST to MODIFIED
  • % Done changed from 0 to 100
Actions #13

Updated by bmbouter over 6 years ago

  • Sprint set to Sprint 29
Actions #14

Updated by bmbouter over 6 years ago

  • Sprint/Milestone deleted (48)
Actions #15

Updated by dkliban@redhat.com over 6 years ago

  • Sprint/Milestone set to 3.0.0
Actions #16

Updated by bmbouter over 5 years ago

  • Tags deleted (Pulp 3, Pulp 3 MVP)
Actions #17

Updated by bmbouter almost 5 years ago

  • Status changed from MODIFIED to CLOSED - CURRENTRELEASE

Also available in: Atom PDF