diff --git a/install.sh b/install.sh index 49f243c..9740527 100755 --- a/install.sh +++ b/install.sh @@ -1,6 +1,6 @@ #!/bin/bash -### version: 1.10.8 +### version: 1.11.0 # ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.# # Nightscout Mikr.us setup script # @@ -16,7 +16,7 @@ # This file is automatically generated. Do not modify it directly! # Instead, modify the source files in the src directory and run the build script! # -# Build time: 2026.01.06 19:11 +# Build time: 2026.03.09 16:28 @@ -46,6 +46,8 @@ WATCHDOG_TIME_FILE=/srv/nightscout/data/watchdog_time WATCHDOG_LOG_FILE=/srv/nightscout/data/watchdog.log WATCHDOG_FAILURES_FILE=/srv/nightscout/data/watchdog-failures.log WATCHDOG_CRON_LOG=/srv/nightscout/data/watchdog-cron.log +DOMAIN_NAME_FILE=/srv/nightscout/data/domain_name +DOMAIN_UPDATE_TIMESTAMP=/srv/nightscout/data/domain_update_timestamp SUPPORT_LOG=/srv/nightscout/data/support.log EVENTS_DB=/srv/nightscout/data/events.env UPDATE_CHANNEL_FILE=/srv/nightscout/data/update_channel @@ -61,8 +63,8 @@ DISK_LOW_MAIL=5184000 # == 60 days in seconds DISK_CRITICAL_WARNING=104857600 # == 100 MiB DISK_CRITICAL_MAIL=604800 # == 7 days in seconds DOCKER_DOWN_MAIL=604800 # == 7 days in seconds -SCRIPT_VERSION="1.10.8" #auto-update -SCRIPT_BUILD_TIME="2026.01.06" #auto-update +SCRIPT_VERSION="1.11.0" #auto-update +SCRIPT_BUILD_TIME="2026.03.09" #auto-update FORCE_DEBUG_LOG="" NONINTERACTIVE_MODE="false" EXECUTED="true" @@ -1529,6 +1531,15 @@ get_watchdog_status() { } +watchdog_run() { + local host_hash + host_hash=$(printf '%s' "$(hostname)" | md5sum | awk '{print $1}' | cut -c1-8 | xargs printf '%d' 16 16) + local offset=$(( host_hash % 300 )) + echo "Watchdog delay: sleeping ${offset}s (host-based offset)" + sleep "$offset" + watchdog_check +} + watchdog_check() { echo "---------------------------" echo " Nightscout Watchdog mode" @@ -1630,6 +1641,7 @@ watchdog_check() { else WATCHDOG_STATUS="domain_failed" + invalidate_domain_cache fi else @@ -2259,7 +2271,7 @@ update_logto() { fi } -get_td_domain() { +get_td_domain_from_api() { local MHOST=$(hostname) if ! [[ "$MHOST" =~ [a-zA-Z]{2,16}[0-9]{3} ]]; then MIKRUS_APIKEY=$(cat "/klucz_api") @@ -2272,6 +2284,63 @@ get_td_domain() { curl -sd "srv=$MHOST&key=$APIKEY" https://api.mikr.us/domain | jq -r ".[].name" | grep ".ns.techdiab.pl" | head -n 1 } +invalidate_domain_cache() { + echo "" >"$DOMAIN_UPDATE_TIMESTAMP" + echo "" >"$DOMAIN_NAME_FILE" +} + +get_td_domain() { + local domain="" + local cache_age=999999 + local now + now=$(date +%s) + + # Check if we have a valid timestamp and cached domain + if [[ -f "$DOMAIN_UPDATE_TIMESTAMP" && -f "$DOMAIN_NAME_FILE" ]]; then + local last_update + last_update=$(cat "$DOMAIN_UPDATE_TIMESTAMP" | tr -d '[:space:]') + if [[ -n "$last_update" && "$last_update" =~ ^[0-9]+$ ]]; then + cache_age=$((now - last_update)) + fi + fi + + # Use cache if it's less than 24 hours old and contains a valid (non-empty) domain + if ((cache_age < 86400)); then + local cached_domain + cached_domain=$(cat "$DOMAIN_NAME_FILE" 2>/dev/null | tr -d '[:space:]') + if [[ -n "$cached_domain" ]]; then + if [[ "$UPDATE_CHANNEL" == "develop" || "$FORCE_DEBUG_LOG" == "1" ]]; then + echo "domain: from cache (${cache_age}s old): $cached_domain" >>"$DEBUG_LOG_FILE" + fi + echo "$cached_domain" + return + fi + fi + + # Cache miss, stale, or empty cached value - fetch from API + local raw_domain + raw_domain=$(get_td_domain_from_api) + domain=$(echo "$raw_domain" | tr -d '[:space:]') + + # Save result to cache; empty result is also saved so it can be detected as + # "unknown" - empty cached value causes re-check on every subsequent call + echo "$domain" >"$DOMAIN_NAME_FILE" + if [[ -n "$domain" ]]; then + echo "$now" >"$DOMAIN_UPDATE_TIMESTAMP" + if [[ "$UPDATE_CHANNEL" == "develop" || "$FORCE_DEBUG_LOG" == "1" ]]; then + echo "domain: freshly retrieved from API: $domain" >>"$DEBUG_LOG_FILE" + fi + else + # Clear timestamp so next call always retries the API when domain is empty + echo "" >"$DOMAIN_UPDATE_TIMESTAMP" + if [[ "$UPDATE_CHANNEL" == "develop" || "$FORCE_DEBUG_LOG" == "1" ]]; then + echo "domain: API returned empty result, cache cleared" >>"$DEBUG_LOG_FILE" + fi + fi + + echo "$domain" +} + get_domain_status() { local domain=$(get_td_domain) local domainLen=${#domain} @@ -2989,7 +3058,7 @@ parse_commandline_args() { if [ "$WATCHDOGMODE" = "true" ]; then startup_version startup_debug - watchdog_check + watchdog_run fi } @@ -3265,6 +3334,8 @@ uninstall_menu() { version_menu ;; "E)") + cachedMenuDomain='' + invalidate_domain_cache if ! [[ "$0" =~ .*"/usr/bin/nightscout-tool" ]]; then okdlg "Opcja niedostępna" \ @@ -3734,6 +3805,8 @@ main_menu() { case $CHOICE in "S)") + cachedMenuDomain='' + invalidate_domain_cache status_menu ;; "P)") diff --git a/updated b/updated index 822a844..4387c62 100644 --- a/updated +++ b/updated @@ -1 +1 @@ -2026-01-06T19:11:33.574Z \ No newline at end of file +2026-03-09T16:28:23.043Z \ No newline at end of file