common/bin/hgforest.sh

Print this page
rev 1022 : 8041151: More concurrent hgforest
Reviewed-by: duke


  55     -*)  # bad option
  56       usage
  57       ;;
  58 
  59      * )  # non option
  60       break
  61       ;;
  62   esac
  63   shift
  64 done
  65 
  66 
  67 command="$1"; shift
  68 command_args="$@"
  69 
  70 usage() {
  71       echo "usage: $0 [-q|--quiet] [-v|--verbose] [-s|--sequential] [--] <command> [commands...]" > ${status_output}
  72       exit 1
  73 }
  74 
  75 
  76 if [ "x" = "x$command" ] ; then
  77   echo "ERROR: No command to hg supplied!"
  78   usage
  79 fi
  80 










  81 # Clean out the temporary directory that stores the pid files.
  82 tmp=/tmp/forest.$$
  83 rm -f -r ${tmp}
  84 mkdir -p ${tmp}
  85 
  86 safe_interrupt () {
  87   if [ -d ${tmp} ]; then
  88     if [ "`ls ${tmp}/*.pid`" != "" ]; then
  89       echo "Waiting for processes ( `cat ${tmp}/*.pid | tr '\n' ' '`) to terminate nicely!" > ${status_output}
  90       sleep 1
  91       # Pipe stderr to dev/null to silence kill, that complains when trying to kill
  92       # a subprocess that has already exited.
  93       kill -TERM `cat ${tmp}/*.pid | tr '\n' ' '` 2> /dev/null
  94       wait
  95       echo "Interrupt complete!" > ${status_output}
  96     fi
  97     rm -f -r ${tmp}
  98   fi
  99   exit 130
 100 }


 193         echo "allow_push = *"
 194         echo "push_ssl = False"
 195 
 196         echo "[paths]"
 197         for i in ${repos} ${repos_extra} ; do
 198           if [ "${i}" != "." ] ; then
 199             echo "/$(basename $(pwd))/${i} = ${i}"
 200           else
 201             echo "/$(basename $(pwd)) = $(pwd)"
 202           fi
 203         done
 204       ) > ${tmp}/serve.web-conf
 205 
 206       echo "serving root repo $(basename $(pwd))"
 207 
 208       (PYTHONUNBUFFERED=true hg${global_opts} serve -A ${status_output} -E ${status_output} --pid-file ${tmp}/serve.pid --web-conf ${tmp}/serve.web-conf; echo "$?" > ${tmp}/serve.pid.rc ) 2>&1 &
 209     ) 2>&1 | sed -e "s@^@serve:   @" > ${status_output}
 210   ) &
 211 else
 212   # Run the supplied command on all repos in parallel.


 213   n=0










 214   for i in ${repos} ${repos_extra} ; do
 215     n=`expr ${n} '+' 1`
 216     repopidfile=`echo ${i} | sed -e 's@./@@' -e 's@/@_@g'`
 217     reponame=`echo ${i} | sed -e :a -e 's/^.\{1,20\}$/ &/;ta'`
 218     pull_base="${pull_default}"
 219     for j in $repos_extra ; do
 220       if [ "$i" = "$j" ] ; then
 221           pull_base="${pull_extra}"
 222       fi
 223     done

 224     (
 225       (
 226         if [ "${command}" = "clone" -o "${command}" = "fclone" -o "${command}" = "tclone" ] ; then
 227           pull_newrepo="`echo ${pull_base}/${i} | sed -e 's@\([^:]/\)//*@\1@g'`"
 228           path="`dirname ${i}`"
 229           if [ "${path}" != "." ] ; then
 230             times=0
 231             while [ ! -d "${path}" ]   ## nested repo, ensure containing dir exists
 232             do
 233               times=`expr ${times} '+' 1`
 234               if [ `expr ${times} '%' 10` -eq 0 ] ; then
 235                 echo "${path} still not created, waiting..." > ${status_output}
 236               fi
 237               sleep 5
 238             done
 239           fi
 240           echo "hg clone ${pull_newrepo} ${i}" > ${status_output}
 241           (PYTHONUNBUFFERED=true hg${global_opts} clone ${pull_newrepo} ${i}; echo "$?" > ${tmp}/${repopidfile}.pid.rc ) 2>&1 &
 242         else
 243           echo "cd ${i} && hg${global_opts} ${command} ${command_args}" > ${status_output}
 244           cd ${i} && (PYTHONUNBUFFERED=true hg${global_opts} ${command} ${command_args}; echo "$?" > ${tmp}/${repopidfile}.pid.rc ) 2>&1 &
 245         fi
 246 
 247         echo $! > ${tmp}/${repopidfile}.pid
 248       ) 2>&1 | sed -e "s@^@${reponame}:   @" > ${status_output}



 249     ) &
 250 
 251     if [ `expr ${n} '%' ${at_a_time}` -eq 0 -a "${sflag}" = "false" ] ; then
 252       sleep 2
 253       echo "Waiting 5 secs before spawning next background command." > ${status_output}
 254       sleep 3





 255     fi
 256 
 257     if [ "${sflag}" = "true" ] ; then











 258         wait
 259     fi

 260   done
 261 fi
 262 
 263 # Wait for all hg commands to complete
 264 wait
 265 
 266 # Terminate with exit 0 only if all subprocesses were successful
 267 ec=0
 268 if [ -d ${tmp} ]; then
 269   for rc in ${tmp}/*.pid.rc ; do
 270     exit_code=`cat ${rc} | tr -d ' \n\r'`
 271     if [ "${exit_code}" != "0" ] ; then
 272       repo="`echo ${rc} | sed -e s@^${tmp}@@ -e 's@/*\([^/]*\)\.pid\.rc$@\1@' -e 's@_@/@g'`"
 273       echo "WARNING: ${repo} exited abnormally." > ${status_output}
 274       ec=1
 275     fi
 276   done
 277 fi
 278 exit ${ec}


  55     -*)  # bad option
  56       usage
  57       ;;
  58 
  59      * )  # non option
  60       break
  61       ;;
  62   esac
  63   shift
  64 done
  65 
  66 
  67 command="$1"; shift
  68 command_args="$@"
  69 
  70 usage() {
  71       echo "usage: $0 [-q|--quiet] [-v|--verbose] [-s|--sequential] [--] <command> [commands...]" > ${status_output}
  72       exit 1
  73 }
  74 

  75 if [ "x" = "x$command" ] ; then
  76   echo "ERROR: No command to hg supplied!"
  77   usage
  78 fi
  79 
  80 # Check if we can use fifos for monitoring sub-process completion.
  81 on_windows=`uname -s | egrep -ic -e 'cygwin|msys'`
  82 if [ on_windows = "1" ]; then
  83   # cygwin has (2014-04-18) broken (single writer only) FIFOs
  84   # msys has (2014-04-18) no FIFOs.
  85   have_fifos="false"
  86 else
  87   have_fifos="true"
  88 fi
  89 
  90 # Clean out the temporary directory that stores the pid files.
  91 tmp=/tmp/forest.$$
  92 rm -f -r ${tmp}
  93 mkdir -p ${tmp}
  94 
  95 safe_interrupt () {
  96   if [ -d ${tmp} ]; then
  97     if [ "`ls ${tmp}/*.pid`" != "" ]; then
  98       echo "Waiting for processes ( `cat ${tmp}/*.pid | tr '\n' ' '`) to terminate nicely!" > ${status_output}
  99       sleep 1
 100       # Pipe stderr to dev/null to silence kill, that complains when trying to kill
 101       # a subprocess that has already exited.
 102       kill -TERM `cat ${tmp}/*.pid | tr '\n' ' '` 2> /dev/null
 103       wait
 104       echo "Interrupt complete!" > ${status_output}
 105     fi
 106     rm -f -r ${tmp}
 107   fi
 108   exit 130
 109 }


 202         echo "allow_push = *"
 203         echo "push_ssl = False"
 204 
 205         echo "[paths]"
 206         for i in ${repos} ${repos_extra} ; do
 207           if [ "${i}" != "." ] ; then
 208             echo "/$(basename $(pwd))/${i} = ${i}"
 209           else
 210             echo "/$(basename $(pwd)) = $(pwd)"
 211           fi
 212         done
 213       ) > ${tmp}/serve.web-conf
 214 
 215       echo "serving root repo $(basename $(pwd))"
 216 
 217       (PYTHONUNBUFFERED=true hg${global_opts} serve -A ${status_output} -E ${status_output} --pid-file ${tmp}/serve.pid --web-conf ${tmp}/serve.web-conf; echo "$?" > ${tmp}/serve.pid.rc ) 2>&1 &
 218     ) 2>&1 | sed -e "s@^@serve:   @" > ${status_output}
 219   ) &
 220 else
 221   # Run the supplied command on all repos in parallel.
 222 
 223   # n is the number of subprocess started or which might still be running.
 224   n=0
 225   if [ $have_fifos = "true" ]; then
 226     # if we have fifos use them to detect command completion.
 227     mkfifo ${tmp}/fifo
 228     exec 3<>${tmp}/fifo
 229     if [ "${sflag}" = "true" ] ; then
 230       # force sequential
 231       at_a_time=1
 232     fi
 233   fi
 234 
 235   for i in ${repos} ${repos_extra} ; do
 236     n=`expr ${n} '+' 1`
 237     repopidfile=`echo ${i} | sed -e 's@./@@' -e 's@/@_@g'`
 238     reponame=`echo ${i} | sed -e :a -e 's/^.\{1,20\}$/ &/;ta'`
 239     pull_base="${pull_default}"
 240     for j in $repos_extra ; do
 241       if [ "$i" = "$j" ] ; then
 242           pull_base="${pull_extra}"
 243       fi
 244     done
 245     pull_base="`echo ${pull_base} | sed -e 's@[/]*$@@'`"
 246     (
 247       (
 248         if [ "${command}" = "clone" -o "${command}" = "fclone" -o "${command}" = "tclone" ] ; then
 249           pull_newrepo="${pull_base}/${i}"
 250           path="`dirname ${i}`"
 251           if [ "${path}" != "." ] ; then
 252             times=0
 253             while [ ! -d "${path}" ]   ## nested repo, ensure containing dir exists
 254             do
 255               times=`expr ${times} '+' 1`
 256               if [ `expr ${times} '%' 10` -eq 0 ] ; then
 257                 echo "${path} still not created, waiting..." > ${status_output}
 258               fi
 259               sleep 5
 260             done
 261           fi
 262           echo "hg${global_opts} clone ${pull_newrepo} ${i}" > ${status_output}
 263           (PYTHONUNBUFFERED=true hg${global_opts} clone ${pull_newrepo} ${i}; echo "$?" > ${tmp}/${repopidfile}.pid.rc ) 2>&1 &
 264         else
 265           echo "cd ${i} && hg${global_opts} ${command} ${command_args}" > ${status_output}
 266           cd ${i} && (PYTHONUNBUFFERED=true hg${global_opts} ${command} ${command_args}; echo "$?" > ${tmp}/${repopidfile}.pid.rc ) 2>&1 &
 267         fi
 268 
 269         echo $! > ${tmp}/${repopidfile}.pid
 270       ) 2>&1 | sed -e "s@^@${reponame}:   @" > ${status_output}
 271       if [ $have_fifos = "true" ]; then
 272         echo "${reponame}" >&3
 273       fi
 274     ) &
 275 
 276     if [ $have_fifos = "true" ]; then
 277       # check on count of running subprocesses and possibly wait for completion
 278       if [ ${at_a_time} -lt ${n} ] ; then
 279         # read will block until there are completed subprocesses
 280         while read repo_done; do
 281           n=`expr ${n} '-' 1`
 282           if [ ${n} -lt ${at_a_time} ] ; then
 283             # we should start more subprocesses
 284             break;
 285           fi
 286         done <&3
 287       fi
 288     else
 289       if [ "${sflag}" = "false" ] ; then
 290         # Compare completions to starts
 291         completed="`(ls -1 ${tmp}/*.pid.rc 2> /dev/null | wc -l) || echo 0`"
 292         while [ ${at_a_time} -lt `expr ${n} '-' ${completed}` ] ; do
 293           # sleep a short time to give time for something to complete
 294           sleep 1
 295           completed="`(ls -1 ${tmp}/*.pid.rc 2> /dev/null | wc -l) || echo 0`"
 296         done
 297       else
 298         # complete this task before starting another.
 299         wait
 300       fi
 301     fi
 302   done
 303 fi
 304 
 305 # Wait for all subprocesses to complete
 306 wait
 307 
 308 # Terminate with exit 0 only if all subprocesses were successful
 309 ec=0
 310 if [ -d ${tmp} ]; then
 311   for rc in ${tmp}/*.pid.rc ; do
 312     exit_code=`cat ${rc} | tr -d ' \n\r'`
 313     if [ "${exit_code}" != "0" ] ; then
 314       repo="`echo ${rc} | sed -e s@^${tmp}@@ -e 's@/*\([^/]*\)\.pid\.rc$@\1@' -e 's@_@/@g'`"
 315       echo "WARNING: ${repo} exited abnormally ($exit_code)" > ${status_output}
 316       ec=1
 317     fi
 318   done
 319 fi
 320 exit ${ec}