home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / CNEWS / _CNEWS.TAR / usr / lib / newsbin / batch / sendbatches < prev    next >
Encoding:
Text File  |  1994-09-02  |  4.4 KB  |  216 lines

  1. #! /bin/sh
  2. # Master batching control.
  3.  
  4. # =()<. ${NEWSCONFIG-@<NEWSCONFIG>@}>()=
  5. . ${NEWSCONFIG-/var/lib/news/bin/config}
  6.  
  7. PATH=$NEWSCTL/bin:$NEWSBIN/batch:$NEWSBIN:$NEWSPATH ; export PATH
  8. umask $NEWSUMASK
  9.  
  10. origpath="$PATH"
  11.  
  12. parms=$NEWSCTL/batchparms
  13. log=$NEWSCTL/batchlog
  14.  
  15. case "$-" in
  16. *x*)    n='***NOTE***'
  17.     echo "$n expect spurious batching failures due to \`sh -x' $n" >&2
  18.     # no, there isn't any portable fix
  19.     ;;
  20. esac
  21.  
  22. # set up unlocker
  23. glock=
  24. llock=
  25. trap "rm -f \$glock \$llock ; exit 0" 0 1 2 15
  26.  
  27. # lock against multiple simultaneous execution, unless suppressed by -p
  28. case $1 in
  29. -p)    shift    ;;
  30. *)    lock="$NEWSCTL/LOCKbatch"
  31.     ltemp="$NEWSCTL/L.$$"
  32.     echo $$ >$ltemp
  33.     glock="$ltemp"
  34.     if newslock $ltemp $lock
  35.     then
  36.         glock="$ltemp $lock"
  37.     else
  38.         exit 0
  39.     fi
  40.     ;;
  41. esac
  42.  
  43. cd $NEWSARTS/out.going
  44.  
  45. # Determine what systems are being requested, in what order.
  46. case "$1" in
  47. -d)    debug=yes
  48.     shift
  49.     ;;
  50. esac
  51. case $# in
  52. 0)    if egrep '^/default/[     ]' $parms >/dev/null    # default line found
  53.     then
  54.         syses=`ls -tr | egrep -v '^[@.]'`    # oldest first
  55.     else
  56.         syses="`egrep '^[^/#]' $parms | awk '{ print $1 }'`"
  57.     fi
  58.     ;;
  59. *)    syses="$*"    ;;
  60. esac
  61. case $debug in
  62. yes)    for sys in $syses
  63.     do
  64.         echo $sys
  65.     done
  66.     exit 0
  67.     ;;
  68. esac
  69.  
  70. # Start up logging.
  71. echo "`date`, sendbatches $$" >>$log
  72.  
  73. # Run through them.
  74. for sys in $syses
  75. do
  76.     # Move into his directory, include it in search path.
  77.     here=$NEWSARTS/out.going/$sys
  78.     if test ! -d $here
  79.     then
  80.         echo "$0: cannot find batch directory for \`$sys'" |
  81.                             mail $NEWSMASTER
  82.         continue
  83.     fi
  84.     cd $here
  85.     PATH=$here:$origpath ; export PATH
  86.     NEWSSITE=$sys ; export NEWSSITE        # For site-specific programs.
  87.     NEWSSITEDIR=$here ; export NEWSSITEDIR    # ditto
  88.  
  89.     # Is there anything to do?
  90.     files=`echo togo*`
  91.     if test "$files" = 'togo*' || test "$files" = "togo" -a ! -s togo
  92.     then
  93.         continue            # no
  94.     fi
  95.  
  96.     # Pick up the batchparms line.
  97.     ctlline="`egrep \"^$sys[     ]\" $parms | sed 1q`"
  98.     if test " $ctlline" = " "
  99.     then
  100.         ctlline="`egrep '^/default/[     ]' $parms | sed 1q`"
  101.     fi
  102.     set $ctlline
  103.     if test " $#" -ne 6
  104.     then
  105.         echo "$0: bad or missing batchparms line for \`$sys'" |
  106.                             mail $NEWSMASTER
  107.         continue
  108.     fi
  109.     batchsize=$2
  110.     limit=$3
  111.     batcher=$4
  112.     muncher=$5
  113.     sender=$6
  114.  
  115.     # lock against multiple simultaneous execution of batcher for this site
  116.     lock="LOCKbatch"
  117.     ltemp="L.$$"
  118.     llock="$ltemp"
  119.     echo $$ >$ltemp
  120.     if newslock $ltemp $lock
  121.     then
  122.         llock="$ltemp $lock"
  123.     else
  124.         rm -f $ltemp    # didn't get the lock; this site is busy
  125.         continue    # try the next site
  126.     fi
  127.  
  128.     # How many to send?
  129.     outstand="`queuelen $sys`"
  130.     nbatch=`expr $limit - $outstand`
  131.     roomfor=`spacefor $batchsize outbound $sys`
  132.     if test " $nbatch" -gt " $roomfor"
  133.     then
  134.         nbatch=$roomfor
  135.     fi
  136.  
  137.     # If not allowed to send, remember reason.
  138.     status='batches flowing'
  139.     if test " $nbatch" -le 0
  140.     then
  141.         if test " $roomfor" -le 0
  142.         then
  143.             status='disk too full for batching'
  144.         else
  145.             status='queue full, no recent movement'
  146.         fi
  147.     fi
  148.  
  149.     # Try sending some.
  150.     while test " $nbatch" -gt 0
  151.     do
  152.         # Does he have batches prepared already?
  153.         if test "`echo togo.[0-9]`" = 'togo.[0-9]'
  154.         then
  155.             # No -- need some more batches.
  156.             if test ! -s togo && test ! -s togo.more &&
  157.                             test ! -s togo.next
  158.             then
  159.                 break        # Nothing left to do.
  160.             fi
  161.             batchsplit $batchsize $nbatch
  162.         fi
  163.  
  164.         # Send some batches.
  165.         them=`ls | egrep '^togo\.[0-9]' | sed "${nbatch}q"`
  166.         for f in $them
  167.         do
  168.             # Sigh... sh -x on this won't work, because the -x
  169.             # output ends up in /tmp/nb$$, and there is no way
  170.             # to either (a) separate it out (bearing in mind that
  171.             # some shells randomly interleave -x lines from the
  172.             # processes in a pipeline) or (b) turn off -x for
  173.             # a moment in a portable way.
  174.             ( ( cd $NEWSARTS ; $batcher $here/$f ) | $muncher |
  175.                         $sender $sys ) >/tmp/nb$$ 2>&1
  176.             if test $? -eq 0 -a ! -s /tmp/nb$$
  177.             then
  178.                 # okay
  179.                 rm -f $f /tmp/nb$$
  180.             else
  181.                 (
  182.                     echo "$0: batching $f for \`$sys' failed"
  183.                     cat /tmp/nb$$
  184.                     echo "$0: aborting"
  185.                 ) | mail $NEWSMASTER
  186.                 rm -f /tmp/nb$$
  187.                 # unlock this site
  188.                 rm -f $ltemp $lock
  189.                 exit 1
  190.             fi
  191.         done
  192.         ndone=`echo $them | wc -w`
  193.         nbatch=`expr $nbatch - $ndone`
  194.  
  195.         # Recheck the space -- it can fall for other reasons.
  196.         roomfor=`spacefor $batchsize outbound $sys`
  197.         if test " $nbatch" -gt " $roomfor"
  198.         then
  199.             nbatch=$roomfor
  200.         fi
  201.     done
  202.  
  203.     # Report status, if appropriate.
  204.     nart=`cat togo* | wc -l | awk '{print $1}'`
  205.     if test " $nart" -gt 0
  206.     then
  207.         echo "$sys    backlog $nart ($status)" >>$log
  208.     fi
  209.  
  210.     # unlock this site
  211.     rm -f $ltemp $lock
  212. done
  213.  
  214. echo "`date`, sendbatches $$" >>$log
  215. echo >>$log
  216.