Browse Source

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.
pull/4296/head
Daniel Gibbs 2 years ago
parent
commit
46d73f03a6
  1. 72
      lgsm/modules/command_monitor.sh
  2. 11
      lgsm/modules/command_start.sh
  3. 2
      lgsm/modules/command_update.sh
  4. 2
      lgsm/modules/command_validate.sh
  5. 1
      lgsm/modules/update_jediknight2.sh

72
lgsm/modules/command_monitor.sh

@ -35,12 +35,12 @@ fn_monitor_check_lockfile() {
fn_monitor_check_install() { fn_monitor_check_install() {
# Monitor will check if update is running. # 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 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_print_checking_eol
fn_script_log_info "Checking for installer: CHECKING" fn_script_log_info "Checking installer: CHECKING"
fn_print_info "Checking for installer: LinuxGSM is currently installing: " fn_print_info "Checking installer: LinuxGSM is currently installing: "
fn_print_info_eol 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 core_exit.sh
fi fi
} }
@ -48,6 +48,8 @@ fn_monitor_check_install() {
fn_monitor_check_debug() { fn_monitor_check_debug() {
# Monitor will check if debug is running. # Monitor will check if debug is running.
if [ "$(pgrep -fc -u "${USER}" "${selfname} debug")" != "0" ] || [ "$(pgrep -fc -u "${USER}" "${selfname} d")" != "0" ]; then 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 "Checking debug: LinuxGSM is currently in debug mode: "
fn_print_info_eol fn_print_info_eol
fn_script_log_pass "Checking debug: LinuxGSM is currently in debug mode" fn_script_log_pass "Checking debug: LinuxGSM is currently in debug mode"
@ -55,21 +57,69 @@ fn_monitor_check_debug() {
fi 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() { fn_monitor_check_backup() {
# Remove stale lockfile. # Remove stale lockfile.
if [ -f "${lockdir}/backup.lock" ]; then if [ -f "${lockdir}/backup.lock" ]; then
if [ "$(find "${lockdir}/backup.lock" -mmin +60)" ]; then if [ "$(find "${lockdir}/backup.lock" -mmin +60)" ]; then
fn_print_dots "Checking backup: " fn_print_dots "Checking backup: "
fn_print_checking_eol
fn_print_warn "Checking backup: Removing stale lockfile: " fn_print_warn "Checking backup: Removing stale lockfile: "
fn_print_warn_eol fn_print_warn_eol
fn_script_log_warn "Checking backup: Removing stale lockfile" fn_script_log_warn "Checking backup: Removing stale lockfile"
rm -f "${lockdir}/backup.lock" rm -f "${lockdir:?}/backup.lock"
fi fi
fi fi
# Monitor will check if backup is running. # 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 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_dots "Checking backup: "
fn_print_checking_eol
fn_print_info "Checking backup: LinuxGSM is currently running a backup: " fn_print_info "Checking backup: LinuxGSM is currently running a backup: "
fn_print_info_eol fn_print_info_eol
fn_script_log_info "Checking backup: LinuxGSM is currently running a backup" fn_script_log_info "Checking backup: LinuxGSM is currently running a backup"
@ -82,14 +132,18 @@ fn_monitor_check_update() {
# Remove stale lockfile. # Remove stale lockfile.
if [ -f "${lockdir}/${selfname}-update.lock" ]; then if [ -f "${lockdir}/${selfname}-update.lock" ]; then
if [ "$(find "${lockdir}/${selfname}-update.lock" -mmin +15)" ]; 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 "Checking update: Removing stale lockfile: "
fn_print_warn_eol fn_print_warn_eol
fn_script_log_warn "Checking update: Removing stale lockfile" fn_script_log_warn "Checking update: Removing stale lockfile"
rm -f "${lockdir}/${selfname}-update.lock" rm -f "${lockdir:?}/${selfname}-update.lock"
fi fi
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 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_nl "Checking update: LinuxGSM is currently updating: "
fn_print_info_eol fn_print_info_eol
fn_script_log_pass "Checking update: LinuxGSM is currently updating" 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_script_log_info "Checking update: CHECKING"
fn_print_ok "Checking update: " fn_print_ok "Checking update: "
fn_print_ok_eol_nl 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="restart"
alert.sh alert.sh
fn_script_log_info "Checking update: Monitor is restarting ${selfname} to apply update"
command_restart.sh command_restart.sh
core_exit.sh core_exit.sh
fi fi
@ -305,7 +358,8 @@ fn_monitor_check_update
fn_monitor_check_backup fn_monitor_check_backup
fn_monitor_check_debug fn_monitor_check_debug
fn_monitor_check_lockfile fn_monitor_check_lockfile
fn_monitor_check_starting
fn_monitor_check_stopping
fn_monitor_check_session fn_monitor_check_session
# Monitor will not continue if session only check. # Monitor will not continue if session only check.

11
lgsm/modules/command_start.sh

@ -43,6 +43,11 @@ fn_start_tmux() {
mv "${consolelog}" "${consolelogdate}" mv "${consolelog}" "${consolelogdate}"
fi 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 fn_reload_startparameters
# Create uid to ensure unique tmux socket name. # Create uid to ensure unique tmux socket name.
@ -156,14 +161,11 @@ fn_start_tmux() {
fi fi
fi fi
fi fi
rm -f "${lockdir:?}/${selfname}-starting.lock"
core_exit.sh core_exit.sh
else else
fn_print_ok "${servername}" fn_print_ok "${servername}"
fn_script_log_pass "Started ${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 fi
rm -f "${lgsmlogdir:?}/.${selfname}-tmux-error.tmp" 2> /dev/null rm -f "${lgsmlogdir:?}/.${selfname}-tmux-error.tmp" 2> /dev/null
echo -en "\n" echo -en "\n"
@ -201,4 +203,5 @@ else
fn_start_tmux fn_start_tmux
fi fi
rm -f "${lockdir:?}/${selfname}-starting.lock"
core_exit.sh core_exit.sh

2
lgsm/modules/command_update.sh

@ -38,6 +38,6 @@ else
fi fi
# remove update lockfile # remove update lockfile
rm -f "${lockdir}/${selfname}-update.lock" rm -f "${lockdir:?}/${selfname}-update.lock"
core_exit.sh core_exit.sh

2
lgsm/modules/command_validate.sh

@ -47,6 +47,6 @@ else
fi fi
# remove update lockfile # remove update lockfile
rm -f "${lockdir}/${selfname}-update.lock" rm -f "${lockdir:?}/${selfname}-update.lock"
core_exit.sh core_exit.sh

1
lgsm/modules/update_jediknight2.sh

@ -65,7 +65,6 @@ fn_update_compare() {
if [ "${localbuild}" != "${remotebuildversion}" ] || [ "${forceupdate}" == "1" ]; then if [ "${localbuild}" != "${remotebuildversion}" ] || [ "${forceupdate}" == "1" ]; then
# Create update lockfile. # Create update lockfile.
date '+%s' > "${lockdir}/${selfname}-update.lock" date '+%s' > "${lockdir}/${selfname}-update.lock"
rm -f "${lockdir}/${selfname}-update.lock"
fn_print_ok_nl "Checking for update: ${remotelocation}" fn_print_ok_nl "Checking for update: ${remotelocation}"
echo -en "\n" echo -en "\n"
echo -e "Update available" echo -e "Update available"

Loading…
Cancel
Save