blob: e05dca4b7c3b8228c0a28c1bf5512022dee6fa25 (
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
|
#!/bin/sh
FIFO_PATH="${QUEUE_FIFO_PATH:-/tmp/job_queue}"
# 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)
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
# 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
|