139 lines
4.2 KiB
Bash
Executable File
139 lines
4.2 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# OSIS Worker Example Script
|
|
# This script demonstrates the OSIS worker by:
|
|
# 1. Starting the worker with the config.toml
|
|
# 2. Sending 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="osis_example_worker"
|
|
REDIS_URL="redis://localhost:6379"
|
|
|
|
echo "=== OSIS 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 OSIS worker in the background
|
|
echo "Starting OSIS worker..."
|
|
cd "$SCRIPT_DIR/../.."
|
|
cargo run --bin osis -- --config "$CONFIG_FILE" &
|
|
WORKER_PID=$!
|
|
echo "✅ OSIS 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 and check for pong response
|
|
send_ping_job() {
|
|
local job_num=$1
|
|
local job_id="ping_job_${job_num}_$(date +%s)"
|
|
|
|
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
|
|
|
|
# Wait for job completion and check result
|
|
local timeout=10
|
|
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 1
|
|
elapsed=$((elapsed + 1))
|
|
done
|
|
|
|
echo "❌ Job $job_id timed out after ${timeout}s"
|
|
return 1
|
|
}
|
|
|
|
# Send multiple ping jobs to test the worker
|
|
echo "Testing ping/pong functionality..."
|
|
success_count=0
|
|
total_jobs=3
|
|
|
|
for i in $(seq 1 $total_jobs); do
|
|
echo
|
|
echo "--- Test $i/$total_jobs ---"
|
|
if send_ping_job $i; then
|
|
success_count=$((success_count + 1))
|
|
fi
|
|
sleep 1
|
|
done
|
|
|
|
echo
|
|
echo "=== Test Results ==="
|
|
echo "Successful ping/pong tests: $success_count/$total_jobs"
|
|
|
|
if [ $success_count -eq $total_jobs ]; then
|
|
echo "🎉 All tests passed! OSIS worker is working correctly."
|
|
exit_code=0
|
|
else
|
|
echo "⚠️ Some tests failed. Check the worker logs for details."
|
|
exit_code=1
|
|
fi
|
|
|
|
# Clean up
|
|
echo
|
|
echo "Cleaning up..."
|
|
echo "Stopping OSIS 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 "=== OSIS Worker Example Complete ==="
|
|
exit $exit_code
|