Last active
February 24, 2025 19:56
-
-
Save balupton/4d1af347074e676ff9e5eb96c1cbaf3d to your computer and use it in GitHub Desktop.
Caching of `setup-environment-commands` using Dorothy User Configuration's `$DOROTHY/user/config/envrionment.bash`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
cache_validity_seconds=600 # six hundred seconds in ten minutes | |
cache_checksum_tool='/opt/homebrew/bin/xxhsum' # xxhsum and b2sum are equivalent in speed, then openssl sha256, then b3sum | |
if [[ -n "$cache_validity_seconds" && -x "$cache_checksum_tool" ]]; then | |
ppid=$$ | |
now_env="$(env -u TERM_SESSION_ID | sort)" | |
context_id="$shell-$("$cache_checksum_tool" <<<"$now_env" | cut -f1 -d ' ')" | |
lock_file="$XDG_CACHE_HOME/dorothy-environment-$context_id.lock" # this file is the process id that has the lock | |
result_file="$XDG_CACHE_HOME/dorothy-environment-$context_id.result" # this file is the environment modifications for the environment vars we are valid for | |
# wait for a exclusive lock | |
while :; do | |
# don't bother with a [[ -s "$lock_file" ]] before [cat] as the lock_file could have been removed between | |
wait="$(cat "$lock_file" 2>/dev/null || :)" | |
if [[ -z "$wait" ]]; then | |
__print_string "$ppid" >"$lock_file" | |
elif [[ "$wait" == "$ppid" ]]; then | |
break | |
fi | |
sleep "0.01$RANDOM" | |
done | |
# if cache is available and applicable | |
if [[ -f "$result_file" ]]; then | |
# if cache is still valid | |
now_seconds="$(date +%s)" | |
cache_seconds="$(date -r "$result_file" +%s)" | |
cache_ago_seconds="$((now_seconds - cache_seconds))" | |
if [[ "$cache_ago_seconds" -lt "$cache_validity_seconds" ]]; then | |
# then use the cache | |
trap - EXIT # remove the prior trap, as we are using our cache result, rather than a new env.bash result | |
cat "$result_file" | |
rm -f "$lock_file" | |
exit | |
else | |
: # __print_lines 'CACHE INVALIDATED' >/dev/tty | |
fi | |
else | |
: # __print_lines 'CACHE NOT FOUND' >/dev/tty | |
fi | |
# cache needs updating | |
function on_env_finish_cache { | |
local status=$? | |
if [[ $status -ne 0 ]]; then | |
rm -f "$lock_file" | |
return $status | |
fi | |
# store the result | |
on_env_finish | tee "$result_file" || { | |
status=$? | |
rm -f "$lock_file" | |
return $status | |
} | |
rm -f "$lock_file" | |
return 0 | |
} | |
trap on_env_finish_cache EXIT # replace our prior trap with one that caches the result | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment