home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / cnews.tar / batch / sendbatches < prev    next >
Text File  |  1995-04-27  |  5KB  |  243 lines

  1. #! /bin/sh
  2. # Master batching control.
  3.  
  4. # =()<. ${NEWSCONFIG-@<NEWSCONFIG>@}>()=
  5. . ${NEWSCONFIG-/etc/news/bin/config}
  6.  
  7. PATH=$NEWSCTL/bin:$NEWSBIN/batch:$NEWSBIN:$NEWSPATH ; export PATH
  8. umask $NEWSUMASK
  9.  
  10. origpath="$PATH"
  11.  
  12. args="$*"
  13.  
  14. parms=$NEWSCTL/batchparms
  15. log=$NEWSCTL/batchlog
  16. parallel=n
  17. debug=n
  18. classpat=.
  19. for dummy
  20. do
  21.     case "$1" in
  22.     -p)    parallel=y        ;;
  23.     -d)    debug=y            ;;
  24.     -f)    parms="$2" ; shift    ;;
  25.     -c)    classpat="$2" ; shift    ;;
  26.     --)    shift ; break        ;;
  27.     -*)    echo "$0: unknown option \`$1'" >&2 ; exit 2    ;;
  28.     *)    break            ;;
  29.     esac
  30.     shift
  31. done
  32. case "$parms" in
  33. /*)    ;;
  34. *)    echo "$0: \`$parms' is not a full pathname" >&2 ; exit 2    ;;
  35. esac
  36.  
  37. # set up unlocker
  38. glock=
  39. llock=
  40. exitst=1
  41. tmp=/tmp/sb$$
  42. trap "unlock \$glock \$llock ; rm -f $tmp ; trap 0 ; exit \$exitst" 0 1 2 15
  43.  
  44. # lock against multiple simultaneous execution, unless suppressed by -p
  45. if test " $parallel" = " n"
  46. then
  47.     lock -o LOCKbatch $$ || exit
  48.     glock=LOCKbatch
  49. fi
  50.  
  51. # let's be paranoid...
  52. if test ! -d $NEWSARTS/out.going
  53. then
  54.     echo "no $NEWSARTS/out.going directory!" |
  55.                     report -u "sendbatches cannot run"
  56.     exit
  57. fi
  58. cd $NEWSARTS/out.going
  59.  
  60. # Determine what systems are being requested, in what order.
  61. case "$#" in
  62. 0)    awk "\$2 ~ /$classpat/" $parms >$tmp
  63.     # real classpat filter is done by bpfind; the question here is whether
  64.     #    this class is the default
  65.     if egrep '^/default/[     ]' $tmp >/dev/null    # default line found
  66.     then
  67.         syses=`ls -tr | egrep -v '^[@.]'`    # oldest first
  68.     else
  69.         syses="`egrep '^[^/#]' $parms | awk '{ print $1 }'`"
  70.     fi
  71.     ;;
  72. *)    syses="$*"    ;;
  73. esac
  74. case $debug in
  75. y)    for sys in $syses
  76.     do
  77.         echo $sys
  78.     done
  79.     exitst=0
  80.     exit
  81.     ;;
  82. esac
  83.  
  84. # Start up logging.
  85. echo "`date`, sendbatches $$ $args" >>$log
  86.  
  87. # Run through them.
  88. for sys in $syses
  89. do
  90.     # Move into his directory, include it in search path.
  91.     here=$NEWSARTS/out.going/$sys
  92.     if test ! -d $here
  93.     then
  94.         echo "cannot find batch directory for \`$sys'" |
  95.                     report -u 'missing batch directory'
  96.         continue
  97.     fi
  98.     cd $here
  99.     PATH=$here:$origpath ; export PATH
  100.     NEWSSITE=$sys ; export NEWSSITE        # For site-specific programs.
  101.     NEWSSITEDIR=$here ; export NEWSSITEDIR    # ditto
  102.  
  103.     # Is there anything to do?
  104.     tdfiles=`ls | egrep '^togo\.([0-9]|more|next)'`
  105.     if test " $tdfiles" = " " -a ! -s togo
  106.     then
  107.         continue            # no
  108.     fi
  109.  
  110.     # Pick up the batchparms line.
  111.     eval `bpfind "$classpat" "$sys" $parms`    # sets the bpxxx variables
  112.     if test " $bpokay" = " x"
  113.     then
  114.         continue    # wrong class
  115.     elif test " $bpokay" = " !"
  116.     then
  117.         echo "$sys    backlog ? (disabled in $parms)" >>$log
  118.         continue
  119.     elif test " $bpokay" != " y"
  120.     then
  121.         (
  122.             echo "bad or missing batchparms line for \`$sys':"
  123.             echo "    $bpgrump"
  124.         ) | report -u 'batchparms file error'
  125.         continue
  126.     fi
  127.  
  128.     # lock against multiple simultaneous batchers for this site
  129.     if lock -o ./LOCKb $$
  130.     then
  131.         llock="$here/LOCKb"
  132.     else
  133.         continue    # try the next site
  134.     fi
  135.  
  136.     # How many to send?
  137.     roomfor=`spacefor $bpsize outbound $sys`
  138.     if test " $bpqueue" != " -"
  139.     then
  140.         outstand="`queuelen $sys`"
  141.         allowed=`expr $bpqueue - $outstand`
  142.     else
  143.         allowed=$roomfor
  144.     fi
  145.     if test " $roomfor" -gt " $allowed"
  146.     then
  147.         nbatch=$allowed
  148.     else
  149.         nbatch=$roomfor
  150.     fi
  151.  
  152.     # If not allowed to send, remember reason.
  153.     status='batching stalled'
  154.     if test " $nbatch" -le 0
  155.     then
  156.         if test " $roomfor" -le 0
  157.         then
  158.             status='disk too full for batching'
  159.         else
  160.             status='queue full, no recent movement'
  161.         fi
  162.     fi
  163.  
  164.     # Try sending some.
  165.     rm -f togo.leftover
  166.     while test " $nbatch" -gt 0
  167.     do
  168.         # Does he have batches prepared already?
  169.         if test "`echo togo.[0-9]`" = 'togo.[0-9]'
  170.         then
  171.             # No -- need some more batches.
  172.             if test ! -s togo && test ! -s togo.more &&
  173.                             test ! -s togo.next
  174.             then
  175.                 break        # Nothing left to do.
  176.             fi
  177.             batchsplit $bpsize $bpmax $nbatch >/tmp/bs$$ 2>&1
  178.             if test -s /tmp/bs$$
  179.             then
  180.                 report -u "batch-splitting problems for $sys" </tmp/bs$$
  181.             fi
  182.             rm -f /tmp/bs$$
  183.         fi
  184.  
  185.         # Send some batches.
  186.         them=`ls | egrep '^togo\.[0-9]' | sed "${nbatch}q"`
  187.         cd $NEWSARTS
  188.         for f in $them
  189.         do
  190.             sh -c "$bpcmd" <$here/$f >/tmp/nb$$ 2>&1
  191.             if test -s $here/togo.leftover
  192.             then
  193.                 leftis=$f
  194.                 break        # NOTE BREAK OUT
  195.             elif test ! -s /tmp/nb$$
  196.             then
  197.                 # okay
  198.                 status='batches flowing'
  199.                 rm -f $here/$f /tmp/nb$$
  200.             else
  201.                 (
  202.                     echo "batching $f for \`$sys' failed"
  203.                     cat /tmp/nb$$
  204.                     echo "aborting"
  205.                 ) | report -u 'sendbatches failure'
  206.                 rm -f /tmp/nb$$
  207.                 exit
  208.             fi
  209.         done
  210.         cd $here
  211.         if test -s togo.leftover
  212.         then
  213.             mv togo.leftover $leftis
  214.             status='batching slow or stalled'
  215.             break                # NOTE BREAK OUT
  216.         fi
  217.         ndone=`echo $them | wc -w`
  218.         nbatch=`expr $nbatch - $ndone`
  219.  
  220.         # Recheck the space -- it can fall for other reasons.
  221.         roomfor=`spacefor $bpsize outbound $sys`
  222.         if test " $nbatch" -gt " $roomfor"
  223.         then
  224.             nbatch=$roomfor
  225.         fi
  226.     done
  227.  
  228.     # Report status, if appropriate.
  229.     nart=`cat togo* | wc -l | awk '{print $1}'`
  230.     if test " $nart" -gt 0
  231.     then
  232.         echo "$sys    backlog $nart ($status)" >>$log
  233.     fi
  234.  
  235.     # unlock this site
  236.     llock=
  237.     unlock ./LOCKb
  238. done
  239.  
  240. echo "`date`, sendbatches $$ $args" >>$log
  241. echo >>$log
  242. exitst=0
  243.