Timer Node Implementation¶
This document describes the implementation details of timer nodes in the Floxide framework.
Overview¶
Timer nodes in Floxide provide scheduling capabilities with support for various schedule types and proper error handling.
Core Components¶
TimerNode Trait¶
The TimerNode
trait defines the core interface for scheduled execution:
#[async_trait]
pub trait TimerNode<Context, Action>: Send + Sync
where
Context: Send + Sync + 'static,
Action: ActionType + Send + Sync + 'static + Default + Debug,
{
fn schedule(&self) -> Schedule;
async fn execute_on_schedule(&self, ctx: &mut Context) -> Result<Action, FloxideError>;
fn id(&self) -> NodeId;
}
Schedule Types¶
The Schedule
enum supports different scheduling patterns:
pub enum Schedule {
Once(DateTime<Utc>),
Periodic(ChronoDuration),
Cron(String),
}
Implementation Details¶
Schedule Management¶
- Schedule Types
- One-time execution
- Periodic execution
-
Cron-based scheduling
-
Time Handling
- UTC time management
- Timezone considerations
-
Leap second handling
-
Execution Control
- Start/stop capabilities
- Pause/resume support
- Graceful shutdown
Error Handling¶
- Execution Errors
- Proper error propagation
- Retry mechanisms
-
Error reporting
-
Schedule Errors
- Invalid schedule detection
- Schedule parsing errors
- Recovery strategies
Resource Management¶
- Timer Resources
- Efficient timer allocation
- Resource cleanup
-
Memory management
-
Thread Safety
- Thread-safe execution
- Safe schedule updates
- Proper synchronization
Usage Patterns¶
Basic Usage¶
let node = SimpleTimer::new(
Schedule::Periodic(ChronoDuration::minutes(5)),
|ctx| { /* execution implementation */ },
);
With Cron Schedule¶
let node = SimpleTimer::new(
Schedule::Cron("0 2 * * *".to_string()), // Run at 2 AM daily
|ctx| {
match perform_daily_task(ctx) {
Ok(_) => Ok(DefaultAction::Next),
Err(e) => Err(e.into()),
}
},
);
With Error Handling¶
let node = SimpleTimer::new(
Schedule::Once(Utc::now() + ChronoDuration::hours(1)),
|ctx| {
if let Err(e) = check_preconditions() {
return Err(e.into());
}
Ok(DefaultAction::complete())
},
);
Testing¶
The implementation includes comprehensive tests:
- Unit Tests
- Schedule parsing
- Execution timing
- Error handling
-
Resource cleanup
-
Integration Tests
- Complex schedules
- Long-running scenarios
- Edge cases
Performance Considerations¶
- Timer Efficiency
- Minimal allocations
- Efficient scheduling
-
Low overhead
-
Resource Usage
- Optimized timer pools
- Efficient thread usage
- Minimal locking
Future Improvements¶
- Enhanced Features
- More schedule types
- Advanced retry strategies
-
Schedule composition
-
Performance Optimizations
- Improved timer allocation
- Better resource sharing
- Enhanced concurrency