Skip to content

Instantly share code, notes, and snippets.

@fidgetingbits
Created July 8, 2021 14:10
Show Gist options
  • Save fidgetingbits/cfc1699da2e8a60533db6c4cfdf390c3 to your computer and use it in GitHub Desktop.
Save fidgetingbits/cfc1699da2e8a60533db6c4cfdf390c3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# trap ctrl-c and call ctrl_c()
trap ctrl_c INT
trap ctrl_c SIGUSR1
MONITOR_PID=0
ctrl_c() {
#echo "Restarting talon..."
notify-send.sh "Restaring talon..."
pkill "run.sh"
#notify-send.sh "Killing monitor ${MONITOR_PID}"
kill -9 $MONITOR_PID 2>/dev/null
}
monitor_talon() {
local LOG_FILE=$1
while true; do
inotifywait -e modify "${LOG_FILE}" >/dev/null 2>&1
tail -n 10 "${LOG_FILE}" | while read -r line; do
if [[ "${line}" == *"(stalled)"* ]]; then
count=$(echo "${line}" | cut -d@ -f2 | cut -ds -f1 | cut -d. -f1)
# Keep the count high in case we are running certain scripts
if [[ "${count}" -ge "30" ]]; then
notify-send.sh "Talon stalled. Auto-restarting"
PID=$(pgrep -f "releases/latest/talon")
# send SIGINT
kill -s 2 "${PID}"
exit 1
fi
fi
done
# This $? comes from exit in the read line while loop above
if [[ $? == 1 ]]; then
exit 1
fi
done
}
TALON_FOLDER=~/source/talon/releases/latest/
cd ${TALON_FOLDER} || (
echo "ERROR: ${TALON_FOLDER} does not exist?"
exit 1
)
PLUGIN_FOLDER=${TALON_FOLDER}/resources/talon_plugins/
# expect something like: v0.1.5-23-g23f3
LAST_VERSION=$(strings ${TALON_FOLDER}/talon | grep "v0.*-g")
PATCH_FOLDER=~/talon/patches/
~/.talon/bin/pip -q install -r ~/.talon/user/fidget/requirements.txt
# TODO - this should be moved into talon somewhere?
#~/source/obs-cli/env/bin/python ~/source/obs-cli/obs-cli.py --start-recording
while true; do
# Detect any updates while the script is still running
CURRENT_VERSION=$(strings ${TALON_FOLDER}/talon | grep "v0.*-g")
if [ "${CURRENT_VERSION}" != "${LAST_VERSION}" ]; then
notify-send.sh "Update detected. Patching"
~/.talon/bin/pip -q install -r ~/.talon/user/fidget/requirements.txt
# Backup the talon plugins for the current version
cp -r ${PLUGIN_FOLDER}/* ${PATCH_FOLDER}/backup/
LAST_VERSION=${CURRENT_VERSION}
fi
# Patch in custom plugins
# XXX - make this a function and call it before the loop is well, then move
# this part into the version test above
for FILE in "${PATCH_FOLDER}"/*.custom; do
FILENAME=$(basename "${FILE/.custom/}")
PLUGIN_SUM=$(sha256sum "${PLUGIN_FOLDER}"/"${FILENAME}")
CUSTOM_SUM=$(sha256sum "${FILE}")
if [ "${PLUGIN_SUM}" != "${CUSTOM_SUM}" ]; then
notify-send.sh "Replacing ${FILENAME}"
cp "${FILE}" "${PLUGIN_FOLDER}"/"${FILENAME}"
fi
done
notify-send.sh "Running talon"
LOG_FILE=~/talon/logs/$(date +%Y-%m-%d-%H-%M-%S).log
touch "${LOG_FILE}"
monitor_talon "${LOG_FILE}" &
MONITOR_PID=$!
if [ ! -f "run.sh" ]; then
echo "ERROR: run.sh not found in: $PWD"
exit 1
fi
./run.sh 2>&1 | tee -a "${LOG_FILE}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment