Public: anyone can read this; the crew can change it.
The game agent has one compose command: an array every call and the background job share #17261
Merged 35h 29m ago, live in production
sold/agent-compose-array → main 0 commits
1 file
changed +8
−7
src/games/agent.sh +8 −7
| @@ -74,7 +74,9 @@ while [ -n "$(missing)" ]; do | ||
| 74 | 74 | sleep "" |
| 75 | 75 | done |
| 76 | 76 | # The project directory is the box's own, so compose reads the `.env` the forge writes beside it. |
| 77 | compose() { "" compose -p game --project-directory "" -f "/compose.yml" "$@"; } | |
| 77 | # One array, the real program and its flags: timeout(1) and `bash -c` both run it, which a shell | |
| 78 | # function never could be. | |
| 79 | COMPOSE=("" compose -p game --project-directory "" -f "/compose.yml") | |
| 78 | 80 | now() { date -u +%Y-%m-%dT%H:%M:%SZ; } |
| 79 | 81 | # A quick command that must not hang the loop: at most 30 s where timeout(1) is there. |
| 80 | 82 | # Run a command for at most `secs` seconds, timeout(1) or not: a docker that hangs never holds the |
| @@ -82,8 +84,7 @@ now() { date -u +%Y-%m-%dT%H:%M:%SZ; } | ||
| 82 | 84 | capped() { |
| 83 | 85 | local secs="$1" pid i |
| 84 | 86 | shift |
| 85 | # timeout(1) runs a program, never a shell function (`compose` is one): a function takes the loop below. | |
| 86 | if ! declare -F "$1" >/dev/null && command -v timeout >/dev/null 2>&1; then | |
| 87 | if command -v timeout >/dev/null 2>&1; then | |
| 87 | 88 | timeout "" "$@" |
| 88 | 89 | return |
| 89 | 90 | fi |
| @@ -113,7 +114,7 @@ since="$(now)" | ||
| 113 | 114 | # The services as compose sees them, with each one's CPU and memory from docker stats. |
| 114 | 115 | services() { |
| 115 | 116 | local ps stats |
| 116 | ps="$(capped 5 compose ps -a --format json 2>/dev/null | jq -cs 'flatten' 2>/dev/null)" || ps='[]' | |
| 117 | ps="$(capped 5 "" ps -a --format json 2>/dev/null | jq -cs 'flatten' 2>/dev/null)" || ps='[]' | |
| 117 | 118 | [ -n "" ] || ps='[]' |
| 118 | 119 | stats="$(capped 5 "" stats --no-stream --format json 2>/dev/null | jq -cs '.' 2>/dev/null)" || stats='[]' |
| 119 | 120 | [ -n "" ] || stats='[]' |
| @@ -133,7 +134,7 @@ services() { | ||
| 133 | 134 | |
| 134 | 135 | # New log lines since the last sync, at most 200 a service: "hub | text" split at the first bar. |
| 135 | 136 | logs() { |
| 136 | capped 5 compose logs --no-color --since "$1" 2>/dev/null | jq -cRn ' | |
| 137 | capped 5 "" logs --no-color --since "$1" 2>/dev/null | jq -cRn ' | |
| 137 | 138 | [inputs | capture("^(?<s>[^ |]+)[ ]*\\| ?(?<l>.*)$")?] |
| 138 | 139 | | group_by(.s) | map({key: .[0].s, value: (map(.l) | .[-200:])}) | from_entries' 2>/dev/null || echo '{}' |
| 139 | 140 | } |
| @@ -161,7 +162,7 @@ run() { | ||
| 161 | 162 | else |
| 162 | 163 | case "" in |
| 163 | 164 | # Through compose, by service: a container is named game-<service>-1 unless the file says otherwise. |
| 164 | rcon) out="$(bounded30 compose exec -T "" rcon-cli -- "" 2>&1)" || ok=false ;; | |
| 165 | rcon) out="$(bounded30 "" exec -T "" rcon-cli -- "" 2>&1)" || ok=false ;; | |
| 165 | 166 | # Anything that can take long goes to the job queue: the sync never waits on it, and its |
| 166 | 167 | # result is sent when it ends. |
| 167 | 168 | restart | stop | start | pull) |
| @@ -250,7 +251,7 @@ job_tick() { | ||
| 250 | 251 | now >"/.job.since" |
| 251 | 252 | # Its own bash, named on its command line ($0), writing its exit status only when it ends. |
| 252 | 253 | # shellcheck disable=SC2086,SC2016 # the verb and its words, each a safe name; the script is single-quoted on purpose |
| 253 | DOCKER="" DIR="" bash -c '"$DOCKER" compose -p game --project-directory "$DIR" -f "$DIR/compose.yml" "$@" >"$DIR/.up.log" 2>&1; echo $? >"$DIR/.job.rc"' "" & | |
| 254 | DIR="" bash -c '"$@" >"$DIR/.up.log" 2>&1; echo $? >"$DIR/.job.rc"' "" "" & | |
| 254 | 255 | echo $! >"/.job.pid" |
| 255 | 256 | } |
| 256 | 257 |
new above
offline