summaryrefslogtreecommitdiffstats
path: root/consumer.sh
blob: 975f103311ed82273b484a9e68a1456db3eae073 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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