From 37561a0a33e7d24ecfb1e87dca5e897ff2fd82a6 Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Sun, 2 Aug 2026 18:49:40 +0800 Subject: Define consumer. --- consumer.sh | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100755 consumer.sh (limited to 'consumer.sh') diff --git a/consumer.sh b/consumer.sh new file mode 100755 index 0000000..975f103 --- /dev/null +++ b/consumer.sh @@ -0,0 +1,41 @@ +#!/bin/sh + +REMOTE_HOST="${QUEUE_REMOTE_HOST:-lex-queue}" +REMOTE_FIFO="${QUEUE_REMOTE_FIFO:-/tmp/job_queue}" +TARGET_DIR="${QUEUE_TARGET_DIR:-/home/lex}" + +# Ensure the FIFO exists +ssh "$REMOTE_HOST" "test -p \"$REMOTE_FIFO\" || mkfifo -m 0600 \"$REMOTE_FIFO\"" || { + echo "[$(date '+%H:%M:%S')] ERROR: Failed to reach remote host or create FIFO" >&2 + exit 1 +} + +echo "[$(date '+%H:%M:%S')] Listening to remote FIFO '$REMOTE_FIFO' on $REMOTE_HOST..." + +ssh "$REMOTE_HOST" "exec 3<> \"$REMOTE_FIFO\"; cat <&3" | while read -r msg; do + [ -z "$msg" ] && continue + + # Log formatted line capped at 72 chars + log_line="[$(date '+%H:%M:%S')] Processing message: ${msg}" + if [ ${#log_line} -gt 72 ]; then + printf "[%s] Processing message: %.38s...\n" "$(date '+%H:%M:%S')" "$msg" + else + echo "$log_line" + fi + + first_word=$(echo "$msg" | awk '{print $1}') + if [ -z "$first_word" ]; then + echo "[$(date '+%H:%M:%S')] ERROR: Invalid first word." + continue + fi + + target_file="${TARGET_DIR}/${first_word}.txt" + + # Process payload locally with ./lex and stream output back over SSH + if ./lex "$msg" 2>/dev/null | fold -s -w 72 | ssh "$REMOTE_HOST" "cat > \"$target_file\"" 2>/dev/null; then + echo "[$(date '+%H:%M:%S')] OK: Saved to $target_file" + else + echo "[$(date '+%H:%M:%S')] ERROR: Failed processing '$first_word'" + fi + +done -- cgit v1.2.3