Project

Profile

Help

Refactor #3038

DRY up asynchronous update and delete tasks

Added by amacdona@redhat.com over 3 years ago. Updated about 1 year 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 - CURRENTRELEASE

<a title="Actions" class="icon-only icon-actions js-contextmenu" href="#">Actions</a>

Associated revisions

Revision 95574fb5 View on GitHub
Added by dkliban@redhat.com about 3 years ago

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

Revision 95574fb5 View on GitHub
Added by dkliban@redhat.com about 3 years ago

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

History

#1 Updated by amacdona@redhat.com over 3 years ago

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

#2 Updated by amacdona@redhat.com over 3 years ago

  • Sprint Candidate changed from No to Yes

#3 Updated by bmbouter over 3 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.

#4 Updated by mhrivnak over 3 years ago

+1

#5 Updated by dkliban@redhat.com over 3 years ago

  • Groomed changed from No to Yes

#6 Updated by jortel@redhat.com over 3 years ago

  • Sprint/Milestone set to 46

#7 Updated by dkliban@redhat.com over 3 years ago

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

#8 Updated by mhrivnak about 3 years ago

  • Sprint/Milestone changed from 46 to 47

#9 Updated by dkliban@redhat.com about 3 years ago

  • Status changed from ASSIGNED to POST

#10 Updated by rchan about 3 years ago

  • Sprint/Milestone changed from 47 to 48

#11 Updated by daviddavis about 3 years ago

  • Tags Pulp 3 MVP added

#12 Updated by dkliban@redhat.com about 3 years ago

  • Status changed from POST to MODIFIED
  • % Done changed from 0 to 100

#13 Updated by bmbouter almost 3 years ago

  • Sprint set to Sprint 29

#14 Updated by bmbouter almost 3 years ago

  • Sprint/Milestone deleted (48)

#15 Updated by dkliban@redhat.com almost 3 years ago

  • Sprint/Milestone set to 3.0.0

#16 Updated by bmbouter almost 2 years ago

  • Tags deleted (Pulp 3, Pulp 3 MVP)

#17 Updated by bmbouter about 1 year ago

  • Status changed from MODIFIED to CLOSED - CURRENTRELEASE

Please register to edit this issue

Also available in: Atom PDF