From 1b6513ee836c63315d5a974322d57d0c1a6d8a65 Mon Sep 17 00:00:00 2001 From: TheiLLeniumStudios <104288623+TheiLLeniumStudios@users.noreply.github.com> Date: Thu, 9 Feb 2023 15:50:41 +0000 Subject: [PATCH] Support for restarting the whole server when there are 0 players --- Dockerfile | 2 +- README.md | 1 + action.yml | 2 +- entrypoint.sh | 26 +++++++++++++++++--------- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/Dockerfile b/Dockerfile index faf1d3f..8eebf34 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ # Container image that runs your code FROM alpine:3.17 -RUN apk add --no-cache git +RUN apk add --no-cache git jq # Use icecon instead. For some reason rcon-cli doesn't send a valid command #ARG RCON_VERSION="0.10.2" diff --git a/README.md b/README.md index 50749cf..da3ae8d 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ This allows you, as a server owner to have a Git Managed workflow for your serve | serverPort | Port of the FiveM server | false | 30120 | | resourcesFolder | Resources folder name | false | resources | | resourcesToIgnore | List of resources that you want to ignore separated by spaces and not restart when changes are made to them | false | | +| restartServerWhen0Players | Restart the server instead when there are no players on the server. (Takes priority over `restartIndividualReesources`) | false | false | ## How to set up diff --git a/action.yml b/action.yml index 728773c..ce809b2 100644 --- a/action.yml +++ b/action.yml @@ -23,7 +23,7 @@ inputs: required: false default: "resources" restartServerWhen0Players: - description: "Restart the server instead when there are no players on the server" + description: "Restart the server instead when there are no players on the server. (Takes priority over restartIndividualReesources)" required: false default: false type: boolean diff --git a/entrypoint.sh b/entrypoint.sh index e3b37bd..0dc7ecd 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -23,10 +23,14 @@ append_if_not_exists() { fi } +icecon_command() { + icecon --command "$1" "${SERVER_IP}:${SERVER_PORT}" "${RCON_PASSWORD}" +} -is_array_empty() { - local array_str="$1" - [ -z "$array_str" ] +get_player_count() { + response=$(curl -s "${SERVER_IP}:${SERVER_PORT}/players.json") + player_count=$(echo "$response" | jq 'length') + echo "$player_count" } RESTART_INDIVIDUAL_RESOURCES=$1 @@ -64,21 +68,25 @@ for changed in $DIFF; do done unset IFS -if ! is_array_empty "$resources_to_restart"; then - if [ "$RESTART_INDIVIDUAL_RESOURCES" = true ]; then +if [ -z "$resources_to_restart" ]; then + echo "Nothing to restart" +else + player_count=$(get_player_count) + if [ "$RESTART_SERVER_WHEN_0_PLAYERS" = true ] && [ "$player_count" -eq 0 ]; then + echo "Will restart the whole server due to 0 players" + icecon_command "quit" + elif [ "$RESTART_INDIVIDUAL_RESOURCES" = true ]; then echo "Will restart individual resources" for resource in $resources_to_restart; do if exists_in_array "${resource}" "${IGNORED_RESOURCES}"; then echo "Ignoring restart of the resource ${resource}" else echo "Restarting ${resource}" - icecon --command "ensure ${resource}" ${SERVER_IP}:${SERVER_PORT} ${RCON_PASSWORD} + icecon_command "ensure ${resource}" fi done else echo "Will restart the whole server" - icecon --command "quit" "${SERVER_IP}:${SERVER_PORT}" "${RCON_PASSWORD}" + icecon_command "quit" fi -else - echo "Nothing to restart" fi