tmux-continuum/scripts/continuum_save.sh
v9v 800488ca6f
Replace flock with mkdir
flock is not supported on MacOS.

`mkdir` locks have a drawback: they are not cleaned up automatically. If the lock owner crashed before cleaning up the lock, the directory will stay in the filesystem and the lock will be never acquired by someone else. To avoid that, we create temporary locks (the lockdir name changes every 100 seconds). We grab two lock (N and N+1) to avoid the case where process A grabs lock N and process B grabs lock N+1 and both enter the critical section.
2019-07-08 15:58:42 +02:00

55 lines
1.7 KiB
Bash
Executable File

#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/helpers.sh"
source "$CURRENT_DIR/variables.sh"
source "$CURRENT_DIR/shared.sh"
supported_tmux_version_ok() {
"$CURRENT_DIR/check_tmux_version.sh" "$SUPPORTED_VERSION"
}
get_interval() {
get_tmux_option "$auto_save_interval_option" "$auto_save_interval_default"
}
auto_save_not_disabled() {
[ "$(get_interval)" -gt 0 ]
}
enough_time_since_last_run_passed() {
local last_saved_timestamp="$(get_tmux_option "$last_auto_save_option" "0")"
local interval_minutes="$(get_interval)"
local interval_seconds="$((interval_minutes * 60))"
local next_run="$((last_saved_timestamp + $interval_seconds))"
[ "$(current_timestamp)" -ge "$next_run" ]
}
fetch_and_run_tmux_resurrect_save_script() {
local resurrect_save_script_path="$(get_tmux_option "$resurrect_save_path_option" "")"
if [ -n "$resurrect_save_script_path" ]; then
"$resurrect_save_script_path" "quiet" >/dev/null 2>&1 &
set_last_save_timestamp
fi
}
main() {
# Sometimes tmux starts multiple saves in parallel. We want only one
# save to be running, otherwise we can get corrupted saved state.
# The following implements a lock that auto-expires after 100...200s.
local lockdir_prefix="/tmp/tmux-continuum-$(current_tmux_server_pid)-lock-"
local lockdir1="${lockdir_prefix}$[ `date +%s` / 100 ]"
local lockdir2="${lockdir_prefix}$[ `date +%s` / 100 + 1]"
if mkdir "$lockdir1"; then
trap "rmdir "$lockdir1"" EXIT
if mkdir "$lockdir2"; then
trap "rmdir "$lockdir1" "$lockdir2"" EXIT
if supported_tmux_version_ok && auto_save_not_disabled && enough_time_since_last_run_passed; then
fetch_and_run_tmux_resurrect_save_script
fi
fi
fi
}
main