summaryrefslogtreecommitdiffstats
path: root/consumer.sh
blob: 4ee3d0c113ceb369e98012fe7e522bf17d13828f (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
42
43
44
45
46
#!/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
	exit 1
}

echo "[$(date '+%H:%M:%S')] Listening to remote FIFO '$REMOTE_FIFO' on $REMOTE_HOST..."

while true; do
	msg=$(ssh -n "$REMOTE_HOST" "head -n 1 \"$REMOTE_FIFO\"")

	[ -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