#!/bin/bash
# =============================================================================
# Runner Watchdog 配置腳本
# =============================================================================
# 用途: 配置 systemd watchdog，Runner 卡住 5 分鐘自動重啟
# 執行: ssh 192.168.0.110 "bash -s" < scripts/setup-runner-watchdog.sh
# =============================================================================

set -e

SERVICE_NAME="actions.runner.owenhytsai-awoooi.awoooi-110"
OVERRIDE_DIR="/etc/systemd/system/${SERVICE_NAME}.service.d"

echo "=== Runner Watchdog 配置 ==="
echo "Service: ${SERVICE_NAME}"
echo ""

# 創建 override 目錄
sudo mkdir -p "${OVERRIDE_DIR}"

# 創建 watchdog 配置
sudo tee "${OVERRIDE_DIR}/watchdog.conf" <<'EOF'
[Service]
# Phase 16: Runner 穩定性改善
# 2026-03-26 統帥裁示: 徹底解決卡住問題

# Runner 卡住 5 分鐘自動重啟
WatchdogSec=300

# 永遠重啟
Restart=always
RestartSec=10

# 重啟次數限制 (1小時內最多重啟5次，防止無限重啟迴圈)
StartLimitIntervalSec=3600
StartLimitBurst=5
EOF

# 重載 systemd
sudo systemctl daemon-reload

# 驗證配置
echo ""
echo "=== 驗證配置 ==="
systemctl show "${SERVICE_NAME}" | grep -E "(WatchdogUSec|Restart=|RestartUSec)"

echo ""
echo "✅ Watchdog 配置完成！"
echo ""
echo "📝 注意: 配置已載入，但 Runner 未重啟 (避免中斷當前 job)"
echo "   如需立即生效: sudo systemctl restart ${SERVICE_NAME}"
