diff --git a/bot.py b/bot.py index 8a840f1..a428b3e 100644 --- a/bot.py +++ b/bot.py @@ -61,6 +61,14 @@ _SSH_OPTS = ["-p", FORCE_SSH_PORT, "-o", "BatchMode=yes", "-o", "StrictHostKeyCh *(["-i", FORCE_SSH_KEY] if FORCE_SSH_KEY else [])] _RSYNC_RSH = "ssh " + " ".join(shlex.quote(o) for o in _SSH_OPTS) +# /force is known to be big and slow, so run its subprocesses (yt-dlp + the node +# n-sig solver and ffmpeg merge it spawns, plus rsync) at lowest CPU and idle IO +# priority. They yield to everything else under load but still run full-speed when +# the box is idle; child processes inherit the niceness. Normal short downloads +# are left at full priority for responsiveness. +_NICE = (["nice", "-n", "19"] if shutil.which("nice") else []) + \ + (["ionice", "-c", "3"] if shutil.which("ionice") else []) + logging.basicConfig( level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s", @@ -649,6 +657,7 @@ def _force_download_and_publish(url: str, token: str) -> tuple[str | None, int | tokdir = os.path.join(stage, token) os.makedirs(tokdir) cmd = [ + *_NICE, YTDLP, "--no-playlist", "--no-warnings", "--js-runtimes", "node", "-f", "best[ext=mp4]/best", "--merge-output-format", "mp4", "--max-filesize", FORCE_MAX_FILESIZE, @@ -674,7 +683,7 @@ def _force_download_and_publish(url: str, token: str) -> tuple[str | None, int | size_mb = os.path.getsize(os.path.join(tokdir, fname)) // (1024 * 1024) up = subprocess.run( - ["rsync", "-a", "--chmod=F644,D755", "-e", _RSYNC_RSH, + [*_NICE, "rsync", "-a", "--chmod=F644,D755", "-e", _RSYNC_RSH, tokdir, f"{FORCE_REMOTE}:{FORCE_REMOTE_DIR}/"], capture_output=True, text=True, timeout=RSYNC_TIMEOUT, )