summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <LukeShu@sbcglobal.net>2011-09-28 03:02:00 -0400
committerLuke Shumaker <LukeShu@sbcglobal.net>2011-09-28 03:02:00 -0400
commitaf58e68243f3b6b10d7dd177f54c55c3f379ee92 (patch)
tree616f03ed14ea60c2bcffc7e4817c20bb28131059
parent1a36a4497f417f008c9a5e19fcbdfc30f813db17 (diff)
add offlineimap-runner
-rw-r--r--.gitignore21
-rw-r--r--Makefile1
-rw-r--r--offlineimap-runner.sh60
3 files changed, 72 insertions, 10 deletions
diff --git a/.gitignore b/.gitignore
index 7cee854..ad1cfdc 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,18 +1,19 @@
*~
arg
-ord
-urldecode
-roll
-v-www-browser
-v-editor
-whatismyip
chardiff
chardiff_pre
chardiff_post
-newegg
-hangman
-sysfiles
daemon
+e
emacsmail
emacsterm
-e
+hangman
+newegg
+offlineimap-runner
+ord
+roll
+sysfiles
+urldecode
+v-www-browser
+v-editor
+whatismyip
diff --git a/Makefile b/Makefile
index 59161c0..89d25f6 100644
--- a/Makefile
+++ b/Makefile
@@ -15,6 +15,7 @@ BINFILES = \
emacsterm \
hangman \
newegg \
+ offlineimap-runner \
ord \
roll \
sysfiles \
diff --git a/offlineimap-runner.sh b/offlineimap-runner.sh
new file mode 100644
index 0000000..4828f35
--- /dev/null
+++ b/offlineimap-runner.sh
@@ -0,0 +1,60 @@
+#!/bin/sh
+
+# offlineimap is pretty nifty, and generally does the right thing, and is
+# remarkably fault-tolerant, even of it's own code.
+#
+# One nifty thing it doesn is to check if an instance of it is already running,
+# and to exit and let the original insance do it's thing. That lets us do
+# things like put it in a crontab. We wouldn't want to do this if it didn't have
+# that feature, because then
+# * If the previous run hadn't finished, things would break
+# I have it run every at m="*/5". 5 minutes is usually plenty of time, but
+# for large attachments, it might not be enough.
+# * If I wanted to run it manually, at say m="*/5+4", if it takes more than a
+# minute to run, then the instance invoked by cron would bork everything.
+
+# As awesome as this is, it has one fatal flaw: an instance hanging.
+# I've only had this happen once, but an instance of offlineimap invoked by cron
+# decided to hang up. After 2 days of recieving suspiciously little email, I
+# decided to check if offlineimap was running ok. That instance had hung up, not
+# recieving any mail, but preventing cron from starting another instance.
+#
+# So, here is my workaround: keep track of how many times in a row that we've
+# tried to start offlineimap, and it failed. If that number exceeds a specified
+# number, then assume the running instance hung, and kill it.
+
+# A note on notation:
+# the number/limit is the number of failures allowed *before* anything is killed.
+# So, if the limit is "1", the first time, it will just pass by. The second
+# time, it will see that we've already failed once, so if it's still running,
+# kill it.
+default_limit=2
+limit=${1-$default_limit}
+
+cookie_file="$HOME/.offlineimap.cookie"
+offlineimap='offlineimap -u quiet'
+
+# Try running offlineimap
+if $offlineimap; then
+ # Everything went smoothly, remove any record of failures
+ echo '0' > "$cookie_file"
+else
+ # oh noez!
+ # we interupt your regularly scheduled panic for a sanity check
+ if [ ! -f "$cookie_file" ]; then
+ echo '0' > "$cookie_file"
+ fi
+ # you may now resume panicing
+ number=`cat "$cookie_file"`
+ if [ "$number" -ge "$limit" ]; then # kill it!
+ # extract the first argument from our offlineimap command
+ set -- $offlineimap
+ killall $1
+ # We invoke offlineimap directly, instead of recursing on
+ # ourself because if something else is causing it to fail, then
+ # we'll fork-bomb
+ $offlineimap
+ else
+ echo $(($number+1)) > "$cookie_file"
+ fi
+fi