summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xconsumer.sh58
-rw-r--r--producer.sh17
2 files changed, 47 insertions, 28 deletions
diff --git a/consumer.sh b/consumer.sh
index 4ee3d0c..1fa4894 100755
--- a/consumer.sh
+++ b/consumer.sh
@@ -1,24 +1,33 @@
#!/bin/sh
-REMOTE_HOST="${QUEUE_REMOTE_HOST:-lex-queue}"
-REMOTE_FIFO="${QUEUE_REMOTE_FIFO:-/tmp/job_queue}"
-TARGET_DIR="${QUEUE_TARGET_DIR:-/home/sadeep/lex}"
-
-# Ensure the FIFO exists
-ssh -n "$REMOTE_HOST" "
- fuser -k \"$REMOTE_FIFO\" 2>/dev/null
- test -p \"$REMOTE_FIFO\" || mkfifo -m 0600 \"$REMOTE_FIFO\"
-" || {
- echo "[$(date '+%H:%M:%S')] ERROR: Failed to reach remote host or prepare FIFO" >&2
+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 FIFO '$REMOTE_FIFO' on $REMOTE_HOST..."
+echo "[$(date '+%H:%M:%S')] Listening to remote queue '$REMOTE_FIFO' on $REMOTE_HOST..."
while true; do
- msg=$(ssh -n "$REMOTE_HOST" "head -n 1 \"$REMOTE_FIFO\"")
+ 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
+ ")
- [ -z "$msg" ] && continue
+ if [ -z "$msg" ]; then
+ sleep 1
+ continue
+ fi
# Log formatted line capped at 72 chars
log_line="[$(date '+%H:%M:%S')] Processing message: ${msg}"
@@ -28,19 +37,34 @@ while true; do
echo "$log_line"
fi
- first_word=$(echo "$msg" | awk '{print $1}')
+ # 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 first word."
+ 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"
- # 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
+ # 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
index e05dca4..4e9ec41 100644
--- a/producer.sh
+++ b/producer.sh
@@ -1,6 +1,6 @@
#!/bin/sh
-FIFO_PATH="${QUEUE_FIFO_PATH:-/tmp/job_queue}"
+FIFO_PATH="${QUEUE_PATH:-/tmp/job_queue.txt}"
# Validate argument
if [ $# -eq 0 ] || [ -z "$1" ]; then
@@ -11,18 +11,13 @@ fi
MESSAGE="$1"
# Ensure the FIFO exists (create if missing)
-if [ ! -p "$FIFO_PATH" ]; then
- mkfifo -m 0600 "$FIFO_PATH" || {
- echo "[$(date '+%H:%M:%S')] ERROR: Failed to create FIFO at $FIFO_PATH" >&2
- exit 1
- }
-fi
-
-# Handle SIGPIPE gracefully so the producer doesn't crash if the consumer disconnects
-trap '' PIPE
+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
+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