#!/usr/bin/env bash

WATCHDOG_LOG='/var/log/mysql_watchdog.log'
SECONDS_TO_WAIT=300
SECONDS=0

function logmessage {
  printf '%s\n' " $(date +%c): ${1}" >> "${WATCHDOG_LOG}"
  printf '\n' >> "${WATCHDOG_LOG}"
}

# NFS is a prerequisite for downstream refresh processes and should always be running
if [ "$(systemctl is-active nfs)" = 'active' ]; then
  logmessage 'NFS is running. Nothing to do.'
else
  logmessage 'NFS is not running. Restarting...'
  timeout 60 systemctl restart nfs
  if [ "$(systemctl is-active nfs)" = 'active' ]; then
    logmessage 'NFS is now running. Restart successful.'
  else
    logmessage 'NFS is still not running. Restart was not successful.'
  fi
fi

# Check if refresh (or other manual) process is running
while [ ${SECONDS} -lt ${SECONDS_TO_WAIT} ]; do 
  if ss -t | grep -iq ssh; then
    logmessage "SSH connection(s) found, so a refresh process or manual session is active. Sleeping for 10 seconds."
    sleep 5
  else
    logmessage "No SSH connection(s) found, so a refresh is not currently running."
    break 2
  fi
done

if [ "$(systemctl is-active mysqld)" = 'active' ]; then
  logmessage 'MySQL is running. Nothing to do.'
else
  logmessage 'MySQL is not running. Restarting...'
  timeout 60 systemctl restart mysqld
  if [ "$(systemctl is-active mysqld)" = 'active' ]; then
    logmessage 'MySQL is now running. Restart successful.'
  else
    logmessage 'MySQL is still not running. Restart was not successful.'
    exit 1
  fi
fi
