diff options
author | Malfurious <m@lfurio.us> | 2023-11-09 18:01:47 -0500 |
---|---|---|
committer | Malfurious <m@lfurio.us> | 2023-11-15 23:11:08 -0500 |
commit | bc9a915a2709fe7c50f826183ce4d2f6b1816052 (patch) | |
tree | b4773b932d200afe7a958b2a1678db97ae275434 | |
parent | 8ead8ec8ac5e4bad5d4f03de005f89212fd8a495 (diff) | |
download | cychedelic-bc9a915a2709fe7c50f826183ce4d2f6b1816052.tar.gz cychedelic-bc9a915a2709fe7c50f826183ce4d2f6b1816052.zip |
dmt: Implement API endpoint /api/job/{jobnum}
This returns all details related to the requested job number as well as a
tail of the job log file. If `jobnum` is omited, details of the latest
job are returned.
Signed-off-by: Malfurious <m@lfurio.us>
-rw-r--r-- | dmt/config.sh | 2 | ||||
-rwxr-xr-x | dmt/dmt | 34 |
2 files changed, 35 insertions, 1 deletions
diff --git a/dmt/config.sh b/dmt/config.sh new file mode 100644 index 0000000..a19e14f --- /dev/null +++ b/dmt/config.sh @@ -0,0 +1,2 @@ +# Number of lines of text to show per job on the main page +CYCHE_LOG_TAIL_LENGTH=25 @@ -7,11 +7,17 @@ CYCHE_LOG_DIR="/data/logs" CYCHE_STATUS_FILE="/data/status" CYCHE_VERSION_FILE="/version" +source config.sh integer() { [ "$1" -eq "$1" ] >/dev/null 2>&1 } +escape() { + sed 's/\\/\\\\/g; s/\r/\\r/g; s/\t/\\t/g; s/"/\\"/g' \ + | awk '{printf "%s\\n", $0}' +} + newest_job() { ls -A "$CYCHE_LOG_DIR" | sort -rn | head -n1 } @@ -20,6 +26,10 @@ oldest_job() { ls -A "$CYCHE_LOG_DIR" | sort -n | head -n1 } +log_tail() { + tail -n "$CYCHE_LOG_TAIL_LENGTH" "$CYCHE_LOG_DIR/$1/log" | sed 's/\r//g' +} + api_status() { status=$(cat "$CYCHE_STATUS_FILE") [ "$status" == "" ] && active="false" || active="true" @@ -33,4 +43,26 @@ api_status() { printf '"oldest":%i}' "$(oldest_job)" } -api_status +api_job() { + [ -z "$1" ] && job="$(newest_job)" || job="$1" + jobdir="$CYCHE_LOG_DIR/$job" + + if [ -d "$jobdir" ]; then + status=$(cat "$CYCHE_STATUS_FILE") + + result=$( ([ "$status" == "$job" ] && echo "active") \ + || ([ -f "$jobdir/fail" ] && echo "fail") \ + || echo "pass" ) + + printf 'Content-type: text/json\n\n{' + printf '"job":%i,' "$job" + printf '"hash":"%s",' "$(cat "$jobdir/hash")" + printf '"reason":"%s",' "$(cat "$jobdir/reason")" + printf '"service":"%s",' "$(cat "$jobdir/service")" + printf '"time":%i,' "$(cat "$jobdir/time")" + printf '"result":"%s",' "$result" + printf '"log":"%s"}' "$(log_tail "$job" | escape)" + fi +} + +api_job |