סקריפט rebake איטי כדי לא להעמיס על השרת שלך

סיכום מבוסס בינה מלאכותית

In a discussion about managing server load during Discourse migration, SubStrider shares a custom Ruby script (slow_rebake_resumer_ultralow.rb) to enqueue rebake jobs into Sidekiq’s :ultra_low priority queue, avoiding server overload and preventing delays to critical processes like notifications. The script processes posts in batches (default 500), pauses between batches (default 5 seconds), and resumes from a specified post ID. pfaffman notes that Discourse already runs a background rebake process, suggesting the script may be redundant unless migration was non-standard. SubStrider confirms they skipped the standard migration and plans to elaborate later. adopilot asks for guidance on using the script, and SubStrider replies with setup instructions: create the file in /shared/tmp, paste the code, and run it inside the container via rails runner. The script includes progress reporting and error handling, and is designed for safe, slow rebaking during live migration.

אז נאלצתי לעלות לאוויר מבלי לחכות לסיום ה-rebake המלא. לאחר שעליתי לאוויר, לא יכולתי להריץ את ה-rebake מכיוון שזה היה הורג את השרת. יכולתי להריץ את ה-rebake בתור ברירת המחדל, מכיוון שזה היה מעכב את כל התהליכים האחרים כמו התראות.

לכן השתמשתי בסקריפט זה כדי להכניס את כל משימות ה-rebake לתור ה-sidekiq בעדיפות נמוכה במיוחד. משתף כאן למקרה שמישהו אחר יזדקק לזה.

# slow_rebake_resumer_ultralow.rb
# מחדש הכנסת משימות rebake מנקודה ספציפית ושולח אותן
# ישירות לתור בעדיפות :ultra_low.
# יש להריץ בתוך הקונטיינר עם -> rails runner slow_rebake_resumer_ultralow.rb

# --- הגדרות ---
BATCH_SIZE = 500
SLEEP_DURATION = 5
# --- נקודת התחלה מהעדכון האחרון שלך ---
last_processed_id = 1952526 
# --------------------

start_time = Time.now
total_posts = Post.count

puts "מחדש ומכניס משימות rebake עבור #{total_posts} פוסטים לתור :ultra_low..."
puts "מתחיל אחרי פוסט ID #{last_processed_id}."
puts "---"

loop do
  # מצא ביעילות את קבוצת הפוסטים הבאה לעיבוד
  posts_to_enqueue = Post.where("id "> ?", last_processed_id).order(id: :asc).limit(BATCH_SIZE)

  # צא מהלולאה אם אין עוד פוסטים
  break if posts_to_enqueue.empty?

  posts_to_enqueue.each do |post|
    begin
      # הגדר את כל האפשרויות ב-hash יחיד, כולל התור המיועד
      options = {
        post_id: post.id,
        cook: true,
        bypass_bump: true,
        queue: :ultra_low
      }

      # הכנס את המשימה עם התחביר הנכון והמאומת
      Jobs.enqueue(:process_post, options)

    rescue ="> e
      puts "!!! שגיאה בהכנסת משימה עבור פוסט ID #{post.id}: #{e.message}"
    end
  end

  # עדכן את המיקום שלנו ל-ID הפוסט האחרון בקבוצה הנוכחית
  last_processed_id = posts_to_enqueue.last.id
  
  # קבל ספירה מדויקת יותר עבור דוח ההתקדמות
  enqueued_count = Post.where("id "> ?", last_processed_id).count

  # --- דיווח התקדמות ---
  percentage = (enqueued_count.to_f / total_posts * 100).round(2)
  elapsed_minutes = ((Time.now - start_time) / 60).round(2)

  puts "הוכנסו עד פוסט ID #{last_processed_id}. סה"כ הוכנסו: ~#{enqueued_count} / #{total_posts} (#{percentage}%)"
  puts "זמן שחלף (סשן זה): #{elapsed_minutes} דקות."
  puts "המשימות נשלחות לתור :ultra_low."

  # השהה בין קבוצות כדי לשמור על עומס נמוך
  if enqueued_count < total_posts
    puts "ממתין #{SLEEP_DURATION} שניות..."
    sleep(SLEEP_DURATION)
    puts "---"
  end
end

puts "\nכל משימות ה-rebake הנותרות הוכנסו בהצלחה לתור :ultra_low!"

אני די בטוח שכבר קיים תהליך שרץ כל הזמן ועושה בערך אותו הדבר ושמוזכר על ידי סקריפט השחזור, אבל אולי הרצת את המיגרציה על השרת החי ולא ראית אותה.

Yes there is. I did not do the standard migration script import. Will post more about it later, once the users have settled down from complaining about infinite scroll and overall bad cluttered user experience on Discourse. :upside_down_face:

But in general also once might need a full rebake like changing the domain, doing some find and replace etc.

Sorry for bumping into post, I am kind of same situation with my Discourse.

Any tips how to run this script ?

Where to create script and how to call it

Thanks in advanced

Create a file slow_rebake_resumer_ultralow.rb under tmp directory within the shared directory and paste the entire code in it.

Then execute that file once inside the container with rails runner slow_rebake_resumer_ultralow.rb

Observe the progress under yousite.com/sidekiq