lloesche / valheim-server-docker

Valheim dedicated gameserver with automatic update, World backup, BepInEx and ValheimPlus mod support
https://hub.docker.com/r/lloesche/valheim-server
Apache License 2.0
1.89k stars 269 forks source link

Add shutdown if idle feature to save resources #687

Open mrskizzex opened 1 month ago

mrskizzex commented 1 month ago

Would it be possible to add either a feature in the title as an environmental variable or simply add a feature where you can add your own cron entry?

I crudely edited valheim's user crontab and added:

*/15 * * * * /usr/local/bin/valheim-is-idle && sleep 600 && /usr/local/bin/valheim-is-idle && /usr/local/bin/supervisorctl stop valheim-server

which works but It will probably not survive container updates.

I exposed supervisor page behind 2fa auth to my friends who can start the server if needed but I want to also shut down the server if there is no one on the server for more than 15-30 minutes to save resources.

lloesche commented 3 weeks ago

You can shut it down when idle, the question is how do you start it back up when a client connects? You would need some sort of UDP listener/socket handler that waits for incoming clients, puts them in some sort of waiting state while the server starts, so the Windows client doesn't timeout, and then hand over the connection to the Valheim server. You could also try something like systemd socket activation and maybe it would work out. Depends on how the client behaves when the server doesn't react immediately. For TCP services that's easier as you can delay the handshake. For UDP I'm guessing the client would just assume that its packets got lost fairly quickly. So you'd likely have to implement parts of the Valheim server protocol.