mirror of
https://github.com/rootzoll/raspiblitz.git
synced 2025-02-28 16:58:03 +01:00
(background.scan): only init values if they do not exist
This commit is contained in:
parent
accea759f6
commit
8119b33f3a
2 changed files with 58 additions and 51 deletions
|
@ -13,7 +13,7 @@ if [ "$1" == "only-one-loop" ]; then
|
||||||
fi
|
fi
|
||||||
# start with parameter "install" (to setup service as systemd background running)
|
# start with parameter "install" (to setup service as systemd background running)
|
||||||
if [ "$1" == "install" ]; then
|
if [ "$1" == "install" ]; then
|
||||||
|
|
||||||
# write systemd service
|
# write systemd service
|
||||||
cat > /etc/systemd/system/background.scan.service <<EOF
|
cat > /etc/systemd/system/background.scan.service <<EOF
|
||||||
# Monitor the RaspiBlitz State
|
# Monitor the RaspiBlitz State
|
||||||
|
@ -56,7 +56,7 @@ configFile="/mnt/hdd/raspiblitz.conf"
|
||||||
# INFOFILE - persited state data
|
# INFOFILE - persited state data
|
||||||
infoFile="/home/admin/raspiblitz.info"
|
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
|
MINUTE=60
|
||||||
MINUTE2=115
|
MINUTE2=115
|
||||||
MINUTE5=290
|
MINUTE5=290
|
||||||
|
@ -74,21 +74,21 @@ YEAR=31536000
|
||||||
usermod -G bitcoin root
|
usermod -G bitcoin root
|
||||||
|
|
||||||
####################################################################
|
####################################################################
|
||||||
# INIT
|
# INIT
|
||||||
####################################################################
|
####################################################################
|
||||||
|
|
||||||
# init values
|
# init values
|
||||||
/home/admin/_cache.sh set system_temp_celsius "0"
|
/home/admin/_cache.sh set system_temp_celsius "0" NX
|
||||||
/home/admin/_cache.sh set system_temp_fahrenheit "0"
|
/home/admin/_cache.sh set system_temp_fahrenheit "0" NX
|
||||||
/home/admin/_cache.sh set system_count_longscan "0"
|
/home/admin/_cache.sh set system_count_longscan "0" NX
|
||||||
/home/admin/_cache.sh set system_count_undervoltage "0"
|
/home/admin/_cache.sh set system_count_undervoltage "0" NX
|
||||||
/home/admin/_cache.sh set system_count_start_blockchain "0"
|
/home/admin/_cache.sh set system_count_start_blockchain "0" NX
|
||||||
/home/admin/_cache.sh set system_count_start_lightning "0"
|
/home/admin/_cache.sh set system_count_start_lightning "0" NX
|
||||||
/home/admin/_cache.sh set system_count_start_tui "0"
|
/home/admin/_cache.sh set system_count_start_tui "0" NX
|
||||||
/home/admin/_cache.sh set btc_default_peers "0"
|
/home/admin/_cache.sh set btc_default_peers "0" NX
|
||||||
/home/admin/_cache.sh set btc_default_sync_percentage "0"
|
/home/admin/_cache.sh set btc_default_sync_percentage "0" NX
|
||||||
/home/admin/_cache.sh set btc_default_address ""
|
/home/admin/_cache.sh set btc_default_address "" NX
|
||||||
/home/admin/_cache.sh set btc_default_port ""
|
/home/admin/_cache.sh set btc_default_port "" NX
|
||||||
|
|
||||||
# import all base values from raspiblitz.info
|
# import all base values from raspiblitz.info
|
||||||
echo "importing: ${infoFile}"
|
echo "importing: ${infoFile}"
|
||||||
|
@ -132,8 +132,8 @@ while [ 1 ]
|
||||||
do
|
do
|
||||||
|
|
||||||
####################################################################
|
####################################################################
|
||||||
# LOOP DATA (BASIC SYSTEM)
|
# LOOP DATA (BASIC SYSTEM)
|
||||||
# data that is always available
|
# data that is always available
|
||||||
####################################################################
|
####################################################################
|
||||||
|
|
||||||
# check that redis contains init data (detect possible restart of redis)
|
# check that redis contains init data (detect possible restart of redis)
|
||||||
|
@ -147,7 +147,7 @@ do
|
||||||
startTime=$(date +%s)
|
startTime=$(date +%s)
|
||||||
|
|
||||||
#################
|
#################
|
||||||
# BASIC SYSTEM
|
# BASIC SYSTEM
|
||||||
|
|
||||||
# uptime just do on every run
|
# uptime just do on every run
|
||||||
system_up=$(cat /proc/uptime | grep -o '^[0-9]\+')
|
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)
|
# update detail infos only when ready (get as value from cache)
|
||||||
source <(/home/admin/_cache.sh meta btc_${CHAIN}net_ready)
|
source <(/home/admin/_cache.sh meta btc_${CHAIN}net_ready)
|
||||||
if [ "${value}" == "1" ]; then
|
if [ "${value}" == "1" ]; then
|
||||||
|
|
||||||
# check if network needs update
|
# check if network needs update
|
||||||
source <(/home/admin/_cache.sh valid \
|
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_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_percentage "${btc_sync_percentage}"
|
||||||
/home/admin/_cache.sh set btc_${CHAIN}net_sync_initialblockdownload "${btc_sync_initialblockdownload}"
|
/home/admin/_cache.sh set btc_${CHAIN}net_sync_initialblockdownload "${btc_sync_initialblockdownload}"
|
||||||
|
|
||||||
if [ "${isDefaultChain}" == "1" ]; then
|
if [ "${isDefaultChain}" == "1" ]; then
|
||||||
/home/admin/_cache.sh set btc_default_synced "${btc_synced}"
|
/home/admin/_cache.sh set btc_default_synced "${btc_synced}"
|
||||||
/home/admin/_cache.sh set btc_default_blocks_headers "${btc_blocks_headers}"
|
/home/admin/_cache.sh set btc_default_blocks_headers "${btc_blocks_headers}"
|
||||||
|
@ -1044,7 +1044,7 @@ do
|
||||||
# if was started with special parameter
|
# if was started with special parameter
|
||||||
if [ "${ONLY_ONE_LOOP}" == "1" ]; then
|
if [ "${ONLY_ONE_LOOP}" == "1" ]; then
|
||||||
echo "Exiting because ONLY_ONE_LOOP==1"
|
echo "Exiting because ONLY_ONE_LOOP==1"
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
done
|
done
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
# 2) KEY-VALUE STORE for system state infos (REDIS)
|
# 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.
|
# 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
|
# command info
|
||||||
if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ "$1" = "-help" ]; then
|
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
|
||||||
echo "_cache.sh set [key] [value] [?expire-seconds]"
|
echo "_cache.sh set [key] [value] [?expire-seconds]"
|
||||||
echo "_cache.sh get [key1] [?key2] [?key3] ..."
|
echo "_cache.sh get [key1] [?key2] [?key3] ..."
|
||||||
echo
|
echo
|
||||||
echo "_cache.sh increment [key1]"
|
echo "_cache.sh increment [key1]"
|
||||||
echo
|
echo
|
||||||
echo "_cache.sh focus [key] [update-seconds] [?duration-seconds]"
|
echo "_cache.sh focus [key] [update-seconds] [?duration-seconds]"
|
||||||
echo "# set in how many seconds value is marked to be rescanned"
|
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 "# 0 = update on every cycle"
|
||||||
echo "# set a 'duration-seconds' after defaults to -1 (optional)"
|
echo "# set a 'duration-seconds' after defaults to -1 (optional)"
|
||||||
echo
|
echo
|
||||||
echo "_cache.sh meta [key] [?default]"
|
echo "_cache.sh meta [key] [?default]"
|
||||||
echo "# get single key with additional metadata:"
|
echo "# get single key with additional metadata:"
|
||||||
echo "# updateseconds= see above"
|
echo "# updateseconds= see above"
|
||||||
echo "# stillvalid=0/1 if value is still valid or outdated"
|
echo "# stillvalid=0/1 if value is still valid or outdated"
|
||||||
echo "# lasttouch= last update timestamp in unix seconds"
|
echo "# lasttouch= last update timestamp in unix seconds"
|
||||||
echo
|
echo
|
||||||
echo "_cache.sh valid [key1] [?key2] [?key3] ..."
|
echo "_cache.sh valid [key1] [?key2] [?key3] ..."
|
||||||
echo "# check multiple keys if all are still not outdated"
|
echo "# check multiple keys if all are still not outdated"
|
||||||
echo "# use for example to check if a complex call needs"
|
echo "# use for example to check if a complex call needs"
|
||||||
echo "# to be made that covers multiple single data points"
|
echo "# to be made that covers multiple single data points"
|
||||||
echo
|
echo
|
||||||
echo "_cache.sh import [bash-keyvalue-file]"
|
echo "_cache.sh import [bash-keyvalue-file]"
|
||||||
echo "# import a bash style key-value file into store"
|
echo "# import a bash style key-value file into store"
|
||||||
echo
|
echo
|
||||||
|
@ -46,7 +46,7 @@ if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ "$1" = "-help" ];
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
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
|
# 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
|
# 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_LASTTOUCH_TS=":ts"
|
||||||
META_VALID_FLAG=":val"
|
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"
|
infoFile="/home/admin/raspiblitz.info"
|
||||||
|
|
||||||
###################
|
###################
|
||||||
|
@ -138,39 +138,46 @@ elif [ "$1" = "set" ]; then
|
||||||
# get parameters
|
# get parameters
|
||||||
keystr=$2
|
keystr=$2
|
||||||
valuestr=$3
|
valuestr=$3
|
||||||
expire=$4
|
expireOrNx=$4
|
||||||
|
|
||||||
# check that key & value are given
|
# check that key & value are provided
|
||||||
if [ "${keystr}" == "" ]; then
|
if [ "${keystr}" == "" ]; then
|
||||||
echo "# Fail: missing parameter"
|
echo "# Fail: missing parameter"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
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=""
|
additionalParams=""
|
||||||
if [ "${expire}" != "" ]; then
|
# add an expire flag if given
|
||||||
|
if [ "${expireOrNx}" != "" ]; then
|
||||||
additionalParams="EX ${expire}"
|
additionalParams="EX ${expire}"
|
||||||
fi
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
# set in redis key value cache
|
# 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
|
# set in redis the timestamp
|
||||||
timestamp=$(date +%s)
|
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})"
|
#echo "# lasttouch(${timestamp})"
|
||||||
|
|
||||||
# check if the value has a outdate policy
|
# check if the value has a outdate policy
|
||||||
outdatesecs=$(redis-cli get ${keystr}${META_OUTDATED_SECONDS})
|
outdatesecs=$(redis-cli get ${keystr}${META_OUTDATED_SECONDS})
|
||||||
if [ "${outdatesecs}" == "" ]; then
|
if [ "${outdatesecs}" == "" ]; then
|
||||||
outdatesecs="-1"
|
outdatesecs="-1"
|
||||||
fi
|
fi
|
||||||
#echo "# outdatesecs(${outdatesecs})"
|
#echo "# outdatesecs(${outdatesecs})"
|
||||||
if [ "${outdatesecs}" != "-1" ]; then
|
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
|
redis-cli set ${keystr}${META_VALID_FLAG} "1" EX ${outdatesecs} 1>/dev/null
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
@ -186,7 +193,7 @@ elif [ "$1" = "get" ]; then
|
||||||
position=0
|
position=0
|
||||||
for keystr in $@
|
for keystr in $@
|
||||||
do
|
do
|
||||||
|
|
||||||
# skip first parameter
|
# skip first parameter
|
||||||
((position++))
|
((position++))
|
||||||
if [ $position -eq 1 ]; then
|
if [ $position -eq 1 ]; then
|
||||||
|
@ -243,7 +250,7 @@ elif [ "$1" = "export" ]; then
|
||||||
# get parameter
|
# get parameter
|
||||||
keyfilter="${2}*"
|
keyfilter="${2}*"
|
||||||
|
|
||||||
# go thru all keys by keyfilter
|
# go through all keys by keyfilter
|
||||||
keylist=$(redis-cli KEYS "${keyfilter}")
|
keylist=$(redis-cli KEYS "${keyfilter}")
|
||||||
readarray -t arr <<< "${keylist}"
|
readarray -t arr <<< "${keylist}"
|
||||||
for key in "${arr[@]}";do
|
for key in "${arr[@]}";do
|
||||||
|
@ -266,7 +273,7 @@ elif [ "$1" = "export" ]; then
|
||||||
|
|
||||||
##################################
|
##################################
|
||||||
# COUNT
|
# COUNT
|
||||||
# count value up
|
# increment value
|
||||||
##################################
|
##################################
|
||||||
|
|
||||||
# set
|
# set
|
||||||
|
@ -309,7 +316,7 @@ elif [ "$1" = "focus" ]; then
|
||||||
for key in "${arr[@]}";do
|
for key in "${arr[@]}";do
|
||||||
if [ "${key}" == "" ]; then
|
if [ "${key}" == "" ]; then
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
keyClean=$(echo $key | cut -d ":" -f1)
|
keyClean=$(echo $key | cut -d ":" -f1)
|
||||||
value=$(redis-cli get "${key}")
|
value=$(redis-cli get "${key}")
|
||||||
echo "${keyClean}=${value}"
|
echo "${keyClean}=${value}"
|
||||||
|
@ -324,7 +331,7 @@ elif [ "$1" = "focus" ]; then
|
||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# sanatize parameters (if not -1)
|
# sanitize parameters (if not -1)
|
||||||
outdatesecs="${outdatesecs//[^0-9.]/}"
|
outdatesecs="${outdatesecs//[^0-9.]/}"
|
||||||
|
|
||||||
# check that key & value are given
|
# check that key & value are given
|
||||||
|
@ -333,7 +340,7 @@ elif [ "$1" = "focus" ]; then
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# add an expire flag if given
|
# add an expire flag if given
|
||||||
additionalParams=""
|
additionalParams=""
|
||||||
if [ "${durationsecs//[^0-9.]/}" != "" ]; then
|
if [ "${durationsecs//[^0-9.]/}" != "" ]; then
|
||||||
additionalParams="EX ${durationsecs//[^0-9.]/}"
|
additionalParams="EX ${durationsecs//[^0-9.]/}"
|
||||||
|
@ -358,13 +365,13 @@ elif [ "$1" = "meta" ]; then
|
||||||
keystr=$2
|
keystr=$2
|
||||||
default=$3
|
default=$3
|
||||||
|
|
||||||
# check that key & value are given
|
# check that key & value are provided
|
||||||
if [ "${keystr}" == "" ]; then
|
if [ "${keystr}" == "" ]; then
|
||||||
echo "# Fail: missing parameter"
|
echo "# Fail: missing parameter"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# get redis basic value
|
# get redis basic value
|
||||||
valuestr=$(redis-cli get ${keystr})
|
valuestr=$(redis-cli get ${keystr})
|
||||||
echo "value=\"${valuestr}\""
|
echo "value=\"${valuestr}\""
|
||||||
|
|
||||||
|
@ -380,7 +387,7 @@ elif [ "$1" = "meta" ]; then
|
||||||
# get META_OUTDATED_SECONDS
|
# get META_OUTDATED_SECONDS
|
||||||
outdatesecs=$(redis-cli get ${keystr}${META_OUTDATED_SECONDS})
|
outdatesecs=$(redis-cli get ${keystr}${META_OUTDATED_SECONDS})
|
||||||
if [ "${outdatesecs}" == "" ]; then
|
if [ "${outdatesecs}" == "" ]; then
|
||||||
# default is -1 --> never outdate
|
# default is -1 --> never outdate
|
||||||
outdatesecs="-1"
|
outdatesecs="-1"
|
||||||
fi
|
fi
|
||||||
echo "outdatesecs=\"${outdatesecs}\""
|
echo "outdatesecs=\"${outdatesecs}\""
|
||||||
|
@ -406,7 +413,7 @@ elif [ "$1" = "valid" ]; then
|
||||||
lasttouch_overall=""
|
lasttouch_overall=""
|
||||||
for keystr in $@
|
for keystr in $@
|
||||||
do
|
do
|
||||||
|
|
||||||
# skip first parameter from script - thats the action string
|
# skip first parameter from script - thats the action string
|
||||||
((position++))
|
((position++))
|
||||||
if [ $position -eq 1 ]; then
|
if [ $position -eq 1 ]; then
|
||||||
|
@ -436,7 +443,7 @@ elif [ "$1" = "valid" ]; then
|
||||||
# get outdate police of value (outdated = not valid anymore)
|
# get outdate police of value (outdated = not valid anymore)
|
||||||
outdatesecs=$(redis-cli get ${keystr}${META_OUTDATED_SECONDS})
|
outdatesecs=$(redis-cli get ${keystr}${META_OUTDATED_SECONDS})
|
||||||
#echo "# ${keystr}${META_OUTDATED_SECONDS}=\"${outdatesecs}\""
|
#echo "# ${keystr}${META_OUTDATED_SECONDS}=\"${outdatesecs}\""
|
||||||
|
|
||||||
# if outdate policy is default or -1 ==> never outdated
|
# if outdate policy is default or -1 ==> never outdated
|
||||||
if [ "${outdatesecs}" == "" ] || [ "${outdatesecs}" == "-1" ]; then
|
if [ "${outdatesecs}" == "" ] || [ "${outdatesecs}" == "-1" ]; then
|
||||||
continue
|
continue
|
||||||
|
@ -453,7 +460,7 @@ elif [ "$1" = "valid" ]; then
|
||||||
|
|
||||||
# so valid flag does not exists anymore
|
# so valid flag does not exists anymore
|
||||||
# ==> this means value is outdated
|
# ==> this means value is outdated
|
||||||
# break loop and
|
# break loop and
|
||||||
echo "stillvalid=\"0\""
|
echo "stillvalid=\"0\""
|
||||||
exit 0
|
exit 0
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue