thijsvanloef / palworld-server-docker

A Docker Container to easily run a Palworld dedicated server.
https://hub.docker.com/r/thijsvanloef/palworld-server-docker
MIT License
2.43k stars 296 forks source link

Restart Container to Stop Memory Leaks #259

Closed mobileqaguy closed 9 months ago

mobileqaguy commented 9 months ago

Is your feature request related to a problem? Please describe. After playing for several hours, things start functioning incorrectly. Sometimes I cant pickup a pal to set it to do a specific task, acquire items made on assembly lines, and other oddities. I've found that simply restarting the container helps a lot. I see in the latest release that there was a k8s setting for a daily reboot but nothing to do scheduled reboots.

Describe the solution you'd like I'd like a way to be able to set environment variable for this. Ideally, use a cron expression for when to run it.

Describe alternatives you've considered I have been just restarting it myself

Additional context It seems that restarting the container almost always fixes the weird issues i encounter. Below is the script that I am using. My cron expression is 15 1,5,9,13,17,21 * * * since these times fall in line the most with people not being on the server.

#!/bin/bash

FIRST_WARNING_MINUTES=5
FINAL_WARNING_MINUTES=1
DOCKER_EXEC_RCON_COMMAND_PREPEND="docker exec -it palworld-server rcon-cli"

sleepForMinutes() {
    local minutes="$1"
    local seconds="$(( minutes * 60 ))"

    if [ "$minutes" -gt 1 ]; then
        seconds="$(( seconds - 60 ))"
    fi

    sleep "$seconds"
}

getRconCliBroadcastRestartString() {
    local warning_minutes="$1"

    if [ "$warning_minutes" -gt 1 ]; then
        echo "$DOCKER_EXEC_RCON_COMMAND_PREPEND 'Broadcast Server_rebooting_in_${warning_minutes}_minutes'"
    elif [ "$warning_minutes" -eq 1 ]; then
        echo "$DOCKER_EXEC_RCON_COMMAND_PREPEND 'Broadcast Server_rebooting_in_${warning_minutes}_minute'"
    else
        echo "$DOCKER_EXEC_RCON_COMMAND_PREPEND 'Broadcast Server_rebooting_now'"
    fi
}

sendRconCliCommand() {
    local command="$1"
    eval "${command}"
    if [ $? -ne 0 ]; then
        exit 1
    fi
}

restartServer() {
    docker restart palworld-server
}

saveServer() {
    sendRconCliCommand "${DOCKER_EXEC_RCON_COMMAND_PREPEND} Save"
}

saveAndRestart() {

    # Send broadcast message to server letting them know that the server will restart in 5 minutes
    sendRconCliCommand "$(getRconCliBroadcastRestartString "$FIRST_WARNING_MINUTES")"

    # Sleep for 1 minute less than 'FIRST_WARNING_MINUTES' since a warning is sent 1 minute before restart
    sleepForMinutes "$FIRST_WARNING_MINUTES"

    # Send broadcast message to server letting them know that the server will restart in 1 minute
    sendRconCliCommand "$(getRconCliBroadcastRestartString "$FINAL_WARNING_MINUTES")"

    # Sleep for 1 minute
    sleepForMinutes "$FINAL_WARNING_MINUTES"

    # Send final broadcast message
    sendRconCliCommand "$(getRconCliBroadcastRestartString 0)"

    # Save current server
    saveServer

    # Restart container
    restartServer
}

saveAndRestart
thijsvanloef commented 9 months ago

hi @mobileqaguy this feature currently has a PR open to solve, however for now you can use the solution provided here: https://github.com/thijsvanloef/palworld-server-docker/issues/37