Skip to content

Commit

Permalink
create stream items in slices
Browse files Browse the repository at this point in the history
Change-Id: I4421d13c6905265f8565d16fd6ca001ba86f3f32
Reviewed-on: https://gerrit.instructure.com/122444
Tested-by: Jenkins
Reviewed-by: Jeremy Stanley <[email protected]>
Product-Review: James Williams  <[email protected]>
QA-Review: James Williams  <[email protected]>
  • Loading branch information
maneframe committed Aug 14, 2017
1 parent 1e838fc commit 5418cee
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions app/models/stream_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,35 +257,38 @@ def self.generate_all(object, user_ids)
# do the bulk insert in user id order to avoid locking problems on postges < 9.3 (foreign keys)
user_ids_subset.sort!

inserts = user_ids_subset.map do |user_id|
{
:stream_item_id => stream_item_id,
:user_id => user_id,
:hidden => false,
:workflow_state => object_unread_for_user(object, user_id),
:context_type => l_context_type,
:context_id => l_context_id,
}
end
if object.is_a?(Submission) && object.assignment.muted?
# set the hidden flag if an assignment and muted (for the owner of the submission)
if owner_insert = inserts.detect{|i| i[:user_id] == object.user_id}
owner_insert[:hidden] = true
user_ids_subset.each_slice(500) do |sliced_user_ids|

inserts = sliced_user_ids.map do |user_id|
{
:stream_item_id => stream_item_id,
:user_id => user_id,
:hidden => false,
:workflow_state => object_unread_for_user(object, user_id),
:context_type => l_context_type,
:context_id => l_context_id,
}
end
if object.is_a?(Submission) && object.assignment.muted?
# set the hidden flag if an assignment and muted (for the owner of the submission)
if owner_insert = inserts.detect{|i| i[:user_id] == object.user_id}
owner_insert[:hidden] = true
end
end
end

StreamItemInstance.unique_constraint_retry do
StreamItemInstance.where(:stream_item_id => stream_item_id, :user_id => user_ids_subset).delete_all
StreamItemInstance.bulk_insert(inserts)
end
StreamItemInstance.unique_constraint_retry do
StreamItemInstance.where(:stream_item_id => stream_item_id, :user_id => sliced_user_ids).delete_all
StreamItemInstance.bulk_insert(inserts)
end

#reset caches manually because the observer wont trigger off of the above mass inserts
user_ids_subset.each do |user_id|
StreamItemCache.invalidate_recent_stream_items(user_id, l_context_type, l_context_id)
end
#reset caches manually because the observer wont trigger off of the above mass inserts
sliced_user_ids.each do |user_id|
StreamItemCache.invalidate_recent_stream_items(user_id, l_context_type, l_context_id)
end

# touch all the users to invalidate the cache
User.where(id: user_ids_subset).touch_all
# touch all the users to invalidate the cache
User.where(id: sliced_user_ids).touch_all
end
end

return [res]
Expand Down

0 comments on commit 5418cee

Please sign in to comment.