.. | ||
osis | ||
system | ||
osis_config.toml | ||
osis_worker_demo.rs | ||
README.md | ||
system_config.toml | ||
system_worker_demo.rs | ||
trait_based_worker_demo.rs |
Actor Examples
This directory contains example configurations and test scripts for both OSIS and System actor binaries.
Overview
Both examples demonstrate the ping/pong functionality built into the Hero actors:
- Actors automatically detect jobs with script content "ping"
- They respond immediately with "pong" without executing the Rhai engine
- This provides a fast health check and connectivity test mechanism
Prerequisites
-
Redis Server: Both examples require a running Redis server
# Install Redis (macOS) brew install redis # Start Redis server redis-server
-
Rust Environment: Make sure you can build the actor binaries
cd /path/to/herocode/hero/core/actor cargo build --bin osis --bin system
OSIS Actor Example
Location: examples/osis/
The OSIS (Operating System Integration Service) actor processes jobs synchronously, one at a time.
Files
config.toml
- Configuration for the OSIS actorexample.sh
- Test script that demonstrates ping/pong functionality
Usage
cd examples/osis
./example.sh
What the script does:
- Checks Redis connectivity
- Cleans up any existing jobs
- Starts the OSIS actor in the background
- Sends 3 ping jobs sequentially
- Verifies each job receives a "pong" response
- Reports success/failure statistics
- Cleans up actor and Redis data
Expected Output
=== OSIS Actor Example ===
✅ Redis is running
✅ OSIS actor started (PID: 12345)
📤 Sending ping job: ping_job_1_1234567890
✅ Job ping_job_1_1234567890 completed successfully with result: pong
...
🎉 All tests passed! OSIS actor is working correctly.
System Actor Example
Location: examples/system/
The System actor processes jobs asynchronously, handling multiple jobs concurrently.
Files
config.toml
- Configuration for the System actor (includes async settings)example.sh
- Test script that demonstrates concurrent ping/pong functionality
Usage
cd examples/system
./example.sh
What the script does:
- Checks Redis connectivity
- Cleans up any existing jobs
- Starts the System actor with stats reporting
- Sends 5 concurrent ping jobs
- Sends 10 rapid-fire ping jobs to test async capabilities
- Verifies all jobs receive "pong" responses
- Reports comprehensive success/failure statistics
- Cleans up actor and Redis data
Expected Output
=== System Actor Example ===
✅ Redis is running
✅ System actor started (PID: 12345)
📤 Sending ping job: ping_job_1_1234567890123
✅ Job ping_job_1_1234567890123 completed successfully with result: pong
...
🎉 All tests passed! System actor is handling concurrent jobs correctly.
Overall success rate: 15/15
Configuration Details
OSIS Configuration (examples/osis/config.toml
)
actor_id = "osis_example_actor"
redis_url = "redis://localhost:6379"
db_path = "/tmp/osis_example_db"
preserve_tasks = false
[actor_type]
type = "sync"
[logging]
timestamps = true
level = "info"
System Configuration (examples/system/config.toml
)
actor_id = "system_example_actor"
redis_url = "redis://localhost:6379"
db_path = "/tmp/system_example_db"
preserve_tasks = false
[actor_type]
type = "async"
default_timeout_seconds = 30
[logging]
timestamps = true
level = "info"
Key Differences
Feature | OSIS Actor | System Actor |
---|---|---|
Processing | Sequential (one job at a time) | Concurrent (multiple jobs simultaneously) |
Use Case | System-level operations requiring resource management | High-throughput job processing |
Timeout | No timeout configuration | Configurable job timeouts |
Stats | Basic logging | Optional statistics reporting (--show-stats ) |
Job Handling | Blocking job execution | Non-blocking async job execution |
Troubleshooting
Redis Connection Issues
# Check if Redis is running
redis-cli ping
# Check Redis logs
redis-server --loglevel verbose
Actor Compilation Issues
# Clean and rebuild
cargo clean
cargo build --bin osis --bin system
Job Processing Issues
- Check Redis for stuck jobs:
redis-cli keys "hero:*"
- Clear all Hero jobs:
redis-cli eval "return redis.call('del', unpack(redis.call('keys', 'hero:*')))" 0
- Check actor logs for detailed error messages
Extending the Examples
Adding Custom Jobs
To test with custom Rhai scripts instead of ping jobs:
-
Modify the job creation in the shell scripts:
# Replace "ping" with your Rhai script redis-cli -u "$REDIS_URL" hset "hero:job:$job_id" \ script "your_rhai_script_here"
-
Update result verification to expect your script's output instead of "pong"
Testing Different Configurations
- Modify
config.toml
files to test different Redis URLs, database paths, or logging levels - Test with
preserve_tasks = true
to inspect job details after completion - Adjust timeout values in the System actor configuration
Architecture Notes
Both examples demonstrate the unified Actor trait architecture:
- Common Interface: Both actors implement the same
Actor
trait - Ping/Pong Handling: Built into the trait's
spawn
method before job delegation - Redis Integration: Uses the shared Job struct from
hero_job
crate - Configuration: TOML-based configuration with CLI overrides
- Graceful Shutdown: Both actors handle SIGTERM/SIGINT properly
This architecture allows for easy extension with new actor types while maintaining consistent behavior and configuration patterns.