mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 22:43:33 +08:00
In populate:posts tool, catch post validation errors and retry
This commit is contained in:
@ -1,5 +1,7 @@
|
|||||||
class Populate < Thor
|
class Populate < Thor
|
||||||
|
|
||||||
|
MAX_ERRORS = 5
|
||||||
|
|
||||||
desc "posts", "Generate posts in a topic"
|
desc "posts", "Generate posts in a topic"
|
||||||
long_desc <<-LONGDESC
|
long_desc <<-LONGDESC
|
||||||
Create a topic with any number of posts, or add posts to an existing topic.
|
Create a topic with any number of posts, or add posts to an existing topic.
|
||||||
@ -54,17 +56,37 @@ class Populate < Thor
|
|||||||
|
|
||||||
puts "Making #{options[:num_posts]} posts"
|
puts "Making #{options[:num_posts]} posts"
|
||||||
|
|
||||||
|
num_errors = 0
|
||||||
|
|
||||||
(start_post..options[:num_posts]).each do |num|
|
(start_post..options[:num_posts]).each do |num|
|
||||||
print '.'
|
print '.'
|
||||||
raw = rand(4) == 0 ? (rand(2) == 0 ? image_posts.sample : wikipedia_posts.sample ) : hipster_words.sample(20).join(' ')
|
raw = rand(4) == 0 ? (rand(2) == 0 ? image_posts.sample : wikipedia_posts.sample ) : hipster_words.sample(20).join(' ')
|
||||||
post_creator = PostCreator.new(users[num % (users.length)], topic_id: topic.id, raw: raw)
|
post_creator = PostCreator.new(users[num % (users.length)], topic_id: topic.id, raw: raw)
|
||||||
post_creator.create
|
post_creator.create
|
||||||
|
if post_creator.errors.present?
|
||||||
|
# It's probably a "Body is too similar to what you recently posted" error.
|
||||||
|
# Try one more time using more random words.
|
||||||
|
post_creator = PostCreator.new(users[num % (users.length)], topic_id: topic.id, raw: hipster_words.sample(40).join(' '))
|
||||||
|
post_creator.create
|
||||||
|
if post_creator.errors.present?
|
||||||
|
# Still failing! Show the error.
|
||||||
|
puts '', "--------------------------"
|
||||||
|
puts "ERROR creating a post!"
|
||||||
|
puts post_creator.errors.full_messages
|
||||||
|
puts "--------------------------"
|
||||||
|
|
||||||
|
# Stop looping after MAX_ERRORS errors
|
||||||
|
num_errors += 1
|
||||||
|
if num_errors > MAX_ERRORS
|
||||||
|
puts "Giving up. Too many errors."
|
||||||
|
exit 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
puts ''
|
puts ''
|
||||||
|
|
||||||
RateLimiter.enable
|
|
||||||
|
|
||||||
puts "Done. Topic id = #{topic.id}"
|
puts "Done. Topic id = #{topic.id}"
|
||||||
ensure
|
ensure
|
||||||
RateLimiter.enable
|
RateLimiter.enable
|
||||||
|
Reference in New Issue
Block a user