]> git.corax.cc Git - toolbox/commitdiff
include/inst: Add functions for implementing singleton daemons
authorMatthias Kruk <m@m10k.eu>
Sun, 13 Jun 2021 08:12:48 +0000 (17:12 +0900)
committerMatthias Kruk <m@m10k.eu>
Sun, 13 Jun 2021 08:12:48 +0000 (17:12 +0900)
The inst_start() function does not check if a process is already
running, making it unusable for the implementation of singleton
daemons.
This commit adds the inst_singleton() function, which allows the
caller to implement daemons which can only be run one at a time.

include/inst.sh

index 9e697c9fc2ad62d00294ce3cc44ebb5effe05cb5..678a2ae8774a5a2eff063d5f3954b77d9dc79a57 100644 (file)
@@ -233,3 +233,29 @@ inst_get_status_timestamp() {
        echo "${status%%:*}"
        return 0
 }
+
+inst_count() {
+        local -i num
+
+        if ! num=$(find "$__inst_path" -regex ".*/[0-9]+" | wc -l); then
+                return 1
+        fi
+
+        echo "$num"
+        return 0
+}
+
+inst_singleton() {
+        local args=("$@")
+
+        if (( $(inst_count) > 0 )); then
+                log_error "Another instance is already running"
+                return 1
+        fi
+
+        if ! inst_start "${args[@]}"; then
+                return 1
+        fi
+
+        return 0
+}