#!/bin/bash
# /root/watchdog_realtime_daemons.sh
#
# Checks each realtime_daemon.php AND realtime_stop_recorder.php instance
# via its PID file. If the PID file is missing or the process is dead,
# restarts the daemon.
# Intended to run every 5 minutes via root crontab:
#
#   */5 * * * * /root/watchdog_realtime_daemons.sh >> /var/log/realtime_watchdog.log 2>&1

PHP=/usr/local/bin/php
BASE=/home/faretrak/public_html/todis_ext
LOGDIR="$BASE/logs"
PIDDIR="$BASE/run"

REALTIME_DAEMON="$BASE/realtime_daemon.php"
STOP_RECORDER="$BASE/realtime_stop_recorder.php"

# Format: "domain_arg|pid_file|log_file"
# realtime_daemon.php accepts comma-separated domains in one process
# (43332,43349 share a feed and run as a single instance).
DAEMON_INSTANCES=(
    "38767|realtime_38767.pid|realtime_38767.log"
    "34456,43332,43349|realtime_34456_43332_43349.pid|realtime_34456_43332_43349.log"
    "90091|realtime_90091.pid|realtime_90091.log"
    "90092|realtime_90092.pid|realtime_90092.log"
    "44270|realtime_44270.pid|realtime_44270.log"
    "80081|realtime_80081.pid|realtime_80081.log"
)

# realtime_stop_recorder.php is deliberately one-process-per-domain (it
# rejects comma-separated --datadomain) — so 43332/43349 get two separate
# entries here, not one combined entry like above. 80081 has no feed, so
# no recorder instance for it — nothing for it to seed/poll.
RECORDER_INSTANCES=(
    "38767|realtime_stops_38767.pid|realtime_stops_38767.log"
    "34456|realtime_stops_34456.pid|realtime_stops_34456.log"
)

TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')

# $1 = label (for log lines), $2 = daemon script path, $3 = instances array name (by nameref)
check_and_restart() {
    local label="$1"
    local daemon_script="$2"
    local -n instances="$3"

    for entry in "${instances[@]}"; do
        IFS='|' read -r DOMAINS PIDFILE LOGFILE <<< "$entry"
        PIDPATH="$PIDDIR/$PIDFILE"
        LOGPATH="$LOGDIR/$LOGFILE"

        RUNNING=0

        if [[ -f "$PIDPATH" ]]; then
            PID=$(cat "$PIDPATH")
            if [[ -n "$PID" ]] && kill -0 "$PID" 2>/dev/null; then
                RUNNING=1
            fi
        fi

        if [[ "$RUNNING" -eq 1 ]]; then
            echo "[$TIMESTAMP] OK   [$label] domains=$DOMAINS pid=$PID"
        else
            echo "[$TIMESTAMP] DEAD [$label] domains=$DOMAINS — restarting..."
            nohup "$PHP" "$daemon_script" "--datadomain=$DOMAINS" >> "$LOGPATH" 2>&1 &
            NEWPID=$!
            echo "[$TIMESTAMP] STARTED [$label] domains=$DOMAINS pid=$NEWPID"
        fi
    done
}

check_and_restart "realtime_daemon"       "$REALTIME_DAEMON" DAEMON_INSTANCES
check_and_restart "realtime_stop_recorder" "$STOP_RECORDER"   RECORDER_INSTANCES
