(background.scan): only init values if they do not exist

This commit is contained in:
Christoph Stenglein 2023-08-16 18:36:53 +02:00
parent 64e5f65eab
commit 2b414703cf
No known key found for this signature in database
GPG Key ID: 43DF8DE289F09EDE
2 changed files with 58 additions and 51 deletions

View File

@ -13,7 +13,7 @@ if [ "$1" == "only-one-loop" ]; then
fi
# start with parameter "install" (to setup service as systemd background running)
if [ "$1" == "install" ]; then
# write systemd service
cat > /etc/systemd/system/background.scan.service <<EOF
# Monitor the RaspiBlitz State
@ -56,7 +56,7 @@ configFile="/mnt/hdd/raspiblitz.conf"
# INFOFILE - persited state data
infoFile="/home/admin/raspiblitz.info"
# better readbale seconds (slightly off to reduce same time window trigger)
# better readable seconds (slightly off to reduce same time window trigger)
MINUTE=60
MINUTE2=115
MINUTE5=290
@ -74,21 +74,21 @@ YEAR=31536000
usermod -G bitcoin root
####################################################################
# INIT
# INIT
####################################################################
# init values
/home/admin/_cache.sh set system_temp_celsius "0"
/home/admin/_cache.sh set system_temp_fahrenheit "0"
/home/admin/_cache.sh set system_count_longscan "0"
/home/admin/_cache.sh set system_count_undervoltage "0"
/home/admin/_cache.sh set system_count_start_blockchain "0"
/home/admin/_cache.sh set system_count_start_lightning "0"
/home/admin/_cache.sh set system_count_start_tui "0"
/home/admin/_cache.sh set btc_default_peers "0"
/home/admin/_cache.sh set btc_default_sync_percentage "0"
/home/admin/_cache.sh set btc_default_address ""
/home/admin/_cache.sh set btc_default_port ""
/home/admin/_cache.sh set system_temp_celsius "0" NX
/home/admin/_cache.sh set system_temp_fahrenheit "0" NX
/home/admin/_cache.sh set system_count_longscan "0" NX
/home/admin/_cache.sh set system_count_undervoltage "0" NX
/home/admin/_cache.sh set system_count_start_blockchain "0" NX
/home/admin/_cache.sh set system_count_start_lightning "0" NX
/home/admin/_cache.sh set system_count_start_tui "0" NX
/home/admin/_cache.sh set btc_default_peers "0" NX
/home/admin/_cache.sh set btc_default_sync_percentage "0" NX
/home/admin/_cache.sh set btc_default_address "" NX
/home/admin/_cache.sh set btc_default_port "" NX
# import all base values from raspiblitz.info
echo "importing: ${infoFile}"
@ -132,8 +132,8 @@ while [ 1 ]
do
####################################################################
# LOOP DATA (BASIC SYSTEM)
# data that is always available
# LOOP DATA (BASIC SYSTEM)
# data that is always available
####################################################################
# check that redis contains init data (detect possible restart of redis)
@ -147,7 +147,7 @@ do
startTime=$(date +%s)
#################
# BASIC SYSTEM
# BASIC SYSTEM
# uptime just do on every run
system_up=$(cat /proc/uptime | grep -o '^[0-9]\+')
@ -396,7 +396,7 @@ do
# update detail infos only when ready (get as value from cache)
source <(/home/admin/_cache.sh meta btc_${CHAIN}net_ready)
if [ "${value}" == "1" ]; then
if [ "${value}" == "1" ]; then
# check if network needs update
source <(/home/admin/_cache.sh valid \
@ -432,7 +432,7 @@ do
/home/admin/_cache.sh set btc_${CHAIN}net_sync_progress "${btc_sync_progress}"
/home/admin/_cache.sh set btc_${CHAIN}net_sync_percentage "${btc_sync_percentage}"
/home/admin/_cache.sh set btc_${CHAIN}net_sync_initialblockdownload "${btc_sync_initialblockdownload}"
if [ "${isDefaultChain}" == "1" ]; then
/home/admin/_cache.sh set btc_default_synced "${btc_synced}"
/home/admin/_cache.sh set btc_default_blocks_headers "${btc_blocks_headers}"
@ -1044,7 +1044,7 @@ do
# if was started with special parameter
if [ "${ONLY_ONE_LOOP}" == "1" ]; then
echo "Exiting because ONLY_ONE_LOOP==1"
exit 0
exit 0
fi
done

View File

@ -5,7 +5,7 @@
# 2) KEY-VALUE STORE for system state infos (REDIS)
# SECURITY NOTE: The files on the RAMDISK can be set with unix file permissions and so restrict certain users access.
# But all data stored in the KEY-VALUE STORE has to be asumed as system-wide public information.
# But all data stored in the KEY-VALUE STORE has to be assumed as system-wide public information.
# command info
if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ "$1" = "-help" ]; then
@ -16,26 +16,26 @@ if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ "$1" = "-help" ];
echo
echo "_cache.sh set [key] [value] [?expire-seconds]"
echo "_cache.sh get [key1] [?key2] [?key3] ..."
echo
echo
echo "_cache.sh increment [key1]"
echo
echo "_cache.sh focus [key] [update-seconds] [?duration-seconds]"
echo "# set in how many seconds value is marked to be rescanned"
echo "# -1 = slowest default update cycle"
echo "# -1 = slowest default update cycle"
echo "# 0 = update on every cycle"
echo "# set a 'duration-seconds' after defaults to -1 (optional)"
echo
echo
echo "_cache.sh meta [key] [?default]"
echo "# get single key with additional metadata:"
echo "# updateseconds= see above"
echo "# stillvalid=0/1 if value is still valid or outdated"
echo "# lasttouch= last update timestamp in unix seconds"
echo
echo
echo "_cache.sh valid [key1] [?key2] [?key3] ..."
echo "# check multiple keys if all are still not outdated"
echo "# use for example to check if a complex call needs"
echo "# to be made that covers multiple single data points"
echo
echo
echo "_cache.sh import [bash-keyvalue-file]"
echo "# import a bash style key-value file into store"
echo
@ -46,7 +46,7 @@ if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ "$1" = "-help" ];
exit 1
fi
# BACKGROUND: we need to build outdated meta info manually,
# BACKGROUND: we need to build outdated meta info manually,
# because there is nothing as "AGE" in redis: https://github.com/redis/redis/issues/1147
# only feature that can be used uis the EXPIRE feature to determine if a value is still valid
@ -55,7 +55,7 @@ META_OUTDATED_SECONDS=":out"
META_LASTTOUCH_TS=":ts"
META_VALID_FLAG=":val"
# path of the raspiblitz.info file (persiting cache values)
# path of the raspiblitz.info file (persisting cache values)
infoFile="/home/admin/raspiblitz.info"
###################
@ -138,39 +138,46 @@ elif [ "$1" = "set" ]; then
# get parameters
keystr=$2
valuestr=$3
expire=$4
expireOrNx=$4
# check that key & value are given
# check that key & value are provided
if [ "${keystr}" == "" ]; then
echo "# Fail: missing parameter"
exit 1
fi
# filter from expire just numbers
expire="${expire//[^0-9.]/}"
# add an expire flag if given
NX=""
if [ "${expireOrNx}" == "NX" ]; then
NX="NX"
else
# filter from expire just numbers
expireOrNx="${expire//[^0-9.]/}"
additionalParams=""
if [ "${expire}" != "" ]; then
# add an expire flag if given
if [ "${expireOrNx}" != "" ]; then
additionalParams="EX ${expire}"
fi
fi
# set in redis key value cache
redis-cli set ${keystr} "${valuestr}" ${additionalParams} 1>/dev/null
redis-cli set ${NX} ${keystr} "${valuestr}" ${additionalParams} 1>/dev/null
# set in redis the timestamp
timestamp=$(date +%s)
redis-cli set ${keystr}${META_LASTTOUCH_TS} "${timestamp}" ${additionalParams} 1>/dev/null
redis-cli set ${NX} ${keystr}${META_LASTTOUCH_TS} "${timestamp}" ${additionalParams} 1>/dev/null
#echo "# lasttouch(${timestamp})"
# check if the value has a outdate policy
outdatesecs=$(redis-cli get ${keystr}${META_OUTDATED_SECONDS})
if [ "${outdatesecs}" == "" ]; then
outdatesecs="-1"
fi
fi
#echo "# outdatesecs(${outdatesecs})"
if [ "${outdatesecs}" != "-1" ]; then
# set exipire valid flag (if its gone - value is considered as outdated)
# set expire valid flag (if its gone - value is considered as outdated)
redis-cli set ${keystr}${META_VALID_FLAG} "1" EX ${outdatesecs} 1>/dev/null
fi
@ -186,7 +193,7 @@ elif [ "$1" = "get" ]; then
position=0
for keystr in $@
do
# skip first parameter
((position++))
if [ $position -eq 1 ]; then
@ -243,7 +250,7 @@ elif [ "$1" = "export" ]; then
# get parameter
keyfilter="${2}*"
# go thru all keys by keyfilter
# go through all keys by keyfilter
keylist=$(redis-cli KEYS "${keyfilter}")
readarray -t arr <<< "${keylist}"
for key in "${arr[@]}";do
@ -266,7 +273,7 @@ elif [ "$1" = "export" ]; then
##################################
# COUNT
# count value up
# increment value
##################################
# set
@ -309,7 +316,7 @@ elif [ "$1" = "focus" ]; then
for key in "${arr[@]}";do
if [ "${key}" == "" ]; then
continue
fi
fi
keyClean=$(echo $key | cut -d ":" -f1)
value=$(redis-cli get "${key}")
echo "${keyClean}=${value}"
@ -324,7 +331,7 @@ elif [ "$1" = "focus" ]; then
exit
fi
# sanatize parameters (if not -1)
# sanitize parameters (if not -1)
outdatesecs="${outdatesecs//[^0-9.]/}"
# check that key & value are given
@ -333,7 +340,7 @@ elif [ "$1" = "focus" ]; then
exit 1
fi
# add an expire flag if given
# add an expire flag if given
additionalParams=""
if [ "${durationsecs//[^0-9.]/}" != "" ]; then
additionalParams="EX ${durationsecs//[^0-9.]/}"
@ -358,13 +365,13 @@ elif [ "$1" = "meta" ]; then
keystr=$2
default=$3
# check that key & value are given
# check that key & value are provided
if [ "${keystr}" == "" ]; then
echo "# Fail: missing parameter"
exit 1
fi
# get redis basic value
# get redis basic value
valuestr=$(redis-cli get ${keystr})
echo "value=\"${valuestr}\""
@ -380,7 +387,7 @@ elif [ "$1" = "meta" ]; then
# get META_OUTDATED_SECONDS
outdatesecs=$(redis-cli get ${keystr}${META_OUTDATED_SECONDS})
if [ "${outdatesecs}" == "" ]; then
# default is -1 --> never outdate
# default is -1 --> never outdate
outdatesecs="-1"
fi
echo "outdatesecs=\"${outdatesecs}\""
@ -406,7 +413,7 @@ elif [ "$1" = "valid" ]; then
lasttouch_overall=""
for keystr in $@
do
# skip first parameter from script - thats the action string
((position++))
if [ $position -eq 1 ]; then
@ -436,7 +443,7 @@ elif [ "$1" = "valid" ]; then
# get outdate police of value (outdated = not valid anymore)
outdatesecs=$(redis-cli get ${keystr}${META_OUTDATED_SECONDS})
#echo "# ${keystr}${META_OUTDATED_SECONDS}=\"${outdatesecs}\""
# if outdate policy is default or -1 ==> never outdated
if [ "${outdatesecs}" == "" ] || [ "${outdatesecs}" == "-1" ]; then
continue
@ -453,7 +460,7 @@ elif [ "$1" = "valid" ]; then
# so valid flag does not exists anymore
# ==> this means value is outdated
# break loop and
# break loop and
echo "stillvalid=\"0\""
exit 0