wait_activities 1.27 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#!/bin/sh
# Small wait script based on watch_activities scripts.

# The goal is keep running until the activities.


function show_help ( )
{
  script_name=`basename $0`
  echo """
  Usage:
    $script_name <mysql_opt> [interval seconds] [timeout in seconds]
  Interval is default 5.
  Timeout default is 600 seconds.
  mysql_opt are default mysql command line options.
  Put them in quotes if more than one option is passed.

  Typical usage:
    $script_name erp5
    $script_name \"-h remotehost -u user erp5remote\" 3
  """
}

MYSQL_OPT=$1
INTERVAL=$2
TIMEOUT=$3
27 28 29 30 31
MYSQL_BIN=$MYSQL

if [ "$MYSQL_BIN" == "" ] ; then
  MYSQL_BIN='mysql'
fi
32 33 34 35 36 37 38 39 40 41

if [ "$MYSQL_OPT" == "" ] ; then
  show_help
  exit 1
fi

if [ "$INTERVAL" == "" ] ; then
  INTERVAL=5
fi
if [ "$TIMEOUT" == "" ] ; then
Łukasz Nowak's avatar
Łukasz Nowak committed
42
  TIMEOUT=600
43 44 45 46 47 48
fi

TIME=0

while true
do
49 50
    MESSAGE_VALUE=`echo "SELECT count(*) AS message_count FROM message;" | $MYSQL_BIN $MYSQL_OPT | grep -v message`
    MESSAGE_QUEUE_VALUE=`echo "SELECT count(*) AS message_count FROM message_queue;" | $MYSQL_BIN $MYSQL_OPT | grep -v message`
Łukasz Nowak's avatar
Łukasz Nowak committed
51 52
    if [ "$MESSAGE_VALUE" == "0" -a "$MESSAGE_QUEUE_VALUE" == 0 ] ; then
      exit 0
53 54 55 56 57 58 59 60 61
    fi
    sleep $INTERVAL;
    TIME="`expr $TIME + $INTERVAL`"
    if [ $TIME -gt $TIMEOUT ]
    then
        echo "Timeout"
        exit 1
    fi
done