diff options
| author | Sadeep Madurange <sadeep@asciimx.com> | 2026-08-02 18:49:40 +0800 |
|---|---|---|
| committer | Sadeep Madurange <sadeep@asciimx.com> | 2026-08-02 18:49:40 +0800 |
| commit | 37561a0a33e7d24ecfb1e87dca5e897ff2fd82a6 (patch) | |
| tree | fb2a553e1ea52d74d525726b6384b4d4cdca9b76 /consumer.sh | |
| parent | e0f92ff24f34d7f8c0227e03d1e17b41447e542a (diff) | |
| download | lex-37561a0a33e7d24ecfb1e87dca5e897ff2fd82a6.tar.gz | |
Define consumer.
Diffstat (limited to 'consumer.sh')
| -rwxr-xr-x | consumer.sh | 41 |
1 files changed, 41 insertions, 0 deletions
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 |
