From 6d77138ebd026419c00ec5e1827ee3be5042b88d Mon Sep 17 00:00:00 2001 From: CJ_Clippy Date: Fri, 27 Jun 2025 22:54:40 -0800 Subject: [PATCH] improve parser --- services/scout/package.json | 2 +- services/scout/src/index.js | 10 +++++++++- services/scout/watchdog.sh | 17 +++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 services/scout/watchdog.sh diff --git a/services/scout/package.json b/services/scout/package.json index 413bc57..f0533c5 100644 --- a/services/scout/package.json +++ b/services/scout/package.json @@ -5,7 +5,7 @@ "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", - "start": "node src/index.js" + "start": "bash -x watchdog.sh" }, "keywords": [], "author": "", diff --git a/services/scout/src/index.js b/services/scout/src/index.js index 05f958d..d9146c5 100644 --- a/services/scout/src/index.js +++ b/services/scout/src/index.js @@ -4,9 +4,17 @@ const fs = require('fs/promises'); const logger = require('./logger.js'); function parseEvent(evt) { - if (evt?.action) { + try { + evt = JSON.parse(evt) + } catch (e) { + logger.error(`failed to parse event=${evt}`) + } + if (typeof evt?.action === "number") { + logger.debug(`event with action=${evt.action}`); if (evt.action === 0) return false; // heartbeat if (evt.action === 15) return true; // chat message + } else { + logger.warn(`event with NO ACTION. evt=${JSON.stringify(evt)} ${typeof evt}`) } } diff --git a/services/scout/watchdog.sh b/services/scout/watchdog.sh new file mode 100644 index 0000000..f27d507 --- /dev/null +++ b/services/scout/watchdog.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +set -e + +while true; do + echo "Starting app..." + node src/index.js + + exit_code=$? + if [ $exit_code -eq 0 ]; then + echo "Exited normally. Not restarting." + break + fi + + echo "App crashed (exit code $exit_code). Restarting in 2 seconds..." + sleep 2 +done