fix to fetch data from api in parallel

This commit is contained in:
shakemid 2024-03-25 16:48:44 +09:00
parent 6f88755276
commit 8e49ddc2e7
1 changed files with 15 additions and 12 deletions

View File

@ -106,11 +106,17 @@ EOF
} }
fetch() { fetch() {
local deviceid func val local deviceid func ref
for deviceid in ${deviceids} for deviceid in ${deviceids}
do do
local $( do_fetch "${deviceid}" ) do_fetch "${deviceid}" &
done
wait
for deviceid in ${deviceids}
do
local padding=1 $( cat "${MUNIN_STATEFILE}_${deviceid}" )
done done
for func in temperature humidity battery for func in temperature humidity battery
@ -119,8 +125,8 @@ fetch() {
for deviceid in ${deviceids} for deviceid in ${deviceids}
do do
val=${func}_${deviceid} ref=${func}_${deviceid}
echo "${deviceid}.value" "${!val}" echo "${deviceid}.value" "${!ref:-U}"
done done
done done
} }
@ -140,15 +146,12 @@ do_fetch() {
fi fi
if [ $(( time_now - time_modified )) -lt "${interval}" ]; then if [ $(( time_now - time_modified )) -lt "${interval}" ]; then
# read from cache # do nothing
cat "${MUNIN_STATEFILE}_${deviceid}" return
else
# update cache
fetch_api "${deviceid}" | tee "${MUNIN_STATEFILE}_${deviceid}"
fi fi
else
fetch_api "${deviceid}"
fi fi
fetch_api "${deviceid}" > "${MUNIN_STATEFILE}_${deviceid}"
} }
fetch_api() { fetch_api() {
@ -156,7 +159,7 @@ fetch_api() {
deviceid=$1 deviceid=$1
t=$( date +'%s%3N' ) t=$( date +'%s%3N' )
nonce=$( openssl rand -base64 32 ) nonce=$( openssl rand -base64 32 )
sign=$( echo -n "${token}${t}${nonce}" | openssl dgst -sha256 -hmac "${secret}" -binary | openssl base64 ) sign=$( echo -n "${token}${t}${nonce}" | openssl dgst -sha256 -hmac "${secret}" -binary | openssl base64 )
http_response=$( curl -s --fail --retry 3 \ http_response=$( curl -s --fail --retry 3 \