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 #!/bin/sh
# Temp disable maildir=~/mail
#exit 0 # 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 LOCKFILE="/tmp/.mailsync.$USER.lock"
>&2 echo "Isync is already running, aborting..." # /!\ 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 exit 1
fi fi
timeout 10m mbsync -a # make sure the lockfile is removed when we exit and then claim it
if [ "$?" -eq 124 ];then trap "rm -f $LOCKFILE; exit" INT TERM EXIT
>&2 echo "Command mbync -a timed out" echo $$ > "$LOCKFILE"
elif [ "$?" -eq 0 ];then
>&2 echo "Successful mail synchronisation" for m in $mailboxes; do
else mbsync "$m" &
>&2 echo "Isync exited with status $?" sleep 1
fi done
wait
fdm fetch fdm fetch
notmuch new notmuch new
rm -f "$LOCKFILE"