Skip to content
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

optimize garabge collection command #4808

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions contentcuration/contentcuration/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from django.core.mail import send_mail
from django.core.validators import MaxValueValidator
from django.core.validators import MinValueValidator
from django.db import connection
from django.db import IntegrityError
from django.db import models
from django.db.models import Count
Expand Down Expand Up @@ -261,9 +262,21 @@ def hard_delete_user_related_data(self):
editable_channels_user_query, field="id")).filter(num_editors=1, public=False)

# Point sole editor non-public channels' contentnodes to orphan tree to let
# our garbage collection delete the nodes and underlying files.
ContentNode._annotate_channel_id(ContentNode.objects).filter(channel_id__in=list(
non_public_channels_sole_editor.values_list("id", flat=True))).update(parent_id=settings.ORPHANAGE_ROOT_ID)
# our garbage collection delete the nodes and underlying file.
channel_tree_id_map = dict(
ContentNode.objects.values_list('id', 'main_tree__tree_id')
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah this is a lot of channels! I meant iterating over the tree_id using non_public_channels_sole_editor. This won't work well either

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yes, my apologies now I see it that was an unnecessary overhead!


logging.debug("Queries after creating channel tree ID map: %s", connection.queries)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need settings.DEBUG = True to get the queries printed.


tree_ids_to_update = [
tree_id for id, tree_id in channel_tree_id_map.items()
if id in non_public_channels_sole_editor.values_list("id", flat=True)
]

ContentNode.objects.filter(tree_id__in=tree_ids_to_update).update(parent_id=settings.ORPHANAGE_ROOT_ID)

logging.debug("Queries after updating content nodes parent ID: %s", connection.queries)

# Hard delete non-public channels associated with this user (if user is the only editor).
non_public_channels_sole_editor.delete()
Expand Down
Loading