]> git.corax.cc Git - toolbox/commitdiff
include/inst: Implement instance status messages
authorMatthias Kruk <m@m10k.eu>
Sun, 6 Jun 2021 02:03:56 +0000 (11:03 +0900)
committerMatthias Kruk <m@m10k.eu>
Sun, 6 Jun 2021 02:14:26 +0000 (11:14 +0900)
In order to make the status of an instance visible to users and other
processes, functions are needed that can be used to set status messages
from within an instance.
This commit adds status messages for instances, which can be manipulated
through the following functions.

 - inst_set_status(): Set the status of the running instance
 - inst_get_status_message(): The the status message of an instance
 - inst_get_status_timestamp(): Get the timestamp of a status message
 - inst_get_status(): Get status message and timestamp of an instance

include/inst.sh

index 8e900f50971e02967ebe7cd75e9df8f7670bbaae..1bd5842e72afdb416debf3d59248e668ba0592e9 100644 (file)
@@ -64,6 +64,9 @@ inst_list() {
                local semval
                local state
                local argv
+               local status_text
+               local status_time
+               local timestamp
 
                owner="${sem##*/}"
                semval=$(sem_peek "$sem")
@@ -72,13 +75,22 @@ inst_list() {
                        continue
                fi
 
+               if ! status_text=$(inst_get_status_message "$owner") ||
+                  ! status_time=$(inst_get_status_timestamp "$owner"); then
+                       continue
+               fi
+
+               if ! timestamp=$(date --date="@$status_time" +"%Y-%m-%d %H:%M:%S %z"); then
+                       continue
+               fi
+
                if (( semval > 0 )); then
                        state="STOPPING"
                else
                        state="RUNNING"
                fi
 
-               echo "$owner $state $__inst_name $argv"
+               echo "$owner $state [$timestamp:$status_text] $__inst_name $argv"
        done < <(find "$__inst_path" -regex ".*/[0-9]+")
 
        return 0
@@ -114,12 +126,17 @@ _inst_run() {
        local ret
 
        declare -xgr __inst_sem="$__inst_path/$BASHPID"
+       declare -xgr __inst_status="$__inst_path/$BASHPID.status"
 
        if ! opt_get_argv > "$__inst_path/$BASHPID.argv"; then
                log_error "Could not save args"
                return 1
        fi
 
+       if ! inst_set_status "unset"; then
+               return 1
+       fi
+
        if ! sem_init "$__inst_sem" 0; then
                log_error "Could not initialize semaphore $__inst_sem"
                return 1
@@ -144,3 +161,59 @@ inst_start() {
 
        return 0
 }
+
+inst_set_status() {
+       local status="$1"
+
+       local timestamp
+
+       if ! timestamp=$(date +"%s"); then
+               log_error "Couldn't make timestamp"
+               return 1
+       fi
+
+       if ! echo "$timestamp:$status" > "$__inst_status"; then
+               log_error "Could not write to $__inst_status"
+               return 1
+       fi
+
+       return 0
+}
+
+inst_get_status() {
+       local pid="$1"
+
+       local status
+
+       if ! status=$(< "$__inst_path/$pid.status"); then
+               log_error "Could not read from $__inst_path/$pid.status"
+               return 1
+       fi
+
+       echo "$status"
+       return 0
+}
+
+inst_get_status_message() {
+       local pid="$1"
+
+       if ! status=$(inst_get_status "$pid"); then
+               return 1
+       fi
+
+       echo "${status#*:}"
+       return 0
+}
+
+inst_get_status_timestamp() {
+       local pid="$1"
+
+       local status
+
+       if ! status=$(inst_get_status "$pid"); then
+               return 1
+       fi
+
+       echo "${status%%:*}"
+       return 0
+}