From e6bcc2276b8c06190229159ff990b1a0dd2b267d Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Sun, 2 Aug 2026 21:16:34 +0800 Subject: Rename to pub/sub. --- consumer.sh | 70 ------------------------------------------------------------- producer.sh | 31 --------------------------- pub.sh | 31 +++++++++++++++++++++++++++ sub.sh | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 101 insertions(+), 101 deletions(-) delete mode 100755 consumer.sh delete mode 100644 producer.sh create mode 100644 pub.sh create mode 100755 sub.sh diff --git a/consumer.sh b/consumer.sh deleted file mode 100755 index 1fa4894..0000000 --- a/consumer.sh +++ /dev/null @@ -1,70 +0,0 @@ -#!/bin/sh - -REMOTE_HOST="${FIFO_REMOTE_HOST:-lex-queue}" -REMOTE_FIFO="${FIFO_REMOTE_FIFO:-/tmp/job_queue.txt}" -TARGET_DIR="${FIFO_TARGET_DIR:-/home/sadeep/lex}" - -# Ensure the queue file exists -ssh -n "$REMOTE_HOST" "touch \"$REMOTE_FIFO\"" || { - echo "[$(date '+%H:%M:%S')] ERROR: Failed to reach remote host or init queue" >&2 - exit 1 -} - -echo "[$(date '+%H:%M:%S')] Listening to remote queue '$REMOTE_FIFO' on $REMOTE_HOST..." - -while true; do - msg=$(ssh -n "$REMOTE_HOST" " - if [ -s \"$REMOTE_FIFO\" ]; then - head -n 1 \"$REMOTE_FIFO\" - ed -s \"$REMOTE_FIFO\" <<'EOF' >/dev/null 2>&1 -1d -w -q -EOF - fi - ") - - if [ -z "$msg" ]; then - sleep 1 - continue - fi - - # 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 - - # Execute ./lex locally into RAM - output=$(./lex "$msg" 2>/dev/null) - - if [ -z "$output" ]; then - echo "[$(date '+%H:%M:%S')] ERROR: ./lex returned empty response" - continue - fi - - # Extract first word in-memory and convert to lowercase - raw_word=$(printf '%s' "$output" | awk 'NR==1{print $1; exit}') - first_word=$(printf '%s' "$raw_word" | tr '[:upper:]' '[:lower:]') - - if [ -z "$first_word" ]; then - echo "[$(date '+%H:%M:%S')] ERROR: Invalid response (no first word)." - continue - fi - - target_file="${TARGET_DIR}/${first_word}.txt" - temp_target="${TARGET_DIR}/${first_word}.tmp" - - # Stream RAM payload to remote temp file and atomically rename - if printf '%s\n' "$output" \ - | fold -s -w 72 \ - | ssh "$REMOTE_HOST" "cat > \"$temp_target\" && mv -f \"$temp_target\" \"$target_file\""; then - echo "[$(date '+%H:%M:%S')] OK: Saved to $target_file" - else - echo "[$(date '+%H:%M:%S')] ERROR: Failed processing '$first_word'" - fi - -done - diff --git a/producer.sh b/producer.sh deleted file mode 100644 index 4e9ec41..0000000 --- a/producer.sh +++ /dev/null @@ -1,31 +0,0 @@ -#!/bin/sh - -FIFO_PATH="${QUEUE_PATH:-/tmp/job_queue.txt}" - -# Validate argument -if [ $# -eq 0 ] || [ -z "$1" ]; then - echo "Usage: $0 \"message to publish\"" >&2 - exit 1 -fi - -MESSAGE="$1" - -# Ensure the FIFO exists (create if missing) -touch "$FIFO_PATH" 2>/dev/null || { - echo "[$(date '+%H:%M:%S')] ERROR: Failed to access queue file at $FIFO_PATH" >&2 - exit 1 -} - -# Publish message to the FIFO -if echo "$MESSAGE" >> "$FIFO_PATH" 2>/dev/null; then - # Format and cap log line at 72 chars - log_line="[$(date '+%H:%M:%S')] Published: ${MESSAGE}" - if [ ${#log_line} -gt 72 ]; then - printf "[%s] Published: %.45s...\n" "$(date '+%H:%M:%S')" "$MESSAGE" - else - echo "$log_line" - fi -else - echo "[$(date '+%H:%M:%S')] ERROR: Failed to write to FIFO at $FIFO_PATH" >&2 - exit 1 -fi diff --git a/pub.sh b/pub.sh new file mode 100644 index 0000000..4e9ec41 --- /dev/null +++ b/pub.sh @@ -0,0 +1,31 @@ +#!/bin/sh + +FIFO_PATH="${QUEUE_PATH:-/tmp/job_queue.txt}" + +# Validate argument +if [ $# -eq 0 ] || [ -z "$1" ]; then + echo "Usage: $0 \"message to publish\"" >&2 + exit 1 +fi + +MESSAGE="$1" + +# Ensure the FIFO exists (create if missing) +touch "$FIFO_PATH" 2>/dev/null || { + echo "[$(date '+%H:%M:%S')] ERROR: Failed to access queue file at $FIFO_PATH" >&2 + exit 1 +} + +# Publish message to the FIFO +if echo "$MESSAGE" >> "$FIFO_PATH" 2>/dev/null; then + # Format and cap log line at 72 chars + log_line="[$(date '+%H:%M:%S')] Published: ${MESSAGE}" + if [ ${#log_line} -gt 72 ]; then + printf "[%s] Published: %.45s...\n" "$(date '+%H:%M:%S')" "$MESSAGE" + else + echo "$log_line" + fi +else + echo "[$(date '+%H:%M:%S')] ERROR: Failed to write to FIFO at $FIFO_PATH" >&2 + exit 1 +fi diff --git a/sub.sh b/sub.sh new file mode 100755 index 0000000..1fa4894 --- /dev/null +++ b/sub.sh @@ -0,0 +1,70 @@ +#!/bin/sh + +REMOTE_HOST="${FIFO_REMOTE_HOST:-lex-queue}" +REMOTE_FIFO="${FIFO_REMOTE_FIFO:-/tmp/job_queue.txt}" +TARGET_DIR="${FIFO_TARGET_DIR:-/home/sadeep/lex}" + +# Ensure the queue file exists +ssh -n "$REMOTE_HOST" "touch \"$REMOTE_FIFO\"" || { + echo "[$(date '+%H:%M:%S')] ERROR: Failed to reach remote host or init queue" >&2 + exit 1 +} + +echo "[$(date '+%H:%M:%S')] Listening to remote queue '$REMOTE_FIFO' on $REMOTE_HOST..." + +while true; do + msg=$(ssh -n "$REMOTE_HOST" " + if [ -s \"$REMOTE_FIFO\" ]; then + head -n 1 \"$REMOTE_FIFO\" + ed -s \"$REMOTE_FIFO\" <<'EOF' >/dev/null 2>&1 +1d +w +q +EOF + fi + ") + + if [ -z "$msg" ]; then + sleep 1 + continue + fi + + # 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 + + # Execute ./lex locally into RAM + output=$(./lex "$msg" 2>/dev/null) + + if [ -z "$output" ]; then + echo "[$(date '+%H:%M:%S')] ERROR: ./lex returned empty response" + continue + fi + + # Extract first word in-memory and convert to lowercase + raw_word=$(printf '%s' "$output" | awk 'NR==1{print $1; exit}') + first_word=$(printf '%s' "$raw_word" | tr '[:upper:]' '[:lower:]') + + if [ -z "$first_word" ]; then + echo "[$(date '+%H:%M:%S')] ERROR: Invalid response (no first word)." + continue + fi + + target_file="${TARGET_DIR}/${first_word}.txt" + temp_target="${TARGET_DIR}/${first_word}.tmp" + + # Stream RAM payload to remote temp file and atomically rename + if printf '%s\n' "$output" \ + | fold -s -w 72 \ + | ssh "$REMOTE_HOST" "cat > \"$temp_target\" && mv -f \"$temp_target\" \"$target_file\""; 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