Old LazyYT cooked posts have not been rebaked after lazy-yt was replaced with lazy-video

AI-generated summary

The discussion addresses an issue where posts containing old lazyYT embedded YouTube videos are not being properly rebaked after the lazy-yt plugin was replaced with lazy-video. Users report that many posts show blank or missing videos because the baked content still references the outdated lazyYT format. A migration script was introduced to mark affected posts for rebaking, but it appears not all posts were processed. One user, MerryMinstrel, discovered the issue and manually rebaked thousands of posts using a Rails console script, iterating through posts with lazyYT in their cooked content and calling rebake! with a one-second delay to avoid rate limits. JammyDodger suggested batching to prevent YouTube rate limiting. The user MerryMinstrel confirmed the manual approach worked, though slowly, and shared the script as a solution for others facing the same problem.

Looks like right now in my forums I have hundreds or thousands of blank posts or posts just missing youtube videos and the reason seems to be that baked post content looks like this:

<div class="cooked">
<div class="lazyYT" data-youtube-id="WJxSNbAer9M" data-youtube-title="Starset - Die For You (Official Audio)" data-width="480" data-height="270" data-parameters="feature=oembed&amp;wmode=opaque">
</div>
</div>

If I rebuild HTML YouTube videos are cooked correctly using lazy-video-wrapper.

I did some digging and seems that this problem maybe related to this PR: DEV: Remove lazy-yt and replace with lazy-videos by jancernik · Pull Request #20722 · discourse/discourse · GitHub For some reason after this change not all posts got rebaked. I’m not a SQL or Discourse guy so I’m not sure if the problem is in migration script it self or in that it failed at some point.

class RebakeLazyYtPosts < ActiveRecord::Migration[7.0]
  def up
    execute <<~SQL
      UPDATE posts SET baked_version = 0
      WHERE cooked LIKE '%lazyYT-container%'
    SQL
  end

Seems that I need to rebake a couple of thousand posts with youtube links in them. How should I go about doing it?

I think it’s possible to be rate-limited by YouTube when rebaking a large amount of posts. You may want to consider doing it in batches with a delay to avoid hitting it again.

Could you point me in a direction of a reference on how to do it? I suppose a way which would not require an active terminal session.

I did not delve into why my posts were not rebaked so it remains a mystery. If somebody encounters this problem and wants to rebake manually their posts this is how i did go about it:

Enter Rails console:

cd /var/discourse
./launcher enter app
rails c

And I did run this script:

posts = Post.where ("cooked like '%lazyYT%'")
posts.each do |p|
    p.rebake!     
    sleep 1 
end

One post per second is slower than I would like, but I wanted to be extra sure that I will not hit any YouTube limits if there are any. For 12 000 posts it was not that bad of a wait.