diff options
Diffstat (limited to 'sub.sh')
| -rwxr-xr-x | sub.sh | 113 |
1 files changed, 62 insertions, 51 deletions
@@ -1,70 +1,81 @@ -#!/bin/sh +#!/bin/ksh +# OpenBSD ksh daemon to process remote spool files over SSH -REMOTE_HOST="${FIFO_REMOTE_HOST:-lex-queue}" -REMOTE_FIFO="${FIFO_REMOTE_FIFO:-/tmp/job_queue.txt}" -TARGET_DIR="${FIFO_TARGET_DIR:-/home/sadeep/lex}" +SSH_HOST="lex-spool" +SPOOL_DIR="/var/spool/remote_spool" +DICT_DIR="/var/www/var/lex/dict" +SOCKET_PATH="/var/run/my_program.sock" +SLEEP_INTERVAL=2 -# 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 +log_err() { + logger -t lexd_sub -p daemon.err "$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 + # Grab the first spool file name + LS_CMD="ls -1 '${SPOOL_DIR}' 2>/dev/null | head -n 1" + SPOOL_FILE=$(ssh -n "${SSH_HOST}" "${LS_CMD}") + if [ $? -ne 0 ]; then + log_err "Failed to reach remote server ${SSH_HOST}" + sleep "${SLEEP_INTERVAL}" 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" + # If spool is empty, wait and retry + if [ -z "${SPOOL_FILE}" ]; then + sleep "${SLEEP_INTERVAL}" + continue fi - # Execute ./lex locally into RAM - output=$(./lex "$msg" 2>/dev/null) + # Fork to mitigate resource leaks in the main process + ( + set -e - if [ -z "$output" ]; then - echo "[$(date '+%H:%M:%S')] ERROR: ./lex returned empty response" - continue - fi + # Fetch remote file into Unix socket + CAT_CMD="cat '${SPOOL_DIR}/${SPOOL_FILE}'" + response=$(ssh -n "${SSH_HOST}" "${CAT_CMD}" \ + | nc -U "${SOCKET_PATH}") + + if [ -z "${response}" ]; then + log_err "Empty socket response: ${SPOOL_FILE}" + exit 1 + 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:]') + # Extract first word (lowercased) + target_word=$(print -r -- "${response}" \ + | awk '{print tolower($1); exit}') - if [ -z "$first_word" ]; then - echo "[$(date '+%H:%M:%S')] ERROR: Invalid response (no first word)." - continue - fi + if [ -z "${target_word}" ]; then + log_err "No target filename for: ${SPOOL_FILE}" + exit 1 + fi + + # Validate target word from socket + case "${target_word}" in + *[!a-z-]*) + log_err "Invalid word '${target_word}': ${SPOOL_FILE}" + exit 1 + ;; + [!a-z]*|*[!a-z]) + log_err "Bad bounds '${target_word}': ${SPOOL_FILE}" + exit 1 + ;; + esac + + target_file="${DICT_DIR}/${target_word}" + + # Write dict file & remove spool file + WRITE_DEL_CMD="cat > '${target_file}' && \ + rm -f '${SPOOL_DIR}/${SPOOL_FILE}'" - target_file="${TARGET_DIR}/${first_word}.txt" - temp_target="${TARGET_DIR}/${first_word}.tmp" + print -r -- "${response}" \ + | ssh -n "${SSH_HOST}" "${WRITE_DEL_CMD}" + ) - # 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'" + if [ $? -ne 0 ]; then + log_err "Processing failed for file: ${SPOOL_FILE}" fi + sleep "${SLEEP_INTERVAL}" done |
