Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
CommentTree.add_comments: Add orphaned comments to the tree
Browse files Browse the repository at this point in the history
Adding them to the tree doesn't hurt anything--they still won't be
displayed until their parent comment is in the tree, but it does allow
for easier cleanup in case comments get processed out of order. Once their
parent is added to the tree everything will be consistent and fine.
  • Loading branch information
bsimpson63 committed Aug 11, 2016
1 parent 4bcc145 commit 9bb3167
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions r2/r2/models/comment_tree.py
Expand Up @@ -129,23 +129,22 @@ def add_comments(cls, link, comments):
if comment._id not in cids
}

# skip adding any comments whose parents are missing from the tree
# because they will never be displayed unless the tree is rebuilt.
# check for the parent in the existing tree and in the current
# batch of comments to be added.
if not comments:
return

# warn on any comments whose parents are missing from the tree
# because they will never be displayed unless their parent is
# added. this can happen in normal operation if there are multiple
# queue consumers and a child is processed before its parent.
parent_ids = set(cids) | {comment._id for comment in comments}
orphan_comments = {
possible_orphan_comments = {
comment for comment in comments
if (comment.parent_id and comment.parent_id not in parent_ids)
}
if orphan_comments:
if possible_orphan_comments:
g.log.error("comment_tree_inconsistent: %s %s", link,
orphan_comments)
possible_orphan_comments)
g.stats.simple_event('comment_tree_inconsistent')
comments -= orphan_comments

if not comments:
return

for comment in comments:
tree.setdefault(comment.parent_id, []).append(comment._id)
Expand Down

0 comments on commit 9bb3167

Please sign in to comment.