Just-Some-Bots / MusicBot

:musical_note: The original MusicBot for Discord (formerly SexualRhinoceros/MusicBot)
https://just-some-bots.github.io/MusicBot/
MIT License
3.09k stars 2.35k forks source link

Restart overhaul - with upgrade commands #2373

Closed itsTheFae closed 6 months ago

itsTheFae commented 6 months ago

Description

This PR expands the restart command to enable full restarts that will reload source files. Additionally this cleans up some logic involved with soft restarts and normal shutdowns to reduce errors.
The restart command now takes an optional argument restart full to trigger a full respawn of the the process. By default restart soft is implied and will only re-initialize the bot from the currently loaded process.

On Linux, Mac, and other Unix-style OS, the process will be replaced using execlp to keep the current Process ID. On Windows, the original process will be closed, and a new one will be spawned in a new console window. Subsequent console windows will automatically close when the bot closes, but the original window will always remain.

Additionally, this PR adds three sub-commands to restart which will attempt to upgrade the bot or its dependencies or both, and restart. These are documented in the commands help text / doc string.

Exhaustive testing has not been done against all the bots commands, in regards to shutdown cleanup changes. So there may still be errors generated from pending tasks that manage to survive a soft restart.

Things of note

The use of subprocess.Popen is not a blocking call, as implied in #1494 and using subprocess.CREATE_NEW_CONSOLE flag enables us to break away from the parent process tree, preventing long chains of processes.

The use of os.execlp on linux/mac may not flush pending data in file or IO buffers. These need to be flushed and closed before execlp call is made.

Tests done thus far

Restart the bot 15 times using each restart method and record memory usage:

Restart the bot and inspect process tree behavior: