From 5990ab855bbfc1085eead60d3a82a849ff241bad Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Mon, 20 Apr 2015 13:34:57 -0400 Subject: [PATCH] PERF: Move post alerting into async --- app/jobs/regular/post_alert.rb | 11 +++++++++++ app/services/post_alerter.rb | 1 + lib/post_creator.rb | 1 - lib/post_jobs_enqueuer.rb | 7 ++++++- spec/components/post_creator_spec.rb | 4 ++++ 5 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 app/jobs/regular/post_alert.rb diff --git a/app/jobs/regular/post_alert.rb b/app/jobs/regular/post_alert.rb new file mode 100644 index 000000000..99b1860c5 --- /dev/null +++ b/app/jobs/regular/post_alert.rb @@ -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 + diff --git a/app/services/post_alerter.rb b/app/services/post_alerter.rb index 56c2fbe20..819e4b2a0 100644 --- a/app/services/post_alerter.rb +++ b/app/services/post_alerter.rb @@ -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| diff --git a/lib/post_creator.rb b/lib/post_creator.rb index 718379379..ae261c022 100644 --- a/lib/post_creator.rb +++ b/lib/post_creator.rb @@ -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 diff --git a/lib/post_jobs_enqueuer.rb b/lib/post_jobs_enqueuer.rb index 207780e26..3d17c8c66 100644 --- a/lib/post_jobs_enqueuer.rb +++ b/lib/post_jobs_enqueuer.rb @@ -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 diff --git a/spec/components/post_creator_spec.rb b/spec/components/post_creator_spec.rb index 950af8188..bc7137c70 100644 --- a/spec/components/post_creator_spec.rb +++ b/spec/components/post_creator_spec.rb @@ -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