#!/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