From f946a8bdffcbca6ee5a4bf657603a0e6334e4e33 Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Sun, 13 Jun 2021 17:12:48 +0900 Subject: [PATCH] include/inst: Add functions for implementing singleton daemons 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 | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/include/inst.sh b/include/inst.sh index 9e697c9..678a2ae 100644 --- a/include/inst.sh +++ b/include/inst.sh @@ -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 +} -- 2.47.3