Browse Source

refactor: Refactor lockfile names for consistency and clarity

- Renamed "${lockdir}/${selfname}-laststart.lock" to "${lockdir}/${selfname}-last-started.lock"
- Renamed "${lockdir}/backup.lock" to "${lockdir}/stopping.lock"
- Renamed "${lockdir}/${selfname}-start.lock" to "${lockdir}/${selfname}-started.lock"
- Renamed "${lockdir}/${selfname}-starting.lock" to "${lockdir}/${selfname}-stopping.lock"

This commit refactors the lockfile names in the codebase for better consistency and clarity. The changes make it easier to understand the purpose of each lockfile and improve readability.
pull/4296/head
Daniel Gibbs 2 years ago
parent
commit
d5a5639ecb
  1. 8
      lgsm/modules/check_last_update.sh
  2. 22
      lgsm/modules/command_backup.sh
  3. 10
      lgsm/modules/command_debug.sh
  4. 20
      lgsm/modules/command_monitor.sh
  5. 19
      lgsm/modules/command_start.sh
  6. 14
      lgsm/modules/command_stop.sh
  7. 2
      lgsm/modules/command_update.sh

8
lgsm/modules/check_last_update.sh

@ -3,13 +3,13 @@
# Author: Daniel Gibbs # Author: Daniel Gibbs
# Contributors: http://linuxgsm.com/contrib # Contributors: http://linuxgsm.com/contrib
# Website: https://linuxgsm.com # Website: https://linuxgsm.com
# Description: Checks lock file to see when last update happened. # Description: Checks Lockfile to see when last update happened.
# Will reboot server if instance not rebooted since update. # Will reboot server if instance not rebooted since update.
moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")" moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
if [ -f "${lockdir}/${selfname}-laststart.lock" ]; then if [ -f "${lockdir}/${selfname}-last-started.lock" ]; then
laststart=$(cat "${lockdir}/${selfname}-laststart.lock") laststart=$(cat "${lockdir}/${selfname}-last-started.lock")
fi fi
if [ -f "${lockdir}/lastupdate.lock" ]; then if [ -f "${lockdir}/lastupdate.lock" ]; then
lastupdate=$(cat "${lockdir}/lastupdate.lock") lastupdate=$(cat "${lockdir}/lastupdate.lock")
@ -17,7 +17,7 @@ fi
check_status.sh check_status.sh
if [ -f "${lockdir}/lastupdate.lock" ] && [ "${status}" != "0" ]; then if [ -f "${lockdir}/lastupdate.lock" ] && [ "${status}" != "0" ]; then
if [ ! -f "${lockdir}/${selfname}-laststart.lock" ] || [ "${laststart}" -lt "${lastupdate}" ]; then if [ ! -f "${lockdir}/${selfname}-last-started.lock" ] || [ "${laststart}" -lt "${lastupdate}" ]; then
fn_print_info "${selfname} has not been restarted since last update" fn_print_info "${selfname} has not been restarted since last update"
fn_script_log_info "${selfname} has not been restarted since last update" fn_script_log_info "${selfname} has not been restarted since last update"
command_restart.sh command_restart.sh

22
lgsm/modules/command_backup.sh

@ -22,7 +22,7 @@ fn_backup_trap() {
echo -en "backup ${backupname}.tar.gz..." echo -en "backup ${backupname}.tar.gz..."
fn_print_removed_eol_nl fn_print_removed_eol_nl
fn_script_log_info "Backup ${backupname}.tar.gz: REMOVED" fn_script_log_info "Backup ${backupname}.tar.gz: REMOVED"
# Remove lock file. # Remove backup lockfile.
rm -f "${lockdir:?}/backup.lock" rm -f "${lockdir:?}/backup.lock"
fn_backup_start_server fn_backup_start_server
unset exitbypass unset exitbypass
@ -31,6 +31,18 @@ fn_backup_trap() {
# Check if a backup is pending or has been aborted using backup.lock. # Check if a backup is pending or has been aborted using backup.lock.
fn_backup_check_lockfile() { fn_backup_check_lockfile() {
# Remove stale lockfile.
if [ -f "${lockdir}/backup.lock" ]; then
if [ "$(find "${lockdir}/backup.lock" -mmin +60)" ]; then
fn_print_dots "Lockfile found: "
fn_print_checking_eol
fn_print_warn "Lockfile found: Removing stale lockfile: "
fn_print_warn_eol
fn_script_log_warn "Lockfile found: Removing stale lockfile"
rm -f "${lockdir:?}/backup.lock"
fi
fi
if [ -f "${lockdir}/backup.lock" ]; then if [ -f "${lockdir}/backup.lock" ]; then
fn_print_info_nl "Lockfile found: Backup is currently running" fn_print_info_nl "Lockfile found: Backup is currently running"
fn_script_log_error "Lockfile found: Backup is currently running: ${lockdir}/backup.lock" fn_script_log_error "Lockfile found: Backup is currently running: ${lockdir}/backup.lock"
@ -118,7 +130,7 @@ fn_backup_migrate_olddir() {
fn_backup_create_lockfile() { fn_backup_create_lockfile() {
# Create lockfile. # Create lockfile.
date '+%s' > "${lockdir}/backup.lock" date '+%s' > "${lockdir}/backup.lock"
fn_script_log_info "Lockfile generated" fn_script_log_info "Backup lockfile generated"
fn_script_log_info "${lockdir}/backup.lock" fn_script_log_info "${lockdir}/backup.lock"
# trap to remove lockfile on quit. # trap to remove lockfile on quit.
trap fn_backup_trap INT trap fn_backup_trap INT
@ -140,7 +152,7 @@ fn_backup_compression() {
core_exit.sh core_exit.sh
fi fi
tar -czf "${backupdir}/${backupname}.tar.gz" -C "${rootdir}" --exclude "${excludedir}" --exclude "${lockdir}/backup.lock" ./. tar -czf "${backupdir}/${backupname}.tar.gz" -C "${rootdir}" --exclude "${excludedir}" --exclude "${lockdir}" ./.
local exitcode=$? local exitcode=$?
if [ "${exitcode}" != 0 ]; then if [ "${exitcode}" != 0 ]; then
fn_print_fail_eol fn_print_fail_eol
@ -153,8 +165,6 @@ fn_backup_compression() {
fn_print_ok_nl "Completed: ${backupname}.tar.gz, total size $(du -sh "${backupdir}/${backupname}.tar.gz" | awk '{print $1}')" fn_print_ok_nl "Completed: ${backupname}.tar.gz, total size $(du -sh "${backupdir}/${backupname}.tar.gz" | awk '{print $1}')"
fn_script_log_pass "Backup created: ${backupname}.tar.gz, total size $(du -sh "${backupdir}/${backupname}.tar.gz" | awk '{print $1}')" fn_script_log_pass "Backup created: ${backupname}.tar.gz, total size $(du -sh "${backupdir}/${backupname}.tar.gz" | awk '{print $1}')"
fi fi
# Remove lock file
rm -f "${lockdir:?}/backup.lock"
} }
# Clear old backups according to maxbackups and maxbackupdays variables. # Clear old backups according to maxbackups and maxbackupdays variables.
@ -265,4 +275,6 @@ fn_backup_compression
fn_backup_prune fn_backup_prune
fn_backup_start_server fn_backup_start_server
# Remove backup lockfile.
rm -f "${lockdir:?}/backup.lock"
core_exit.sh core_exit.sh

10
lgsm/modules/command_debug.sh

@ -13,7 +13,7 @@ fn_firstcommand_set
# Trap to remove lockfile on quit. # Trap to remove lockfile on quit.
fn_lockfile_trap() { fn_lockfile_trap() {
# Remove lockfile. # Remove lockfile.
rm -f "${lockdir:?}/${selfname}-start.lock" rm -f "${lockdir:?}/${selfname}-started.lock"
# resets terminal. Servers can sometimes mess up the terminal on exit. # resets terminal. Servers can sometimes mess up the terminal on exit.
reset reset
fn_print_dots "Stopping debug" fn_print_dots "Stopping debug"
@ -99,11 +99,11 @@ fn_script_log_info "Starting debug"
fn_print_ok_nl "Starting debug" fn_print_ok_nl "Starting debug"
# Create lockfile. # Create lockfile.
date '+%s' > "${lockdir}/${selfname}-start.lock" date '+%s' > "${lockdir}/${selfname}-started.lock"
echo "${version}" >> "${lockdir}/${selfname}-start.lock" echo "${version}" >> "${lockdir}/${selfname}-started.lock"
echo "${port}" >> "${lockdir}/${selfname}-start.lock" echo "${port}" >> "${lockdir}/${selfname}-started.lock"
fn_script_log_info "Lockfile generated" fn_script_log_info "Lockfile generated"
fn_script_log_info "${lockdir}/${selfname}-start.lock" fn_script_log_info "${lockdir}/${selfname}-started.lock"
if [ "${shortname}" == "av" ]; then if [ "${shortname}" == "av" ]; then
cd "${systemdir}" || exit cd "${systemdir}" || exit

20
lgsm/modules/command_monitor.sh

@ -13,7 +13,7 @@ fn_firstcommand_set
fn_monitor_check_lockfile() { fn_monitor_check_lockfile() {
# Monitor does not run if lockfile is not found. # Monitor does not run if lockfile is not found.
if [ ! -f "${lockdir}/${selfname}-start.lock" ]; then if [ ! -f "${lockdir}/${selfname}-started.lock" ]; then
fn_print_dots "Checking lockfile: " fn_print_dots "Checking lockfile: "
fn_print_checking_eol fn_print_checking_eol
fn_script_log_info "Checking lockfile: CHECKING" fn_script_log_info "Checking lockfile: CHECKING"
@ -25,10 +25,10 @@ fn_monitor_check_lockfile() {
fi fi
# Fix if lockfile is not unix time or contains letters # Fix if lockfile is not unix time or contains letters
if [ -f "${lockdir}/${selfname}-start.lock" ] && [[ "$(head -n 1 "${lockdir}/${selfname}-start.lock")" =~ [A-Za-z] ]]; then if [ -f "${lockdir}/${selfname}-started.lock" ] && [[ "$(head -n 1 "${lockdir}/${selfname}-started.lock")" =~ [A-Za-z] ]]; then
date '+%s' > "${lockdir}/${selfname}-start.lock" date '+%s' > "${lockdir}/${selfname}-started.lock"
echo "${version}" >> "${lockdir}/${selfname}-start.lock" echo "${version}" >> "${lockdir}/${selfname}-started.lock"
echo "${port}" >> "${lockdir}/${selfname}-start.lock" echo "${port}" >> "${lockdir}/${selfname}-started.lock"
fi fi
} }
@ -93,12 +93,12 @@ fn_monitor_check_stopping(){
fi fi
fi fi
if [ -f "${lockdir}/stoping.lock" ] && [[ "$(pgrep -fc -u "${USER}" "${selfname} stop")" != "0" || "$(pgrep -fc -u "${USER}" "${selfname} s")" != "0" ]]; then if [ -f "${lockdir}/stopping.lock" ] && [[ "$(pgrep -fc -u "${USER}" "${selfname} stop")" != "0" || "$(pgrep -fc -u "${USER}" "${selfname} s")" != "0" ]]; then
fn_print_dots "Checking stop: " fn_print_dots "Checking stop: "
fn_print_checking_eol fn_print_checking_eol
fn_print_info "Checking stop: LinuxGSM is currently stoping: " fn_print_info "Checking stop: LinuxGSM is currently stopping: "
fn_print_info_eol fn_print_info_eol
fn_script_log_info "Checking backup: LinuxGSM is currently stoping" fn_script_log_info "Checking backup: LinuxGSM is currently stopping"
core_exit.sh core_exit.sh
fi fi
} }
@ -229,12 +229,12 @@ fn_monitor_query() {
fn_print_querying_eol fn_print_querying_eol
fn_script_log_info "Querying port: ${querymethod}: ${queryip}:${queryport} : ${queryattempt} : QUERYING" fn_script_log_info "Querying port: ${querymethod}: ${queryip}:${queryport} : ${queryattempt} : QUERYING"
# querydelay # querydelay
if [ "$(head -n 1 "${lockdir}/${selfname}-start.lock")" -gt "$(date "+%s" -d "${querydelay} mins ago")" ]; then if [ "$(head -n 1 "${lockdir}/${selfname}-started.lock")" -gt "$(date "+%s" -d "${querydelay} mins ago")" ]; then
fn_print_ok "Querying port: ${querymethod}: ${ip}:${queryport} : ${totalseconds}/${queryattempt}: " fn_print_ok "Querying port: ${querymethod}: ${ip}:${queryport} : ${totalseconds}/${queryattempt}: "
fn_print_delay_eol_nl fn_print_delay_eol_nl
fn_script_log_info "Querying port: ${querymethod}: ${ip}:${queryport} : ${queryattempt} : DELAY" fn_script_log_info "Querying port: ${querymethod}: ${ip}:${queryport} : ${queryattempt} : DELAY"
fn_script_log_info "Query bypassed: ${gameservername} started less than ${querydelay} minutes ago" fn_script_log_info "Query bypassed: ${gameservername} started less than ${querydelay} minutes ago"
fn_script_log_info "Server started: $(date -d @$(head -n 1 "${lockdir}/${selfname}-start.lock"))" fn_script_log_info "Server started: $(date -d @$(head -n 1 "${lockdir}/${selfname}-started.lock"))"
fn_script_log_info "Current time: $(date)" fn_script_log_info "Current time: $(date)"
monitorpass=1 monitorpass=1
core_exit.sh core_exit.sh

19
lgsm/modules/command_start.sh

@ -43,11 +43,14 @@ fn_start_tmux() {
mv "${consolelog}" "${consolelogdate}" mv "${consolelog}" "${consolelogdate}"
fi fi
# Create a starting lockfile that only exists while the start command is running.
date '+%s' > "${lockdir}/${selfname}-starting.lock" date '+%s' > "${lockdir}/${selfname}-starting.lock"
# Create start lockfile that exists only when the server is running. # Create start lockfile that exists only when the server is running.
date '+%s' > "${lockdir}/${selfname}-start.lock" date '+%s' > "${lockdir}/${selfname}-started.lock"
echo "${version}" >> "${lockdir}/${selfname}-start.lock" echo "${version}" >> "${lockdir}/${selfname}-started.lock"
echo "${port}" >> "${lockdir}/${selfname}-start.lock" echo "${port}" >> "${lockdir}/${selfname}-started.lock"
fn_reload_startparameters fn_reload_startparameters
# Create uid to ensure unique tmux socket name. # Create uid to ensure unique tmux socket name.
@ -71,8 +74,9 @@ fn_start_tmux() {
# Create logfile. # Create logfile.
touch "${consolelog}" touch "${consolelog}"
# Create last start lock file # Create last started Lockfile.
date +%s > "${lockdir}/${selfname}-laststart.lock" # TODO: should this be since last successful update?
date +%s > "${lockdir}/${selfname}-last-started.lock"
# tmux compiled from source will return "master", therefore ignore it. # tmux compiled from source will return "master", therefore ignore it.
if [ "${tmuxv}" == "master" ]; then if [ "${tmuxv}" == "master" ]; then
@ -162,6 +166,7 @@ fn_start_tmux() {
fi fi
fi fi
fi fi
# Remove starting lockfile when command ends.
rm -f "${lockdir:?}/${selfname}-starting.lock" rm -f "${lockdir:?}/${selfname}-starting.lock"
core_exit.sh core_exit.sh
else else
@ -175,12 +180,13 @@ fn_start_tmux() {
check.sh check.sh
# If the server already started dont start again. # If the server already started dont start again.
# $status comes from check_status.sh, which is run by check.sh for the command.
if [ "${status}" != "0" ]; then if [ "${status}" != "0" ]; then
fn_print_dots "${servername}" fn_print_dots "${servername}"
fn_print_info_nl "${servername} is already running" fn_print_info_nl "${servername} is already running"
fn_script_log_error "${servername} is already running" fn_script_log_error "${servername} is already running"
if [ -z "${exitbypass}" ]; then if [ -z "${exitbypass}" ]; then
# Remove starting lockfile when command ends.
rm -f "${lockdir:?}/${selfname}-starting.lock"
core_exit.sh core_exit.sh
fi fi
fi fi
@ -204,5 +210,6 @@ else
fn_start_tmux fn_start_tmux
fi fi
# Remove starting lockfile when command ends.
rm -f "${lockdir:?}/${selfname}-starting.lock" rm -f "${lockdir:?}/${selfname}-starting.lock"
core_exit.sh core_exit.sh

14
lgsm/modules/command_stop.sh

@ -269,14 +269,20 @@ fn_stop_pre_check() {
} }
check.sh check.sh
# Create a stopping lockfile that only exists while the stop command is running.
date '+%s' > "${lockdir}/${selfname}-stopping.lock"
fn_print_dots "${servername}" fn_print_dots "${servername}"
info_game.sh info_game.sh
fn_stop_pre_check fn_stop_pre_check
# Remove lockfile.
if [ -f "${lockdir}/${selfname}-start.lock" ]; then # Remove started lockfile.
rm -f "${lockdir:?}/${selfname}-start.lock" rm -f "${lockdir:?}/${selfname}-started.lock"
fi
# Remove stopping lockfile.
rm -f "${lockdir:?}/${selfname}-stopping.lock"
if [ -z "${exitbypass}" ]; then if [ -z "${exitbypass}" ]; then
core_exit.sh core_exit.sh

2
lgsm/modules/command_update.sh

@ -37,7 +37,7 @@ else
update_steamcmd.sh update_steamcmd.sh
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

Loading…
Cancel
Save