Skip to content

Commit

Permalink
Use cron instead of intervals for everything (lloesche#100)
Browse files Browse the repository at this point in the history
* Use cron instead of interval for update checks (Fixes lloesche#95)

* Use cron instead of interval for backups
  • Loading branch information
lloesche authored Feb 26, 2021
1 parent d14c05b commit 41bbf30
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ For more deployment options see the [Deployment section](#deployment).
| `WORLD_NAME` | `Dedicated` | Name of the world without `.db/.fwl` file extension |
| `SERVER_PASS` | `secret` | Password for logging into the server - min. 5 characters! |
| `SERVER_PUBLIC` | `1` | Whether the server should be listed in the server browser (`1`) or not (`0`) |
| `UPDATE_INTERVAL` | `900` | How often we check Steam for an updated server version in seconds. This also controls how often Github is checked for ValheimPlus updates. Note that Github has an API rate limit of 60 req/hour. So setting this to less than 60 will result in errors if V+ is enabled. |
| `UPDATE_CRON` | `*/15 * * * *` | [Cron schedule](https://en.wikipedia.org/wiki/Cron#Overview) for update checks (disabled if set to an empty string or if the legacy `UPDATE_INTERVAL` is set) |
| `RESTART_CRON` | `0 5 * * *` | [Cron schedule](https://en.wikipedia.org/wiki/Cron#Overview) for server restarts (disabled if set to an empty string) |
| `TZ` | `Etc/UTC` | Container [time zone](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) |
| `BACKUPS` | `true` | Whether the server should create periodic backups (`true` or `false`) |
| `BACKUPS_INTERVAL` | `3600` | Interval in seconds between backup runs |
| `BACKUPS_CRON` | `0 * * * *` | [Cron schedule](https://en.wikipedia.org/wiki/Cron#Overview) for world backups (disabled if set to an empty string or if the legacy `BACKUPS_INTERVAL` is set) |
| `BACKUPS_DIRECTORY` | `/config/backups` | Path to the backups directory |
| `BACKUPS_MAX_AGE` | `3` | Age in days after which old backups are flushed |
| `PERMISSIONS_UMASK` | `022` | [Umask](https://en.wikipedia.org/wiki/Umask) to use for backups, config files and directories |
Expand Down
3 changes: 3 additions & 0 deletions common
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ vp_install_path=/opt/valheim/plus
vp_zipfile=UnixServer.zip
valheim_restartfile="/tmp/valheim.restart"
vp_mergefile="$vp_download_path/merge"
valheim_server_pidfile=/var/run/valheim-server.pid
valheim_updater_pidfile=/var/run/valheim-updater.pid
valheim_backup_pidfile=/var/run/valheim-backup.pid

ensure_permissions() {
chmod $CONFIG_DIRECTORY_PERMISSIONS /config
Expand Down
12 changes: 10 additions & 2 deletions defaults
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,23 @@ SERVER_PUBLIC=${SERVER_PUBLIC:-1}
STEAMCMD_ARGS=${STEAMCMD_ARGS-validate}

# How often we check for Valheim server updates
UPDATE_INTERVAL=${UPDATE_INTERVAL:-900}
# This used to be 900 (15 min) and is here for backwards
# compatibility reasons. We now set it to 10 years by default
# and use a cron that sends SIGHUP instead
UPDATE_INTERVAL=${UPDATE_INTERVAL:-315360000}
# When we check for updates
UPDATE_CRON=${UPDATE_CRON-*/15 * * * *}


# What time we restart the valheim-server
# This is usful to mitigate the effects of memory/resource leaks
RESTART_CRON=${RESTART_CRON-0 5 * * *}

# World backup related settings
BACKUPS=${BACKUPS:-true}
BACKUPS_INTERVAL=${BACKUPS_INTERVAL:-3600}
# Legacy interval variable used to be 3600 (1h)
BACKUPS_INTERVAL=${BACKUPS_INTERVAL:-315360000}
BACKUPS_CRON=${BACKUPS_CRON-0 * * * *}
BACKUPS_DIRECTORY=${BACKUPS_DIRECTORY:-/config/backups}
BACKUPS_MAX_AGE=${BACKUPS_MAX_AGE:-3}

Expand Down
26 changes: 23 additions & 3 deletions valheim-backup
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@

# Include defaults
. /usr/local/etc/valheim/defaults
. /usr/local/etc/valheim/common

# Remove trailing slash if any
BACKUPS_DIRECTORY=${BACKUPS_DIRECTORY%/}
next_backup=$(date +%s)
run=true

main() {
echo $$ > "$valheim_backup_pidfile"
cd /config
while :; do
while [ $run = true ]; do
backup
flush_old
echo "Waiting $BACKUPS_INTERVAL seconds before next backup run"
sleep $BACKUPS_INTERVAL
next_backup=$(($(date +%s)+$BACKUPS_INTERVAL))
while [ $run = true -a $(date +%s) -lt $next_backup ]; do
sleep 8
done
done
}

Expand All @@ -38,7 +44,21 @@ flush_old() {
find "$BACKUPS_DIRECTORY" -type f -mtime +$BACKUPS_MAX_AGE -name 'worlds-*.zip' -print -exec rm -f "{}" \;
}


backup_now() {
echo "Received signal to backup world"
next_backup=0
}

shutdown() {
rm -f "$valheim_backup_pidfile"
run=false
}


if [ "X$BACKUPS" = Xtrue ]; then
trap backup_now SIGHUP
trap shutdown SIGINT SIGTERM
main
else
echo "Backups have been turned off by env BACKUPS=$BACKUPS"
Expand Down
18 changes: 15 additions & 3 deletions valheim-bootstrap
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,28 @@ else
echo "Error: unknown timezone $TZ"
fi

crontab=$(mktemp)
if [ "$BACKUPS" = true -a -n "$BACKUPS_CRON" -a "$BACKUPS_INTERVAL" = "315360000" ]; then
echo "Creating cron to do world backups using schedule $BACKUPS_CRON"
tmpfile=$(mktemp)
echo "$BACKUPS_CRON [ -f $valheim_backup_pidfile ] && kill -HUP \$(cat $valheim_backup_pidfile)" >> $crontab
fi

if [ -n "$UPDATE_CRON" -a "$UPDATE_INTERVAL" = "315360000" ]; then
echo "Creating cron to check for updates using schedule $UPDATE_CRON"
tmpfile=$(mktemp)
echo "$UPDATE_CRON [ -f $valheim_updater_pidfile ] && kill -HUP \$(cat $valheim_updater_pidfile)" >> $crontab
fi

if [ -n "$RESTART_CRON" ]; then
echo "Creating cron to restart valheim-server using schedule $RESTART_CRON"
tmpfile=$(mktemp)
echo "$RESTART_CRON /usr/bin/supervisorctl restart valheim-server" > $tmpfile
crontab $tmpfile
rm -f $tmpfile
echo "$RESTART_CRON /usr/bin/supervisorctl restart valheim-server" >> $crontab
else
echo "Environment variable RESTART_CRON is empty - no automatic valheim-server restart scheduled"
fi
crontab $crontab
rm -f $crontab

# Notify users of new data paths
if [ -d "/opt/valheim_dl" -o -f "/opt/valheim/valheim_server.x86_64" ]; then
Expand Down
7 changes: 3 additions & 4 deletions valheim-server
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
export SteamAppId=892970

valheim_server_pid=-1
valheim_server_pidfile=/var/run/valheim-server.pid
timeout=20
kill_signal=TERM

Expand Down Expand Up @@ -38,7 +37,7 @@ wait_for_server_download() {
break
else
echo "Valheim Server is not yet downloaded - waiting"
sleep 10
sleep 7
fi
done
}
Expand All @@ -49,7 +48,7 @@ ensure_permissions_after_start() {
ensure_permissions
break
else
sleep 5
sleep 4
fi
done
}
Expand Down Expand Up @@ -103,7 +102,7 @@ shutdown() {
esac
fi
echo "Waiting for Valheim Server with PID $valheim_server_pid to shutdown"
sleep 3
sleep 6
done
if [ -f "$valheim_server_pidfile" ]; then
pid_from_pidfile=$(< "$valheim_server_pidfile")
Expand Down
14 changes: 11 additions & 3 deletions valheim-updater
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@
echo "Running Valheim Server updater as user $USER uid $UID"
just_started=true
next_update=$(date +%s)
run=true

cd /opt/steamcmd
main() {
while :; do
echo $$ > "$valheim_updater_pidfile"
while [ $run = true ]; do
ensure_permissions
update
check_server_restart
next_update=$(($(date +%s)+$UPDATE_INTERVAL))
while [ $(date +%s) -lt $next_update ]; do
while [ $run = true -a $(date +%s) -lt $next_update ]; do
sleep 5
done
done
Expand Down Expand Up @@ -75,8 +77,14 @@ check_server_restart() {

update_now() {
echo "Received signal to check for update"
next_update=$(date +%s)
next_update=0
}

shutdown() {
rm -f "$valheim_updater_pidfile"
run=false
}

trap update_now SIGHUP
trap shutdown SIGINT SIGTERM
main

0 comments on commit 41bbf30

Please sign in to comment.