Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait for the sqlite3 process to terminate #121

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions sqlite-history.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ else
fi

typeset -g HISTDB_INODE=""
typeset -g HISTDB_SQLITE_PID=""
typeset -g HISTDB_SESSION=""
typeset -g HISTDB_HOST=""
typeset -g HISTDB_INSTALLED_IN="${(%):-%N}"
Expand All @@ -36,14 +37,13 @@ _histdb_stop_sqlite_pipe () {
exec {HISTDB_FD}>&-; # https://stackoverflow.com/a/22794374/2639190
fi
fi
# Sometimes, it seems like closing the fd does not terminate the
# sqlite batch process, so here is a horrible fallback.
# if [[ -n $HISTDB_SQLITE_PID ]]; then
# ps -o args= -p $HISTDB_SQLITE_PID | read -r args
# if [[ $args == "sqlite3 -batch ${HISTDB_FILE}" ]]; then
# kill -TERM $HISTDB_SQLITE_PID
# fi
# fi
# Wait for the sqlite3 process to terminate
if [[ "$1" == "block" ]] && [[ -n "${HISTDB_SQLITE_PID}" ]]; then
while ps -o pid= -p "${HISTDB_SQLITE_PID}" &>/dev/null; do
sleep 1
done
HISTDB_SQLITE_PID=""
fi
}

add-zsh-hook zshexit _histdb_stop_sqlite_pipe
Expand All @@ -53,6 +53,7 @@ _histdb_start_sqlite_pipe () {
setopt local_options no_notify no_monitor
mkfifo $PIPE
sqlite3 -batch -noheader "${HISTDB_FILE}" < $PIPE >/dev/null &|
HISTDB_SQLITE_PID=$!
sysopen -w -o cloexec -u HISTDB_FD -- $PIPE
command rm $PIPE
zstat -A HISTDB_INODE +inode ${HISTDB_FILE}
Expand Down Expand Up @@ -222,7 +223,7 @@ histdb-sync () {
git add .gitattributes
git add "${HISTDB_FILE:t}"
fi
_histdb_stop_sqlite_pipe # Stop in case of a merge, starting again afterwards
_histdb_stop_sqlite_pipe block # Stop in case of a merge, starting again afterwards
git commit -am "history" && git pull --no-edit && git push
_histdb_start_sqlite_pipe
popd -q
Expand Down