From 7632fe0b58a1e584cd48511b0ac8b8f6c94488ad Mon Sep 17 00:00:00 2001 From: Sam Saffron Date: Tue, 13 Aug 2019 10:27:52 +1000 Subject: [PATCH] PERF: rake posts:rebake_uncooked_posts runs inline Running this inline makes more sense otherwise there is extreme risk in saturating sidekiq queue. This also reworks ordering and selection so we double check if a post needs rebaking prior to rebaking, this unlocks the ability to run this rake task from multiple consoles. --- lib/tasks/posts.rake | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/tasks/posts.rake b/lib/tasks/posts.rake index 506201211f3..5bcdf2983e8 100644 --- a/lib/tasks/posts.rake +++ b/lib/tasks/posts.rake @@ -8,6 +8,11 @@ task 'posts:rebake' => :environment do end task 'posts:rebake_uncooked_posts' => :environment do + # rebaking uncooked posts can very quickly saturate sidekiq + # this provides an insurance policy so you can safely run and stop + # this rake task without worrying about your sidekiq imploding + Jobs.run_immediately! + ENV['RAILS_DB'] ? rebake_uncooked_posts : rebake_uncooked_posts_all_sites end @@ -24,8 +29,18 @@ def rebake_uncooked_posts rebaked = 0 total = uncooked.count - uncooked.find_each do |post| - rebake_post(post) + ids = uncooked.pluck(:id) + # work randomly so you can run this job from lots of consoles if needed + ids.shuffle! + + ids.each do |id| + # may have been cooked in interim + post = uncooked.where(id: id).first + + if post + rebake_post(post) + end + print_status(rebaked += 1, total) end