PERF: Move post alerting into async

This commit is contained in:
Robin Ward 2015-04-20 13:34:57 -04:00
parent 6ae58d41a7
commit 5990ab855b
5 changed files with 22 additions and 2 deletions

View file

@ -0,0 +1,11 @@
module Jobs
class PostAlert < Jobs::Base
def execute(args)
post = Post.find(args[:post_id])
PostAlerter.post_created(post)
end
end
end

View file

@ -190,6 +190,7 @@ class PostAlerter
exclude_user_ids << reply_to_user.id if reply_to_user.present?
exclude_user_ids.flatten!
TopicUser
.where(topic_id: post.topic_id, notification_level: TopicUser.notification_levels[:watching])
.includes(:user).each do |tu|

View file

@ -129,7 +129,6 @@ class PostCreator
if @post && errors.blank?
publish
PostAlerter.post_created(@post) unless @opts[:import_mode]
track_latest_on_category
enqueue_jobs

View file

@ -9,17 +9,22 @@ class PostJobsEnqueuer
def enqueue_jobs
# We need to enqueue jobs after the transaction. Otherwise they might begin before the data has
# been comitted.
enqueue_post_alerts unless @opts[:import_mode]
feature_topic_users unless @opts[:import_mode]
trigger_post_post_process
unless skip_after_create?
after_post_create
after_topic_create
end
end
private
def enqueue_post_alerts
Jobs.enqueue(:post_alert, post_id: @post.id)
end
def feature_topic_users
Jobs.enqueue(:feature_topic_users, topic_id: @topic.id)
end

View file

@ -137,6 +137,7 @@ describe PostCreator do
it 'queues up post processing job when saved' do
Jobs.expects(:enqueue).with(:feature_topic_users, has_key(:topic_id))
Jobs.expects(:enqueue).with(:process_post, has_key(:post_id))
Jobs.expects(:enqueue).with(:post_alert, has_key(:post_id))
Jobs.expects(:enqueue).with(:notify_mailing_list_subscribers, has_key(:post_id))
creator.create
end
@ -144,6 +145,7 @@ describe PostCreator do
it 'passes the invalidate_oneboxes along to the job if present' do
Jobs.stubs(:enqueue).with(:feature_topic_users, has_key(:topic_id))
Jobs.expects(:enqueue).with(:notify_mailing_list_subscribers, has_key(:post_id))
Jobs.expects(:enqueue).with(:post_alert, has_key(:post_id))
Jobs.expects(:enqueue).with(:process_post, has_key(:invalidate_oneboxes))
creator.opts[:invalidate_oneboxes] = true
creator.create
@ -152,6 +154,7 @@ describe PostCreator do
it 'passes the image_sizes along to the job if present' do
Jobs.stubs(:enqueue).with(:feature_topic_users, has_key(:topic_id))
Jobs.expects(:enqueue).with(:notify_mailing_list_subscribers, has_key(:post_id))
Jobs.expects(:enqueue).with(:post_alert, has_key(:post_id))
Jobs.expects(:enqueue).with(:process_post, has_key(:image_sizes))
creator.opts[:image_sizes] = {'http://an.image.host/image.jpg' => {'width' => 17, 'height' => 31}}
creator.create
@ -491,6 +494,7 @@ describe PostCreator do
# does not notify an unrelated user
expect(unrelated.notifications.count).to eq(0)
expect(post.topic.subtype).to eq(TopicSubtype.user_to_user)
expect(target_user1.notifications.count).to eq(1)
expect(target_user2.notifications.count).to eq(1)
end