Slow rebake script to avoid overwhelming your server

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