Optimize service restart/reloads

This commit is contained in:
Vivek Kumar Dutta 2023-05-27 22:57:38 +05:30
parent 85c139d7b7
commit 8f09187cba
No known key found for this signature in database
GPG key ID: 65C818099F37097D
2 changed files with 44 additions and 49 deletions

View file

@ -5,7 +5,7 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=obuspa
PKG_VERSION:=7.0.2.15
PKG_VERSION:=7.0.2.16
LOCAL_DEV:=0
ifneq ($(LOCAL_DEV),1)

View file

@ -55,38 +55,28 @@ db_set_sql()
value="$*"
if [ -n "${param}" ] && [ -n "${value}" ]; then
${PROG} -f ${SQL_DB_FILE} -c dbset "${param}" "${value}" >/dev/null 2>&1
fi
}
db_del_sql()
{
local param
param="${1}"
if [ -n "${param}" ]; then
${PROG} -f ${SQL_DB_FILE} -c dbdel "${param}" >/dev/null 2>&1
if grep -q "${param} " ${DB_DUMP}; then
value="${value//\//\\/}"
sed -i "s/${param} .*/${param} \"${value}\"/g" ${DB_DUMP}
else
echo "${param} \"${value}\"" >> ${DB_DUMP}
fi
fi
}
db_set()
{
# if sql db present, update sql db
# else update reset file
if [ -f "${SQL_DB_FILE}" ]; then
# if sql db dump file present, update it
if [ -f "${DB_DUMP}" ]; then
db_set_sql "$@"
else
db_set_reset_file "$@"
fi
}
update_db_dump()
dump_db()
{
if [ -f "${DB_DUMP}" ]; then
rm ${DB_DUMP}
${PROG} -f ${SQL_DB_FILE} -c show database |sort > ${DB_DUMP}
fi
${PROG} -f ${SQL_DB_FILE} -c show database |awk 'NR>1 {print $1 " \"" $3 "\""}' | sort > ${DB_DUMP}
}
# if db present then check if it matches with existing instances
@ -102,7 +92,7 @@ get_base_path()
count=0
if [ -f "${DB_DUMP}" ]; then
path=$(grep "${refpath}\d.Alias => ${value}" ${DB_DUMP})
path=$(grep "${refpath}\d.Alias \"${value}\"" ${DB_DUMP})
path=${path%.*}
if [ -z "${path}" ]; then
path=$(grep -o "${refpath}\d" ${DB_DUMP} |sort -r|head -n 1)
@ -314,7 +304,6 @@ configure_localagent()
}
db_set Device.LocalAgent.EndpointID "${EndpointID}"
update_db_dump
}
update_reset_reason()
@ -417,9 +406,6 @@ configure_controller()
db_set "${BASEPATH}.BootParameter.${_pnum}.ParameterName" "${param}"
_pnum=$(( _pnum + 1 ))
done
db_set
update_db_dump
}
configure_subscription()
@ -597,8 +583,6 @@ configure_mtp() {
db_set "${BASEPATH}.WebSocket.Port" "${Port}"
db_set "${BASEPATH}.WebSocket.EnableEncryption" "${EnableEncryption}"
fi
db_set
update_db_dump
}
configure_stomp_connection() {
@ -629,8 +613,6 @@ configure_stomp_connection() {
db_set "${BASEPATH}.EnableEncryption" "${EnableEncryption}"
db_set "${BASEPATH}.EnableHeartbeats" "${EnableHeartbeats}"
db_set "${BASEPATH}.VirtualHost" "${VirtualHost}"
db_set
update_db_dump
}
configure_mqtt_client() {
@ -660,9 +642,6 @@ configure_mqtt_client() {
db_set "${BASEPATH}.ProtocolVersion" "${ProtocolVersion}"
db_set "${BASEPATH}.TransportProtocol" "${TransportProtocol}"
db_set "${BASEPATH}.ClientID" "${ClientID}"
db_set
update_db_dump
}
@ -716,6 +695,9 @@ configure_obuspa() {
fi
if [ -f "${RESET_FILE}" ]; then
if [ -f "${SQL_DB_FILE}" ]; then
mv ${SQL_DB_FILE} ${SQL_DB_FILE}.old
fi
procd_append_param command -r ${RESET_FILE}
fi
@ -760,9 +742,9 @@ get_param_value_from_dump()
return 0
fi
value="$(grep "^${param} " ${DB_DUMP}|awk '{print $3}')"
value="$(grep "^${param} " ${DB_DUMP}|awk '{print $2}')"
echo "$value"
echo "${value//\"/}"
}
update_uci_sec()
@ -786,7 +768,7 @@ sync_db_controller()
{
local cntrs copts sec pvalue protocol
copts="Enable EndpointID AssignedRole"
copts="Enable EndpointID AssignedRole PeriodicNotifInterval"
popts="Destination Topic Reference Host Port Path EnableEncryption"
cntrs="$(get_instances_from_db_dump Device.LocalAgent.Controller.)"
@ -819,7 +801,7 @@ sync_db_localagent_mtp()
local mtps opts popts sec pvalue protocol
opts="Enable"
popts="ResponseTopicConfigured Destination Port Path Reference EnableEncryption"
popts="ResponseTopicConfigured Destination Port Path Reference EnableEncryption PublishQoS"
ropts="mqtt stomp"
mtps="$(get_instances_from_db_dump Device.LocalAgent.MTP.)"
@ -935,10 +917,8 @@ delete_sql_db_entry_with_pattern()
return 0;
fi
params="$(grep "${pattern}" ${DB_DUMP}|awk '{print $1}')"
for p in ${params}; do
db_del_sql "${p}"
done
#log "Deleting with pattern [${pattern}] from ${DB_DUMP}"
sed -i "/${pattern}/d" ${DB_DUMP}
}
check_n_delete_db()
@ -954,7 +934,7 @@ check_n_delete_db()
r="${3}"
sec="${sec/${t}_/cpe-}"
path=$(grep "${r}\d.Alias => ${sec}" ${DB_DUMP})
path=$(grep "${r}\d.Alias \"${sec}\"" ${DB_DUMP})
path=${path%.*}
delete_sql_db_entry_with_pattern "${path}"
@ -982,28 +962,42 @@ db_init()
reason="${1}"
mkdir -p /tmp/obuspa/
# Load configuration
config_load $CONFIGURATION
config_get SQL_DB_FILE global db_file "/tmp/obuspa/usp.db"
if [ -f "${SQL_DB_FILE}.old" ] && [ ! -f "${SQL_DB_FILE}" ]; then
log "Copying old db, since new db not present ..."
mv ${SQL_DB_FILE}.old ${SQL_DB_FILE}
fi
# Dump datamodel parameters from DB
if [ -f "${SQL_DB_FILE}" ]; then
${PROG} -f ${SQL_DB_FILE} -c show database |sort > ${DB_DUMP}
dump_db
fi
# Only sync uci with db in case of non service triggers
# In case of Reboot or service restart update the uci
# from usp.db file
if [ -f "${DB_DUMP}" ] && [ "${reason}" != "update" ]; then
sync_uci_with_db
# Only do this if db have reasonable data
val="$(awk 'END{print NR}' ${DB_DUMP})"
if [ "$val" -gt 15 ]; then
log "Syncing obuspa uci with usp.db ...."
sync_uci_with_db
fi
fi
# remove entries from db if deleted from uci
# remove entries from db if deleted from uci, only in case of reload
if [ -f "${DB_DUMP}" ] && [ "${reason}" = "update" ] && [ -f "/tmp/obuspa/obuspa" ]; then
log "Deleting entries from usp.db if uci not present ...."
reverse_update_db_with_uci
fi
# Remove reset file if present
[ -f "${RESET_FILE}" ] && rm -f ${RESET_FILE}
[ -f "${RESET_FILE}" ] && mv ${RESET_FILE} ${RESET_FILE}.old
#log "Create reset file ...."
config_load $CONFIGURATION
global_init
config_foreach configure_localagent localagent
@ -1024,8 +1018,9 @@ db_init()
uci_commit ${CONFIGURATION}
cp /etc/config/obuspa /tmp/obuspa/
[ -f "${DB_DUMP}" ] && rm -f ${DB_DUMP}
if [ -f "${DB_DUMP}" ]; then
mv ${DB_DUMP} ${RESET_FILE}
fi
return 0;
}