53 lines
2.1 KiB
Markdown
53 lines
2.1 KiB
Markdown
# Architecture of the `rhailib_worker` Crate
|
|
|
|
The `rhailib_worker` crate implements a distributed task execution system for Rhai scripts, providing scalable, reliable script processing through Redis-based task queues. Workers are decoupled from contexts, allowing a single worker to process tasks for multiple contexts (circles).
|
|
|
|
## Core Architecture
|
|
|
|
```mermaid
|
|
graph TD
|
|
A[Worker Process] --> B[Task Queue Processing]
|
|
A --> C[Script Execution Engine]
|
|
A --> D[Result Management]
|
|
|
|
B --> B1[Redis Queue Monitoring]
|
|
B --> B2[Task Deserialization]
|
|
B --> B3[Priority Handling]
|
|
|
|
C --> C1[Rhai Engine Integration]
|
|
C --> C2[Context Management]
|
|
C --> C3[Error Handling]
|
|
|
|
D --> D1[Result Serialization]
|
|
D --> D2[Reply Queue Management]
|
|
D --> D3[Status Updates]
|
|
```
|
|
|
|
## Key Components
|
|
|
|
### Task Processing Pipeline
|
|
- **Queue Monitoring**: Continuous Redis queue polling for new tasks
|
|
- **Task Execution**: Secure Rhai script execution with proper context
|
|
- **Result Handling**: Comprehensive result and error management
|
|
|
|
### Engine Integration
|
|
- **Rhailib Engine**: Full integration with rhailib_engine for DSL access
|
|
- **Context Injection**: Proper authentication and database context setup
|
|
- **Security**: Isolated execution environment with access controls
|
|
|
|
### Scalability Features
|
|
- **Horizontal Scaling**: Multiple worker instances for load distribution
|
|
- **Queue-based Architecture**: Reliable task distribution via Redis
|
|
- **Fault Tolerance**: Robust error handling and recovery mechanisms
|
|
|
|
## Dependencies
|
|
|
|
- **Redis Integration**: Task queue management and communication
|
|
- **Rhai Engine**: Script execution with full DSL capabilities
|
|
- **Client Integration**: Shared data structures with rhai_dispatcher
|
|
- **Heromodels**: Database and business logic integration
|
|
- **Async Runtime**: Tokio for high-performance concurrent processing
|
|
|
|
## Deployment Patterns
|
|
|
|
Workers can be deployed as standalone processes, containerized services, or embedded components, providing flexibility for various deployment scenarios from development to production. |