From 46d73f03a60700a0a82a0f255a67ba9f370d0d82 Mon Sep 17 00:00:00 2001 From: Daniel Gibbs Date: Sun, 30 Jul 2023 14:47:13 +0100 Subject: [PATCH] refactor: improve code readability and remove redundant code - Refactored the `fn_monitor_check_starting` function to check for stale lockfiles and print appropriate messages. - Refactored the `fn_monitor_check_stopping` function to check for stale lockfiles and print appropriate messages. - Removed redundant code that deletes the update lockfile in `command_update.sh` and `command_validate.sh`. - Improved code readability by removing unnecessary comments. --- lgsm/modules/command_monitor.sh | 72 ++++++++++++++++++++++++++---- lgsm/modules/command_start.sh | 11 +++-- lgsm/modules/command_update.sh | 2 +- lgsm/modules/command_validate.sh | 2 +- lgsm/modules/update_jediknight2.sh | 1 - 5 files changed, 72 insertions(+), 16 deletions(-) diff --git a/lgsm/modules/command_monitor.sh b/lgsm/modules/command_monitor.sh index ac7c0a5c5..6e688b3fb 100644 --- a/lgsm/modules/command_monitor.sh +++ b/lgsm/modules/command_monitor.sh @@ -35,12 +35,12 @@ fn_monitor_check_lockfile() { fn_monitor_check_install() { # Monitor will check if update is running. if [ "$(pgrep -fc -u "${USER}" "${selfname} install")" != "0" ] || [ "$(pgrep -fc -u "${USER}" "${selfname} i")" != "0" ] || [ "$(pgrep -fc -u "${USER}" "${selfname} auto-install")" != "0" ] || [ "$(pgrep -fc -u "${USER}" "${selfname} ai")" != "0" ]; then - fn_print_dots "Checking for installer: " + fn_print_dots "Checking installer: " fn_print_checking_eol - fn_script_log_info "Checking for installer: CHECKING" - fn_print_info "Checking for installer: LinuxGSM is currently installing: " + fn_script_log_info "Checking installer: CHECKING" + fn_print_info "Checking installer: LinuxGSM is currently installing: " fn_print_info_eol - fn_script_log_pass "Checking for installer: LinuxGSM is currently installing" + fn_script_log_pass "Checking installer: LinuxGSM is currently installing" core_exit.sh fi } @@ -48,6 +48,8 @@ fn_monitor_check_install() { fn_monitor_check_debug() { # Monitor will check if debug is running. if [ "$(pgrep -fc -u "${USER}" "${selfname} debug")" != "0" ] || [ "$(pgrep -fc -u "${USER}" "${selfname} d")" != "0" ]; then + fn_print_dots "Checking debug: " + fn_print_checking_eol fn_print_info "Checking debug: LinuxGSM is currently in debug mode: " fn_print_info_eol fn_script_log_pass "Checking debug: LinuxGSM is currently in debug mode" @@ -55,21 +57,69 @@ fn_monitor_check_debug() { fi } +fn_monitor_check_starting(){ + # Remove stale lockfile. + if [ -f "${lockdir}/starting.lock" ]; then + if [ "$(find "${lockdir}/starting.lock" -mmin +5)" ]; then + fn_print_dots "Checking start: " + fn_print_checking_eol + fn_print_warn "Checking start: Removing stale lockfile: " + fn_print_warn_eol + fn_script_log_warn "Checking start: Removing stale lockfile" + rm -f "${lockdir:?}/starting.lock" + fi + fi + + if [ -f "${lockdir}/starting.lock" ] && [[ "$(pgrep -fc -u "${USER}" "${selfname} start")" != "0" || "$(pgrep -fc -u "${USER}" "${selfname} s")" != "0" ]]; then + fn_print_dots "Checking start: " + fn_print_checking_eol + fn_print_info "Checking start: LinuxGSM is currently starting: " + fn_print_info_eol + fn_script_log_info "Checking backup: LinuxGSM is currently starting" + core_exit.sh + fi +} + +fn_monitor_check_stopping(){ + # Remove stale lockfile. + if [ -f "${lockdir}/stopping.lock" ]; then + if [ "$(find "${lockdir}/stopping.lock" -mmin +5)" ]; then + fn_print_dots "Checking stop: " + fn_print_checking_eol + fn_print_warn "Checking stop: Removing stale lockfile: " + fn_print_warn_eol + fn_script_log_warn "Checking stop: Removing stale lockfile" + rm -f "${lockdir:?}/stopping.lock" + fi + fi + + if [ -f "${lockdir}/stoping.lock" ] && [[ "$(pgrep -fc -u "${USER}" "${selfname} stop")" != "0" || "$(pgrep -fc -u "${USER}" "${selfname} s")" != "0" ]]; then + fn_print_dots "Checking stop: " + fn_print_checking_eol + fn_print_info "Checking stop: LinuxGSM is currently stoping: " + fn_print_info_eol + fn_script_log_info "Checking backup: LinuxGSM is currently stoping" + core_exit.sh + fi +} + fn_monitor_check_backup() { # Remove stale lockfile. if [ -f "${lockdir}/backup.lock" ]; then if [ "$(find "${lockdir}/backup.lock" -mmin +60)" ]; then fn_print_dots "Checking backup: " + fn_print_checking_eol fn_print_warn "Checking backup: Removing stale lockfile: " fn_print_warn_eol fn_script_log_warn "Checking backup: Removing stale lockfile" - rm -f "${lockdir}/backup.lock" + rm -f "${lockdir:?}/backup.lock" fi fi # Monitor will check if backup is running. if [ -f "${lockdir}/backup.lock" ] && [[ "$(pgrep -fc -u "${USER}" "${selfname} backup")" != "0" || "$(pgrep -fc -u "${USER}" "${selfname} b")" != "0" ]]; then fn_print_dots "Checking backup: " + fn_print_checking_eol fn_print_info "Checking backup: LinuxGSM is currently running a backup: " fn_print_info_eol fn_script_log_info "Checking backup: LinuxGSM is currently running a backup" @@ -82,14 +132,18 @@ fn_monitor_check_update() { # Remove stale lockfile. if [ -f "${lockdir}/${selfname}-update.lock" ]; then if [ "$(find "${lockdir}/${selfname}-update.lock" -mmin +15)" ]; then + fn_print_dots "Checking update: " + fn_print_checking_eol fn_print_warn "Checking update: Removing stale lockfile: " fn_print_warn_eol fn_script_log_warn "Checking update: Removing stale lockfile" - rm -f "${lockdir}/${selfname}-update.lock" + rm -f "${lockdir:?}/${selfname}-update.lock" fi fi if [ -f "${lockdir}/${selfname}-update.lock" ] && [[ "$(pgrep -fc -u "${USER}" "${selfname} update")" != "0" || "$(pgrep -fc -u "${USER}" "${selfname} validate")" != "0" || "$(pgrep -fc -u "${USER}" "${selfname} v")" != "0" || "$(pgrep -fc force-update "${USER}" "${selfname} fu")" != "0" ]]; then + fn_print_dots "Checking update: " + fn_print_checking_eol fn_print_info_nl "Checking update: LinuxGSM is currently updating: " fn_print_info_eol fn_script_log_pass "Checking update: LinuxGSM is currently updating" @@ -105,10 +159,9 @@ fn_monitor_check_update_source(){ fn_script_log_info "Checking update: CHECKING" fn_print_ok "Checking update: " fn_print_ok_eol_nl - fn_script_log_pass "Checking update: Monitor is restarting ${selfname} to apply update" + fn_script_log_info "Checking update: Monitor is restarting ${selfname} to apply update" alert="restart" alert.sh - fn_script_log_info "Checking update: Monitor is restarting ${selfname} to apply update" command_restart.sh core_exit.sh fi @@ -305,7 +358,8 @@ fn_monitor_check_update fn_monitor_check_backup fn_monitor_check_debug fn_monitor_check_lockfile - +fn_monitor_check_starting +fn_monitor_check_stopping fn_monitor_check_session # Monitor will not continue if session only check. diff --git a/lgsm/modules/command_start.sh b/lgsm/modules/command_start.sh index 4badf8ea8..f674c896c 100644 --- a/lgsm/modules/command_start.sh +++ b/lgsm/modules/command_start.sh @@ -43,6 +43,11 @@ fn_start_tmux() { mv "${consolelog}" "${consolelogdate}" fi + date '+%s' > "${lockdir}/${selfname}-starting.lock" + # Create start lockfile that exists only when the server is running. + date '+%s' > "${lockdir}/${selfname}-start.lock" + echo "${version}" >> "${lockdir}/${selfname}-start.lock" + echo "${port}" >> "${lockdir}/${selfname}-start.lock" fn_reload_startparameters # Create uid to ensure unique tmux socket name. @@ -156,14 +161,11 @@ fn_start_tmux() { fi fi fi + rm -f "${lockdir:?}/${selfname}-starting.lock" core_exit.sh else fn_print_ok "${servername}" fn_script_log_pass "Started ${servername}" - # Create start lockfile that exists only when the server is running. - date '+%s' > "${lockdir}/${selfname}-start.lock" - echo "${version}" >> "${lockdir}/${selfname}-start.lock" - echo "${port}" >> "${lockdir}/${selfname}-start.lock" fi rm -f "${lgsmlogdir:?}/.${selfname}-tmux-error.tmp" 2> /dev/null echo -en "\n" @@ -201,4 +203,5 @@ else fn_start_tmux fi +rm -f "${lockdir:?}/${selfname}-starting.lock" core_exit.sh diff --git a/lgsm/modules/command_update.sh b/lgsm/modules/command_update.sh index 1285b8dce..16e4fc8fe 100644 --- a/lgsm/modules/command_update.sh +++ b/lgsm/modules/command_update.sh @@ -38,6 +38,6 @@ else fi # remove update lockfile -rm -f "${lockdir}/${selfname}-update.lock" +rm -f "${lockdir:?}/${selfname}-update.lock" core_exit.sh diff --git a/lgsm/modules/command_validate.sh b/lgsm/modules/command_validate.sh index 9705e4d14..0de9ded9a 100644 --- a/lgsm/modules/command_validate.sh +++ b/lgsm/modules/command_validate.sh @@ -47,6 +47,6 @@ else fi # remove update lockfile -rm -f "${lockdir}/${selfname}-update.lock" +rm -f "${lockdir:?}/${selfname}-update.lock" core_exit.sh diff --git a/lgsm/modules/update_jediknight2.sh b/lgsm/modules/update_jediknight2.sh index 7fa4b4bd7..ac87b0529 100644 --- a/lgsm/modules/update_jediknight2.sh +++ b/lgsm/modules/update_jediknight2.sh @@ -65,7 +65,6 @@ fn_update_compare() { if [ "${localbuild}" != "${remotebuildversion}" ] || [ "${forceupdate}" == "1" ]; then # Create update lockfile. date '+%s' > "${lockdir}/${selfname}-update.lock" - rm -f "${lockdir}/${selfname}-update.lock" fn_print_ok_nl "Checking for update: ${remotelocation}" echo -en "\n" echo -e "Update available"