mirror of
https://github.com/discourse/discourse.git
synced 2025-01-31 06:28:31 +08:00
PERF: Move post alerting into async
This commit is contained in:
parent
6ae58d41a7
commit
5990ab855b
11
app/jobs/regular/post_alert.rb
Normal file
11
app/jobs/regular/post_alert.rb
Normal 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
|
||||||
|
|
|
@ -190,6 +190,7 @@ class PostAlerter
|
||||||
|
|
||||||
exclude_user_ids << reply_to_user.id if reply_to_user.present?
|
exclude_user_ids << reply_to_user.id if reply_to_user.present?
|
||||||
exclude_user_ids.flatten!
|
exclude_user_ids.flatten!
|
||||||
|
|
||||||
TopicUser
|
TopicUser
|
||||||
.where(topic_id: post.topic_id, notification_level: TopicUser.notification_levels[:watching])
|
.where(topic_id: post.topic_id, notification_level: TopicUser.notification_levels[:watching])
|
||||||
.includes(:user).each do |tu|
|
.includes(:user).each do |tu|
|
||||||
|
|
|
@ -129,7 +129,6 @@ class PostCreator
|
||||||
|
|
||||||
if @post && errors.blank?
|
if @post && errors.blank?
|
||||||
publish
|
publish
|
||||||
PostAlerter.post_created(@post) unless @opts[:import_mode]
|
|
||||||
|
|
||||||
track_latest_on_category
|
track_latest_on_category
|
||||||
enqueue_jobs
|
enqueue_jobs
|
||||||
|
|
|
@ -9,17 +9,22 @@ class PostJobsEnqueuer
|
||||||
def enqueue_jobs
|
def enqueue_jobs
|
||||||
# We need to enqueue jobs after the transaction. Otherwise they might begin before the data has
|
# We need to enqueue jobs after the transaction. Otherwise they might begin before the data has
|
||||||
# been comitted.
|
# been comitted.
|
||||||
|
enqueue_post_alerts unless @opts[:import_mode]
|
||||||
feature_topic_users unless @opts[:import_mode]
|
feature_topic_users unless @opts[:import_mode]
|
||||||
trigger_post_post_process
|
trigger_post_post_process
|
||||||
|
|
||||||
unless skip_after_create?
|
unless skip_after_create?
|
||||||
after_post_create
|
after_post_create
|
||||||
after_topic_create
|
after_topic_create
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def enqueue_post_alerts
|
||||||
|
Jobs.enqueue(:post_alert, post_id: @post.id)
|
||||||
|
end
|
||||||
|
|
||||||
def feature_topic_users
|
def feature_topic_users
|
||||||
Jobs.enqueue(:feature_topic_users, topic_id: @topic.id)
|
Jobs.enqueue(:feature_topic_users, topic_id: @topic.id)
|
||||||
end
|
end
|
||||||
|
|
|
@ -137,6 +137,7 @@ describe PostCreator do
|
||||||
it 'queues up post processing job when saved' 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(:feature_topic_users, has_key(:topic_id))
|
||||||
Jobs.expects(:enqueue).with(:process_post, has_key(:post_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))
|
Jobs.expects(:enqueue).with(:notify_mailing_list_subscribers, has_key(:post_id))
|
||||||
creator.create
|
creator.create
|
||||||
end
|
end
|
||||||
|
@ -144,6 +145,7 @@ describe PostCreator do
|
||||||
it 'passes the invalidate_oneboxes along to the job if present' 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.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(: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))
|
Jobs.expects(:enqueue).with(:process_post, has_key(:invalidate_oneboxes))
|
||||||
creator.opts[:invalidate_oneboxes] = true
|
creator.opts[:invalidate_oneboxes] = true
|
||||||
creator.create
|
creator.create
|
||||||
|
@ -152,6 +154,7 @@ describe PostCreator do
|
||||||
it 'passes the image_sizes along to the job if present' 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.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(: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))
|
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.opts[:image_sizes] = {'http://an.image.host/image.jpg' => {'width' => 17, 'height' => 31}}
|
||||||
creator.create
|
creator.create
|
||||||
|
@ -491,6 +494,7 @@ describe PostCreator do
|
||||||
# does not notify an unrelated user
|
# does not notify an unrelated user
|
||||||
expect(unrelated.notifications.count).to eq(0)
|
expect(unrelated.notifications.count).to eq(0)
|
||||||
expect(post.topic.subtype).to eq(TopicSubtype.user_to_user)
|
expect(post.topic.subtype).to eq(TopicSubtype.user_to_user)
|
||||||
|
|
||||||
expect(target_user1.notifications.count).to eq(1)
|
expect(target_user1.notifications.count).to eq(1)
|
||||||
expect(target_user2.notifications.count).to eq(1)
|
expect(target_user2.notifications.count).to eq(1)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user