[Xfce4-commits] <xfce-git-hooks:master> implement a mail queue for commit mails that get sent from a cronjob

Brian J. Tarricone brian at tarricone.org
Wed Aug 12 21:28:03 CEST 2009


Updating branch refs/heads/master
         to d1b4004fcfbfc1e740b294a3500b06f06e7a9aae (commit)
       from f6adb241d5b7a600269734ebbe83464a621e36d5 (commit)

commit d1b4004fcfbfc1e740b294a3500b06f06e7a9aae
Author: Brian J. Tarricone <brian at tarricone.org>
Date:   Mon Aug 10 13:39:53 2009 -0700

    implement a mail queue for commit mails that get sent from a cronjob

 hooks/update-03-send-commit-mails   |   29 ++++++++++++++++++++++++-----
 hooks/xfce-git-mail-queue.functions |   28 ++++++++++++++++++++++++++++
 hooks/xfce-process-git-mail-queue   |   25 +++++++++++++++++++++++++
 3 files changed, 77 insertions(+), 5 deletions(-)

diff --git a/hooks/update-03-send-commit-mails b/hooks/update-03-send-commit-mails
index f8d5a3a..f58197f 100755
--- a/hooks/update-03-send-commit-mails
+++ b/hooks/update-03-send-commit-mails
@@ -19,7 +19,11 @@
 # Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 # Boston, MA 02110-1301, USA.
 
+# FIXME: hardcoded path
+. xfce-git-mail-queue.functions
+
 DATEFORMAT="%F %R %z"
+MAIL_QUEUE_DIR=/var/spool/git-mail
 
 repository=`basename $PWD | cut -d. -f1`
 mailinglist=`git repo-config xfce-hooks.mailinglist`
@@ -242,15 +246,30 @@ EOF
   ) | send "$committer"
 }
 
+get_queue_file()
+{
+  last_file=`ls -1 "$MAIL_QUEUE_DIR" | tail -n 1`
+  [ "$last_file" ] || last_file=0
+  next_num=`expr $last_file + 1`
+  next_file=`printf %08u $next_num`
+  echo "$MAIL_QUEUE_DIR/$next_file"
+}
+
 send()
 {
   #cat $*
   #exit 1
-  if [[ -n "$envelopesender" ]]; then
-    /usr/sbin/sendmail -t -f "$1"
-  else
-    /usr/sbin/sendmail -t
-  fi
+  #if [[ -n "$envelopesender" ]]; then
+  #  /usr/sbin/sendmail -t -f "$1"
+  #else
+  #  /usr/sbin/sendmail -t
+  #fi
+
+  lock_queue_directory
+  mailfile=`get_queue_file`
+  [[ "$envelopesender" ]] && echo "X-Xfce-Envelope-Sender: $envelopesender" >$mailfile
+  cat >>$mailfile
+  unlock_queue_directory
 }
 
 case "$newref_type" in
diff --git a/hooks/xfce-git-mail-queue.functions b/hooks/xfce-git-mail-queue.functions
new file mode 100755
index 0000000..27b2a34
--- /dev/null
+++ b/hooks/xfce-git-mail-queue.functions
@@ -0,0 +1,28 @@
+export MAIL_QUEUE_DIR=/var/spool/git-mail
+
+lock_queue_directory()
+{
+  lockfile="$MAIL_QUEUE_DIR/.lock"
+
+  # here we test directory creation since that's atomic, whereas
+  # doing a test followed by a touch is not.
+  # to avoid waiting forever if someone else screwed up, we wait
+  # about as long as it would take to process the queue at 1 mail
+  # per second.
+  waittime=`ls -1 "$MAIL_QUEUE_DIR" | wc -l`
+  waittime=`$waittime + 10`  # some wiggle room
+  
+  while ! mkdir $lockfile 2>/dev/null && [ $waittime -ge 0 ]; do
+    sleep 1
+    waittime=`expr $waittime - 1`
+  done
+
+  trap 'rmdir "$lockfile"; exit $?' INT TERM EXIT
+}
+
+unlock_queue_directory()
+{
+  lockfile="$MAIL_QUEUE_DIR/.lock"
+  rmdir "$lockfile"
+  trap - INT TERM EXIT
+}
diff --git a/hooks/xfce-process-git-mail-queue b/hooks/xfce-process-git-mail-queue
new file mode 100755
index 0000000..f24a72c
--- /dev/null
+++ b/hooks/xfce-process-git-mail-queue
@@ -0,0 +1,25 @@
+#!/bin/sh
+
+# FIXME: hardcoded path
+. xfce-git-mail-queue.functions
+
+lock_queue_directory
+
+pushd "$MAIL_QUEUE_DIR" >/dev/null 2>&1
+
+mailfiles=`ls -1`
+
+for mf in $mailfiles; do
+  envelopesender=`awk '/^X-Xfce-Envelope-Sender: /{ print $2; }' $mf`
+  if [[ "$envelopesender" ]]; then
+    grep -v '^X-Xfce-Envelope-Sender: ' "$mf" | /usr/sbin/sendmail -t -f "$envelopesender"
+  else
+    /usr/sbin/sendmail -t <"$mf"
+  fi
+  rm -f "$mf"
+  sleep 1
+done
+
+popd >/dev/null 2>&1
+
+unlock_queue_directory



More information about the Xfce4-commits mailing list