chiark / gitweb /
Parallelise
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 27 Jun 2016 13:46:54 +0000 (14:46 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 27 Jun 2016 13:46:54 +0000 (14:46 +0100)
notify
onemethod [new file with mode: 0755]

diff --git a/notify b/notify
index f061ffca8fe059e44ab50700e567ed5970d434d0..e13d97e6cb91a095842e59ed8c9295005bd2b2ed 100755 (executable)
--- a/notify
+++ b/notify
@@ -34,30 +34,32 @@ while read settingname value; do
        esac
 done
 
-exec <"$rcpts"
-line=0
-while read method data; do
-       line=$(( $line+1 ))
-       case "$method" in
-       #*|'')  continue ;;
-       [^a-z]*) echo >&2 "huh ? $rcpts:$line: $method"; continue ;;
-       esac
-       log=log-$method-$line.txt
-       exec >$log
-       set +e
-       printf >&2 "$method"
-       exec 3>&2 2>&1
-       set -x
-       ./"via-$method" "$msg" $data 2>&1
-       rc=$?
-       set +x
-       exec 2>&3 3>&-
-       set -e
-       if [ $rc != 0 ]; then
-               printf >&2 -- "-FAIL:%s\n" $log
-       else
-               printf >&2 " "
-       fi
-done
-exec >&2
-echo >&2
+perl <"$rcpts" -wne '
+    use strict;
+    our @children;
+    our @passon;
+    BEGIN { @passon = @ARGV; @ARGV = (); }
+    s/^\s+//;
+    s/\s+$//;
+    next if m/^\#/;
+    next unless m/\S/;
+    die unless m/^([a-z]\w+)\s/;
+    my $method = $1;
+    my $child = fork;
+    defined $child or die $!;
+    if (!$child) {
+        exec "./onemethod", $method, @passon;
+       die $!;
+    }
+    push @children, $child;
+    END {
+        foreach my $child (@children) {
+           $!=$?=0;
+           my $got = waitpid $child, 0;
+           die $! unless $got==$child;
+           warn "$method [$child] $?" if $?;
+       }
+    }
+' "$rcpts" "$msg"
+
+echo
diff --git a/onemethod b/onemethod
new file mode 100755 (executable)
index 0000000..252c084
--- /dev/null
+++ b/onemethod
@@ -0,0 +1,47 @@
+#!/bin/bash
+set -e
+usage () { cat <<END
+
+usage: ./onemethod METHOD recipientsfile 'message'
+see usage for notify
+
+END
+}
+
+case "$#.$1" in
+3.[^-]*)       ;;
+*)             usage >&2; exit 1 ;;
+esac
+
+onemethod="$1"
+rcpts="$2"
+msg="$3"
+
+exec <"$rcpts"
+line=0
+while read method data; do
+       line=$(( $line+1 ))
+       case "$method" in
+       #*|'')          continue ;;
+       [^a-z]*)        echo >&2 "huh ? $rcpts:$line: $method"; continue ;;
+       "$onemethod")   ;;
+       *)              continue ;;
+       esac
+       log=log-$method-$line.txt
+       exec >$log
+       set +e
+       printf >&2 "."
+       exec 3>&2 2>&1
+       set -x
+       ./"via-$method" "$msg" $data 2>&1
+       rc=$?
+       set +x
+       exec 2>&3 3>&-
+       set -e
+       if [ $rc != 0 ]; then
+               printf >&2 -- "\n$method-FAIL:%s\n" $log
+       else
+               printf >&2 " $method"
+       fi
+done
+exec >&2