#!/bin/sh

# Only put load balanced apps managed by TG in this var.  This script will run
# on alternate app servers to check and restart supervisor managed  apps that
# are using more than MEMLIMIT of memory.
APPS='mirrormanager packagedb smolt transifex'
MEMLIMIT=750000

for app in $APPS ; do
  # Supervisor knows the PID of the processes
  PID=`supervisorctl status $app | sed -e 's/.*pid \([0-9]\+\),.*/\1/'`
  # Ignore crashed apps or apps not present on this server
  if test `echo "$PID" | egrep '^[0-9]+$'` ; then
    # We use Resident Set Size to determine if the app is using too much memory
    RSS=`ps -eo pid,rss|grep $PID| awk '// { print $2 }'`
    if test $RSS -gt $MEMLIMIT ; then
      # Use supervisor to restart the app
      echo "Restarting $app $PID RSS: $RSS"
      supervisorctl restart $app
    fi
  fi
done
