diff --git a/prod-run.sh b/prod-run.sh index 8cac05f..6f0f2b7 100755 --- a/prod-run.sh +++ b/prod-run.sh @@ -13,6 +13,7 @@ SYSTEMD_STDOUT_LOG="$LOG_DIR/runewager-1.log" SERVICE_FILE="/etc/systemd/system/runewager.service" LOGROTATE_FILE="/etc/logrotate.d/runewager" CRON_LINE='0 3 * * * /usr/sbin/logrotate -f /etc/logrotate.d/runewager' +HEALTH_LOG_MAX_AGE_MINUTES=10 say() { printf '[runewager] %s\n' "$*" @@ -45,6 +46,55 @@ ensure_log_paths() { fi } + +get_bot_pid() { + pgrep -f "node .*index\.js" | head -n 1 || true +} + +log_age_seconds() { + local file="$1" + if [ ! -f "$file" ]; then + echo -1 + return + fi + local now mtime + now="$(date +%s)" + mtime="$(stat -c %Y "$file" 2>/dev/null || echo 0)" + echo $((now - mtime)) +} + +print_health_summary() { + local pid + pid="$(get_bot_pid)" + + if [ -n "$pid" ]; then + say "✅ Bot is running (pid: $pid)" + else + err "❌ Bot is not running" + return + fi + + local active_log="$BOT_LOG" + if [ ! -s "$active_log" ] && [ -s "$LEGACY_BOT_LOG" ]; then + active_log="$LEGACY_BOT_LOG" + fi + + local age + age="$(log_age_seconds "$active_log")" + if [ -s "$active_log" ] && [ "$age" -ge 0 ] && [ "$age" -le $((HEALTH_LOG_MAX_AGE_MINUTES * 60)) ]; then + say "✅ Health check passed: recent logs are present (${age}s old) at $active_log" + return + fi + + if [ -s "$active_log" ] && [ "$age" -gt $((HEALTH_LOG_MAX_AGE_MINUTES * 60)) ]; then + warn "Bot process is running but logs are stale (${age}s old) at $active_log" + else + warn "Bot process is running but no recent logs were found" + fi + + warn "If /start is unresponsive, restart with: pkill -f 'node .*index\.js' && bash $PROJECT_DIR/prod-run.sh" +} + fix_working_directory() { if [ "$(pwd)" != "$PROJECT_DIR" ]; then say "Switching working directory to $PROJECT_DIR" @@ -265,6 +315,13 @@ fix_require_and_import_hints() { } ensure_logrotate_config() { + local log_owner="runewager" + local log_group="runewager" + if ! id -u "$log_owner" >/dev/null 2>&1; then + log_owner="$(id -un 2>/dev/null || echo root)" + log_group="$(id -gn 2>/dev/null || echo root)" + fi + local conf conf=$(cat </dev/null 2>&1; then - if ! logrotate -d "$LOGROTATE_FILE" >/dev/null 2>&1; then + local logrotate_output='' + if ! logrotate_output="$(logrotate -d "$LOGROTATE_FILE" 2>&1)"; then warn "logrotate dry-run failed for $LOGROTATE_FILE" + warn "$logrotate_output" else say "logrotate config validation passed" fi @@ -319,6 +378,14 @@ ensure_cron_fallback() { } ensure_systemd_service() { + local service_user="runewager" + local service_group="runewager" + if ! id -u "$service_user" >/dev/null 2>&1; then + service_user="$(id -un 2>/dev/null || echo root)" + service_group="$(id -gn 2>/dev/null || echo root)" + warn "runewager user missing; using $service_user:$service_group for systemd service" + fi + local conf conf=$(cat </dev/null 2>&1 + [ -n "$(get_bot_pid)" ] } check_bot_status_twice() { - if is_bot_running; then - say "Bot running" - else - say "Bot NOT running" - fi + print_health_summary sleep 15 - if is_bot_running; then - say "Bot running" - else - say "Bot NOT running" - fi + print_health_summary } start_background_if_needed() { @@ -482,10 +543,10 @@ main() { ensure_cron_fallback ensure_systemd_service - print_human_errors check_bot_status_twice start_background_if_needed check_bot_status_twice + print_human_errors } main "$@"