Improve mailsync script with parallel IMAP fetching

This commit is contained in:
lhark 2019-02-15 18:35:18 -05:00
parent 3ad6614284
commit 1561348716

View file

@ -1,20 +1,29 @@
#!/bin/sh
# Temp disable
#exit 0
maildir=~/mail
# find "$maildir" -mindepth 1 -maxdepth 1 -type d ! -name cur ! -name tmp ! -name new ! -name .notmuch -printf '%f\0' | xargs -0 -n1 --max-procs=0 mbsync
mailboxes=$(find "$maildir" -mindepth 1 -maxdepth 1 -type d ! -name cur ! -name tmp ! -name new ! -name .notmuch -printf '%f\n')
if pgrep -x mbsync > /dev/null ; then
>&2 echo "Isync is already running, aborting..."
LOCKFILE="/tmp/.mailsync.$USER.lock"
# /!\ locking method is susceptible to race condition
# Should be fine for cron job
if [ -e "$LOCKFILE" ] && kill -0 "$(cat "$LOCKFILE")"; then
echo "mailsync is already running"
exit 1
fi
timeout 10m mbsync -a
if [ "$?" -eq 124 ];then
>&2 echo "Command mbync -a timed out"
elif [ "$?" -eq 0 ];then
>&2 echo "Successful mail synchronisation"
else
>&2 echo "Isync exited with status $?"
fi
# make sure the lockfile is removed when we exit and then claim it
trap "rm -f $LOCKFILE; exit" INT TERM EXIT
echo $$ > "$LOCKFILE"
for m in $mailboxes; do
mbsync "$m" &
sleep 1
done
wait
fdm fetch
notmuch new
rm -f "$LOCKFILE"