Skip to content

Commit

Permalink
refactor: Refactor lockfile names for consistency and clarity
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
dgibbs64 committed Jul 30, 2023
1 parent 3d6c92b commit d5a5639
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 37 deletions.
8 changes: 4 additions & 4 deletions lgsm/modules/check_last_update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
# Author: Daniel Gibbs
# Contributors: http://linuxgsm.com/contrib
# 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.

moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"

if [ -f "${lockdir}/${selfname}-laststart.lock" ]; then
laststart=$(cat "${lockdir}/${selfname}-laststart.lock")
if [ -f "${lockdir}/${selfname}-last-started.lock" ]; then
laststart=$(cat "${lockdir}/${selfname}-last-started.lock")
fi
if [ -f "${lockdir}/lastupdate.lock" ]; then
lastupdate=$(cat "${lockdir}/lastupdate.lock")
fi

check_status.sh
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_script_log_info "${selfname} has not been restarted since last update"
command_restart.sh
Expand Down
26 changes: 19 additions & 7 deletions lgsm/modules/command_backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn_backup_trap() {
echo -en "backup ${backupname}.tar.gz..."
fn_print_removed_eol_nl
fn_script_log_info "Backup ${backupname}.tar.gz: REMOVED"
# Remove lock file.
# Remove backup lockfile.
rm -f "${lockdir:?}/backup.lock"
fn_backup_start_server
unset exitbypass
Expand All @@ -31,9 +31,21 @@ fn_backup_trap() {

# Check if a backup is pending or has been aborted using backup.lock.
fn_backup_check_lockfile() {
# Remove stale lockfile.
if [ -f "${lockdir}/backup.lock" ]; then
fn_print_info_nl "Lock file found: Backup is currently running"
fn_script_log_error "Lock file found: Backup is currently running: ${lockdir}/backup.lock"
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
fn_print_info_nl "Lockfile found: Backup is currently running"
fn_script_log_error "Lockfile found: Backup is currently running: ${lockdir}/backup.lock"
core_exit.sh
fi
}
Expand Down Expand Up @@ -118,7 +130,7 @@ fn_backup_migrate_olddir() {
fn_backup_create_lockfile() {
# Create lockfile.
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"
# trap to remove lockfile on quit.
trap fn_backup_trap INT
Expand All @@ -140,7 +152,7 @@ fn_backup_compression() {
core_exit.sh
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=$?
if [ "${exitcode}" != 0 ]; then
fn_print_fail_eol
Expand All @@ -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_script_log_pass "Backup created: ${backupname}.tar.gz, total size $(du -sh "${backupdir}/${backupname}.tar.gz" | awk '{print $1}')"
fi
# Remove lock file
rm -f "${lockdir:?}/backup.lock"
}

# Clear old backups according to maxbackups and maxbackupdays variables.
Expand Down Expand Up @@ -265,4 +275,6 @@ fn_backup_compression
fn_backup_prune
fn_backup_start_server

# Remove backup lockfile.
rm -f "${lockdir:?}/backup.lock"
core_exit.sh
10 changes: 5 additions & 5 deletions lgsm/modules/command_debug.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn_firstcommand_set
# Trap to remove lockfile on quit.
fn_lockfile_trap() {
# 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.
reset
fn_print_dots "Stopping debug"
Expand Down Expand Up @@ -99,11 +99,11 @@ fn_script_log_info "Starting debug"
fn_print_ok_nl "Starting debug"

# Create lockfile.
date '+%s' > "${lockdir}/${selfname}-start.lock"
echo "${version}" >> "${lockdir}/${selfname}-start.lock"
echo "${port}" >> "${lockdir}/${selfname}-start.lock"
date '+%s' > "${lockdir}/${selfname}-started.lock"
echo "${version}" >> "${lockdir}/${selfname}-started.lock"
echo "${port}" >> "${lockdir}/${selfname}-started.lock"
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
cd "${systemdir}" || exit
Expand Down
20 changes: 10 additions & 10 deletions lgsm/modules/command_monitor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn_firstcommand_set

fn_monitor_check_lockfile() {
# 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_checking_eol
fn_script_log_info "Checking lockfile: CHECKING"
Expand All @@ -25,10 +25,10 @@ fn_monitor_check_lockfile() {
fi

# 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
date '+%s' > "${lockdir}/${selfname}-start.lock"
echo "${version}" >> "${lockdir}/${selfname}-start.lock"
echo "${port}" >> "${lockdir}/${selfname}-start.lock"
if [ -f "${lockdir}/${selfname}-started.lock" ] && [[ "$(head -n 1 "${lockdir}/${selfname}-started.lock")" =~ [A-Za-z] ]]; then
date '+%s' > "${lockdir}/${selfname}-started.lock"
echo "${version}" >> "${lockdir}/${selfname}-started.lock"
echo "${port}" >> "${lockdir}/${selfname}-started.lock"
fi
}

Expand Down Expand Up @@ -93,12 +93,12 @@ fn_monitor_check_stopping(){
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_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_script_log_info "Checking backup: LinuxGSM is currently stoping"
fn_script_log_info "Checking backup: LinuxGSM is currently stopping"
core_exit.sh
fi
}
Expand Down Expand Up @@ -229,12 +229,12 @@ fn_monitor_query() {
fn_print_querying_eol
fn_script_log_info "Querying port: ${querymethod}: ${queryip}:${queryport} : ${queryattempt} : QUERYING"
# 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_delay_eol_nl
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 "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)"
monitorpass=1
core_exit.sh
Expand Down
19 changes: 13 additions & 6 deletions lgsm/modules/command_start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,14 @@ fn_start_tmux() {
mv "${consolelog}" "${consolelogdate}"
fi

# Create a starting lockfile that only exists while the start command is running.
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"
date '+%s' > "${lockdir}/${selfname}-started.lock"
echo "${version}" >> "${lockdir}/${selfname}-started.lock"
echo "${port}" >> "${lockdir}/${selfname}-started.lock"

fn_reload_startparameters

# Create uid to ensure unique tmux socket name.
Expand All @@ -71,8 +74,9 @@ fn_start_tmux() {
# Create logfile.
touch "${consolelog}"

# Create last start lock file
date +%s > "${lockdir}/${selfname}-laststart.lock"
# Create last started Lockfile.
# 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.
if [ "${tmuxv}" == "master" ]; then
Expand Down Expand Up @@ -162,6 +166,7 @@ fn_start_tmux() {
fi
fi
fi
# Remove starting lockfile when command ends.
rm -f "${lockdir:?}/${selfname}-starting.lock"
core_exit.sh
else
Expand All @@ -175,12 +180,13 @@ fn_start_tmux() {
check.sh

# 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
fn_print_dots "${servername}"
fn_print_info_nl "${servername} is already running"
fn_script_log_error "${servername} is already running"
if [ -z "${exitbypass}" ]; then
# Remove starting lockfile when command ends.
rm -f "${lockdir:?}/${selfname}-starting.lock"
core_exit.sh
fi
fi
Expand All @@ -204,5 +210,6 @@ else
fn_start_tmux
fi

# Remove starting lockfile when command ends.
rm -f "${lockdir:?}/${selfname}-starting.lock"
core_exit.sh
14 changes: 10 additions & 4 deletions lgsm/modules/command_stop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,20 @@ fn_stop_pre_check() {
}

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}"

info_game.sh
fn_stop_pre_check
# Remove lockfile.
if [ -f "${lockdir}/${selfname}-start.lock" ]; then
rm -f "${lockdir:?}/${selfname}-start.lock"
fi

# Remove started lockfile.
rm -f "${lockdir:?}/${selfname}-started.lock"

# Remove stopping lockfile.
rm -f "${lockdir:?}/${selfname}-stopping.lock"

if [ -z "${exitbypass}" ]; then
core_exit.sh
Expand Down
2 changes: 1 addition & 1 deletion lgsm/modules/command_update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ else
update_steamcmd.sh
fi

# remove update lockfile
# remove update lockfile.
rm -f "${lockdir:?}/${selfname}-update.lock"

core_exit.sh

0 comments on commit d5a5639

Please sign in to comment.