baobab/core/worker/examples/system/example.sh
2025-08-05 12:19:38 +02:00

184 lines
5.3 KiB
Bash
Executable File

#!/bin/bash
# System Worker Example Script
# This script demonstrates the System worker by:
# 1. Starting the worker with the config.toml
# 2. Sending multiple concurrent ping jobs to Redis
# 3. Verifying pong responses
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CONFIG_FILE="$SCRIPT_DIR/config.toml"
WORKER_ID="system_example_worker"
REDIS_URL="redis://localhost:6379"
echo "=== System Worker Example ==="
echo "Script directory: $SCRIPT_DIR"
echo "Config file: $CONFIG_FILE"
echo "Worker ID: $WORKER_ID"
echo "Redis URL: $REDIS_URL"
echo
# Check if Redis is running
echo "Checking Redis connection..."
if ! redis-cli -u "$REDIS_URL" ping > /dev/null 2>&1; then
echo "❌ Error: Redis is not running or not accessible at $REDIS_URL"
echo "Please start Redis server first: redis-server"
exit 1
fi
echo "✅ Redis is running"
echo
# Clean up any existing jobs in the queue
echo "Cleaning up existing jobs in Redis..."
redis-cli -u "$REDIS_URL" del "hero:jobs:$WORKER_ID" > /dev/null 2>&1 || true
redis-cli -u "$REDIS_URL" eval "return redis.call('del', unpack(redis.call('keys', 'hero:job:*')))" 0 > /dev/null 2>&1 || true
echo "✅ Redis queues cleaned"
echo
# Start the System worker in the background
echo "Starting System worker..."
cd "$SCRIPT_DIR/../.."
cargo run --bin system -- --config "$CONFIG_FILE" --show-stats &
WORKER_PID=$!
echo "✅ System worker started (PID: $WORKER_PID)"
echo
# Wait a moment for the worker to initialize
echo "Waiting for worker to initialize..."
sleep 3
# Function to send a ping job (non-blocking)
send_ping_job() {
local job_num=$1
local job_id="ping_job_${job_num}_$(date +%s%N)"
echo "📤 Sending ping job: $job_id"
# Create job in Redis
redis-cli -u "$REDIS_URL" hset "hero:job:$job_id" \
id "$job_id" \
script "ping" \
status "Queued" \
created_at "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
worker_id "$WORKER_ID" > /dev/null
# Add job to worker queue
redis-cli -u "$REDIS_URL" lpush "hero:jobs:$WORKER_ID" "$job_id" > /dev/null
echo "$job_id"
}
# Function to check job result
check_job_result() {
local job_id=$1
local timeout=15
local elapsed=0
while [ $elapsed -lt $timeout ]; do
local status=$(redis-cli -u "$REDIS_URL" hget "hero:job:$job_id" status 2>/dev/null || echo "")
if [ "$status" = "Finished" ]; then
local result=$(redis-cli -u "$REDIS_URL" hget "hero:job:$job_id" result 2>/dev/null || echo "")
if [ "$result" = "pong" ]; then
echo "✅ Job $job_id completed successfully with result: $result"
return 0
else
echo "❌ Job $job_id completed but with unexpected result: $result"
return 1
fi
elif [ "$status" = "Error" ]; then
local error=$(redis-cli -u "$REDIS_URL" hget "hero:job:$job_id" error 2>/dev/null || echo "")
echo "❌ Job $job_id failed with error: $error"
return 1
fi
sleep 0.5
elapsed=$((elapsed + 1))
done
echo "❌ Job $job_id timed out after ${timeout}s"
return 1
}
# Send multiple concurrent ping jobs to test async processing
echo "Testing concurrent ping/pong functionality..."
total_jobs=5
job_ids=()
echo
echo "--- Sending $total_jobs concurrent ping jobs ---"
for i in $(seq 1 $total_jobs); do
job_id=$(send_ping_job $i)
job_ids+=("$job_id")
sleep 0.1 # Small delay between job submissions
done
echo
echo "--- Waiting for all jobs to complete ---"
success_count=0
for job_id in "${job_ids[@]}"; do
echo "Checking job: $job_id"
if check_job_result "$job_id"; then
success_count=$((success_count + 1))
fi
done
echo
echo "=== Test Results ==="
echo "Successful concurrent ping/pong tests: $success_count/$total_jobs"
if [ $success_count -eq $total_jobs ]; then
echo "🎉 All tests passed! System worker is handling concurrent jobs correctly."
exit_code=0
else
echo "⚠️ Some tests failed. Check the worker logs for details."
exit_code=1
fi
# Test rapid job submission to showcase async capabilities
echo
echo "--- Testing rapid job submission (10 jobs in quick succession) ---"
rapid_jobs=10
rapid_job_ids=()
for i in $(seq 1 $rapid_jobs); do
job_id=$(send_ping_job "rapid_$i")
rapid_job_ids+=("$job_id")
done
echo "Waiting for rapid jobs to complete..."
rapid_success=0
for job_id in "${rapid_job_ids[@]}"; do
if check_job_result "$job_id"; then
rapid_success=$((rapid_success + 1))
fi
done
echo "Rapid submission test: $rapid_success/$rapid_jobs successful"
# Clean up
echo
echo "Cleaning up..."
echo "Stopping System worker (PID: $WORKER_PID)..."
kill $WORKER_PID 2>/dev/null || true
wait $WORKER_PID 2>/dev/null || true
echo "✅ Worker stopped"
echo "Cleaning up Redis jobs..."
redis-cli -u "$REDIS_URL" del "hero:jobs:$WORKER_ID" > /dev/null 2>&1 || true
redis-cli -u "$REDIS_URL" eval "return redis.call('del', unpack(redis.call('keys', 'hero:job:*')))" 0 > /dev/null 2>&1 || true
echo "✅ Redis cleaned up"
echo
echo "=== System Worker Example Complete ==="
total_success=$((success_count + rapid_success))
total_tests=$((total_jobs + rapid_jobs))
echo "Overall success rate: $total_success/$total_tests"
if [ $total_success -eq $total_tests ]; then
exit 0
else
exit 1
fi