rhailib/examples/run_lua_demo.sh
2025-06-12 05:21:52 +03:00

63 lines
1.8 KiB
Bash
Executable File

#!/bin/bash
# Exit immediately if a command exits with a non-zero status.
set -e
CIRCLE_NAME="default"
TASK_QUEUE_KEY="rhai_tasks:${CIRCLE_NAME}"
# --- 1. Clean Up Previous State ---
echo "Cleaning up previous Redis state..."
redis-cli DEL "$TASK_QUEUE_KEY" > /dev/null 2>&1 || true
echo "Redis state cleaned."
# --- 2. Compile and Run the Worker ---
echo "Compiling and running the 'lua_client_demo' worker in the background..."
export RUST_LOG=info
cargo run -p rhailib-examples --bin lua_client_demo &
WORKER_PID=$!
echo "Worker started with PID: $WORKER_PID"
# --- 3. Wait for the Worker to be Ready using Ping-Pong ---
echo "Waiting for worker to be ready (ping-pong check)..."
ATTEMPTS=0
MAX_ATTEMPTS=15 # Wait for a maximum of 15 seconds
while [ $ATTEMPTS -lt $MAX_ATTEMPTS ]; do
# Send a 'ping()' script and check for a 'pong' in the output.
# The timeout for the ping itself is short (2 seconds).
PING_RESULT=$(redis-cli EVAL "$(cat ../scripts/run_rhai.lua)" 0 "$CIRCLE_NAME" "ping()" 2 || true)
if echo "$PING_RESULT" | grep -q "pong"; then
echo "Worker is ready. Received pong."
break
fi
echo "Ping failed or timed out. Retrying..."
sleep 1
ATTEMPTS=$((ATTEMPTS + 1))
done
if [ $ATTEMPTS -eq $MAX_ATTEMPTS ]; then
echo "Error: Timed out waiting for worker to respond to ping."
kill $WORKER_PID
exit 1
fi
# --- 4. Execute the Actual Script ---
echo ""
echo "Executing main Rhai script..."
RESULT=$(redis-cli EVAL "$(cat ../scripts/run_rhai.lua)" 0 "$CIRCLE_NAME" "let x = 100; x * 2" 5)
echo "Result from main script: $RESULT"
# --- 5. Shutdown the Worker ---
echo ""
echo "Shutting down the worker (PID: $WORKER_PID)..."
kill $WORKER_PID
wait $WORKER_PID
echo "Worker shut down."
echo ""
if echo "$RESULT" | grep -q "error"; then
echo "Demo completed with an error."
exit 1
else
echo "Demo completed successfully."
fi