-
Notifications
You must be signed in to change notification settings - Fork 158
Update delete task to skip over already deleted objects #7930
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gerrod3
wants to merge
1
commit into
pulp:main
Choose a base branch
from
gerrod3:fix/7910-general-multi-delete-does-not-exist
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+68
−9
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Fixed `general_delete`, `ageneral_delete`, and `general_multi_delete` to skip instances that no longer exist instead of failing with a bare `DoesNotExist`. The task result now reports skipped pks under `skipped` and per-model delete counts. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| from collections import defaultdict | ||
| from logging import getLogger | ||
|
|
||
| from asgiref.sync import sync_to_async | ||
| from django.db import transaction | ||
|
|
||
|
|
@@ -6,6 +9,8 @@ | |
| from pulpcore.app.models import CreatedResource | ||
| from pulpcore.plugin.models import MasterModel | ||
|
|
||
| log = getLogger(__name__) | ||
|
|
||
|
|
||
| def general_create_from_temp_file(app_label, serializer_name, temp_file_pk, *args, **kwargs): | ||
| """ | ||
|
|
@@ -93,37 +98,73 @@ def general_delete(instance_id, app_label, serializer_name, **kwargs): | |
| id (str): the id of the model | ||
| app_label (str): the Django app label of the plugin that provides the model | ||
| serializer_name (str): name of the serializer class for the model | ||
|
|
||
| Returns: | ||
| dict: Task result. Skipped instances are listed under `skipped`; otherwise contains | ||
| the per-model delete counts from `Model.delete()`. | ||
| """ | ||
| deprecation_logger.warning( | ||
| "`pulpcore.app.tasks.base.general_delete` is deprecated and will be removed in Pulp 4. " | ||
| "Use `pulpcore.app.tasks.base.ageneral_delete` instead." | ||
| ) | ||
| serializer_class = get_plugin_config(app_label).named_serializers[serializer_name] | ||
| instance = serializer_class.Meta.model.objects.get(pk=instance_id) | ||
| model = serializer_class.Meta.model | ||
| output = defaultdict(list) | ||
| try: | ||
| instance = model.objects.get(pk=instance_id) | ||
| except model.DoesNotExist: | ||
| log.info( | ||
| "Skipping delete of %s pk=%s; it no longer exists.", | ||
| model.__name__, | ||
| instance_id, | ||
| ) | ||
| output["skipped"].append(str(instance_id)) | ||
| return dict(output) | ||
| if isinstance(instance, MasterModel): | ||
| instance = instance.cast() | ||
| instance.delete() | ||
| output.update(instance.delete()[1]) | ||
| return dict(output) | ||
|
|
||
|
|
||
| def general_multi_delete(instance_ids, **kwargs): | ||
| """ | ||
| Delete a list of model instances in a transaction | ||
|
|
||
| The model instances are identified using the id, app_label, and serializer_name. | ||
| Instances that no longer exist are skipped. | ||
|
|
||
| Args: | ||
| instance_ids (list): List of tupels of id, app_label, serializer_name | ||
|
|
||
| Returns: | ||
| dict: Task result. Skipped instances are listed under `skipped`; otherwise contains | ||
| the accumulated per-model delete counts from `Model.delete()`. | ||
| """ | ||
| output = defaultdict(list) | ||
| counts = defaultdict(int) | ||
| instances = [] | ||
| for instance_id, app_label, serializer_name in instance_ids: | ||
| serializer_class = get_plugin_config(app_label).named_serializers[serializer_name] | ||
| instance = serializer_class.Meta.model.objects.get(pk=instance_id) | ||
| model = serializer_class.Meta.model | ||
| try: | ||
| instance = model.objects.get(pk=instance_id) | ||
| except model.DoesNotExist: | ||
| log.info( | ||
| "Skipping delete of %s pk=%s; it no longer exists.", | ||
| model.__name__, | ||
| instance_id, | ||
| ) | ||
| output["skipped"].append(str(instance_id)) | ||
| continue | ||
| if isinstance(instance, MasterModel): | ||
| instance = instance.cast() | ||
| instances.append(instance) | ||
| with transaction.atomic(): | ||
| for instance in instances: | ||
| instance.delete() | ||
| for model_label, count in instance.delete()[1].items(): | ||
| counts[model_label] += count | ||
| output.update(counts) | ||
| return dict(output) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Task outputs can be anything as long as they are serializable, correct? |
||
|
|
||
|
|
||
| async def ageneral_update(instance_id, app_label, serializer_name, *args, **kwargs): | ||
|
|
@@ -148,9 +189,25 @@ async def ageneral_update(instance_id, app_label, serializer_name, *args, **kwar | |
| async def ageneral_delete(instance_id, app_label, serializer_name, **kwargs): | ||
| """ | ||
| Async version of [pulpcore.app.tasks.base.general_delete][]. | ||
|
|
||
| Returns: | ||
| dict: Task result. Skipped instances are listed under `skipped`; otherwise contains | ||
| the per-model delete counts from `Model.adelete()`. | ||
| """ | ||
| serializer_class = get_plugin_config(app_label).named_serializers[serializer_name] | ||
| instance = await serializer_class.Meta.model.objects.aget(pk=instance_id) | ||
| model = serializer_class.Meta.model | ||
| output = defaultdict(list) | ||
| try: | ||
| instance = await model.objects.aget(pk=instance_id) | ||
| except model.DoesNotExist: | ||
| log.info( | ||
| "Skipping delete of %s pk=%s; it no longer exists.", | ||
| model.__name__, | ||
| instance_id, | ||
| ) | ||
| output["skipped"].append(str(instance_id)) | ||
| return dict(output) | ||
| if isinstance(instance, MasterModel): | ||
| instance = await instance.acast() | ||
| await instance.adelete() | ||
| output.update((await instance.adelete())[1]) | ||
| return dict(output) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some questions about logging:
gettext? Should we still be wrapping log statements inside it?