cronline 0.2.1

Lightweight Task Scheduling for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# Cronline Real-World Examples

This document provides an overview of the comprehensive real-world examples included with Cronline. Each example demonstrates practical applications and best practices for using Cronline in production scenarios.

## Running the Examples

All examples can be run using:

```bash
cargo run --example <example_name>
```

For example:
```bash
cargo run --example web_integration_axum
cargo run --example monitoring_alerting
```

---

## 1. Web Framework Integration (Axum)

**File:** `examples/web_integration_axum.rs`

**What it demonstrates:**
- Integrating Cronline with the Axum web framework
- Running a scheduler alongside a web server
- Health check tasks running in the background
- Scheduled database maintenance
- Metrics collection for web applications
- Event-driven architecture with the event bus

**Use cases:**
- Web services that need background jobs
- API servers with periodic tasks
- Microservices with scheduled maintenance
- SaaS applications with recurring operations

**Key features shown:**
```rust
// Health check every 30 seconds
Task::new(|| async { /* health check */ })
    .with_interval(Duration::from_secs(30))
    .with_tags(&["monitoring", "health", "critical"])

// Database cleanup every 5 minutes
Task::new(|| async { /* cleanup */ })
    .with_schedule("*/5 * * * *")?
    .with_tags(&["database", "maintenance"])

// Subscribe to events
let mut events = scheduler.event_bus().subscribe();
```

**Topics covered:**
- Application state management
- Task configuration with timeouts and retries
- Event subscription and handling
- Tag-based task organization
- Integration patterns with web frameworks

---

## 2. Monitoring & Alerting System

**File:** `examples/monitoring_alerting.rs`

**What it demonstrates:**
- System resource monitoring (CPU, memory, disk)
- Threshold-based alerting with different severity levels
- Service health checks
- Alert rate limiting and deduplication
- Multi-metric monitoring

**Use cases:**
- Infrastructure monitoring
- Application performance monitoring (APM)
- Service level monitoring
- Automated incident detection
- Proactive alerting systems

**Key features shown:**
```rust
// CPU monitoring with threshold alerts
if cpu > 80.0 {
    alert_mgr.send_alert(Alert {
        severity: AlertSeverity::Critical,
        title: "High CPU Usage".to_string(),
        // ...
    });
}

// Service health checks
Task::new(|| async {
    check_multiple_services().await
})
.with_interval(Duration::from_secs(30))
.with_tags(&["monitoring", "health", "services"])
```

**Topics covered:**
- Real-time metrics collection
- Alert management and prioritization
- Rate limiting for notifications
- Multi-service health checking
- Historical tracking and analysis

---

## 3. Database Maintenance

**File:** `examples/database_maintenance.rs`

**What it demonstrates:**
- Scheduled database backups (full and incremental)
- Index rebuilding and optimization
- Data archival and purging strategies
- VACUUM operations
- Statistics updates
- Backup retention policies

**Use cases:**
- PostgreSQL/MySQL maintenance
- Automated backup systems
- Data lifecycle management
- Database performance optimization
- Compliance-driven data retention

**Key features shown:**
```rust
// Full backup daily at 2 AM
Task::new(|| async { perform_full_backup().await })
    .with_schedule("0 2 * * *")?
    .with_tags(&["backup", "database", "critical"])
    .with_config(TaskConfig {
        timeout: Some(Duration::from_secs(3600)),
        max_retries: 2,
        // ...
    })

// Purge old logs daily
Task::new(|| async { purge_old_data(90).await })
    .with_schedule("0 5 * * *")?
```

**Topics covered:**
- Backup strategies (full vs incremental)
- Data archival patterns
- Database optimization techniques
- Cleanup and maintenance scheduling
- Error handling and retries for critical tasks

---

## 4. API Polling & Web Scraping

**File:** `examples/api_polling_scraping.rs`

**What it demonstrates:**
- Periodic API polling for data updates
- Rate-limited web scraping
- Change detection and alerting
- External service integration
- RSS feed aggregation
- Product price tracking

**Use cases:**
- Price comparison services
- Stock market data aggregation
- Weather data collection
- News feed aggregation
- Third-party API integration
- Competitive intelligence gathering

**Key features shown:**
```rust
// Stock price monitoring with change detection
Task::new(|| async {
    let new_price = fetch_stock_price("AAPL").await?;
    if price_changed_significantly(new_price) {
        send_alert().await;
    }
    Ok(())
})
.with_interval(Duration::from_secs(60))
.with_tags(&["api", "stocks", "finance"])

// Rate-limited scraping
if rate_limiter.can_make_request("api").await {
    fetch_data().await;
}
```

**Topics covered:**
- API rate limiting strategies
- Change detection algorithms
- Data deduplication
- Multi-source data aggregation
- Respectful web scraping practices

---

## 5. Notification System

**File:** `examples/notification_system.rs`

**What it demonstrates:**
- Scheduled email campaigns
- Reminder notifications
- Daily/weekly digest emails
- Event-triggered notifications
- Multi-channel notifications (Email, SMS, Push, Webhook)
- Notification queue processing
- Priority-based delivery

**Use cases:**
- Email marketing platforms
- Transactional email services
- User notification systems
- Automated reminder services
- Multi-channel messaging platforms

**Key features shown:**
```rust
// Daily digest at 8 AM
Task::new(|| async {
    send_digest(user, daily_summary).await
})
.with_schedule("0 8 * * *")?
.with_tags(&["email", "digest", "daily"])

// Process notification queue with priority
Task::new(|| async {
    queue.process_queue(max_batch).await
})
.with_interval(Duration::from_secs(30))
.with_tags(&["queue", "notification", "processing"])

// Weekly marketing campaign
Task::new(|| async {
    send_bulk_email(recipients, campaign).await
})
.with_schedule("0 10 * * 4")?  // Thursday 10 AM
```

**Topics covered:**
- Email delivery patterns
- Queue management
- Priority-based processing
- Bulk sending strategies
- Multi-channel notification routing
- Campaign scheduling

---

## 6. Cache Warming & Management

**File:** `examples/cache_warming.rs`

**What it demonstrates:**
- Proactive cache warming before traffic spikes
- Multi-tier cache management (L1, L2, CDN)
- Cache invalidation strategies
- Cache hit rate monitoring
- Predictive cache preloading
- Automated cleanup of expired entries

**Use cases:**
- E-commerce platforms
- Content delivery networks
- High-traffic web applications
- API caching layers
- Search result caching

**Key features shown:**
```rust
// Morning cache warmup before traffic spike
Task::new(|| async {
    warm_popular_items().await
})
.with_schedule("0 7 * * *")?
.with_tags(&["cache", "warmup", "morning"])

// Pre-sale cache warmup
Task::new(|| async {
    warm_sale_items().await
})
.with_schedule("0 23 * * 5")?  // Friday 11 PM

// Monitor cache hit rates
Task::new(|| async {
    let hit_rate = calculate_hit_rate().await;
    if hit_rate < threshold {
        alert_low_hit_rate().await;
    }
    Ok(())
})
.with_interval(Duration::from_secs(300))
```

**Topics covered:**
- Multi-tier cache architecture
- Predictive warming strategies
- Cache invalidation patterns
- Performance monitoring
- Memory management
- Traffic pattern optimization

---

## Common Patterns Across Examples

### 1. Event Bus Usage
All examples demonstrate subscribing to scheduler events for monitoring and debugging:

```rust
let mut events = scheduler.event_bus().subscribe();
tokio::spawn(async move {
    while let Ok(event) = events.recv().await {
        match event {
            SchedulerEvent::TaskCompleted { .. } => { /* log */ },
            SchedulerEvent::TaskFailed { .. } => { /* alert */ },
            _ => {}
        }
    }
});
```

### 2. Tag-based Organization
Tasks are organized using tags for filtering and management:

```rust
// Add tasks with tags
task.with_tags(&["backup", "critical", "database"])

// Filter tasks by tags
let critical_tasks = scheduler.tasks_with_tag("critical").await;
let backup_tasks = scheduler.tasks_with_all_tags(&["backup", "database"]).await;
```

### 3. Error Handling & Retries
Production-ready error handling with configurable retries:

```rust
.with_config(TaskConfig {
    timeout: Some(Duration::from_secs(60)),
    max_retries: 3,
    retry_delay: Duration::from_secs(5),
    fail_scheduler_on_error: false,
})
```

### 4. Interval vs Cron Scheduling
Examples show when to use each approach:

```rust
// Use intervals for simple periodic tasks
.with_interval(Duration::from_secs(300))

// Use cron for specific times
.with_schedule("0 2 * * *")?  // 2 AM daily
.with_schedule("0 9 * * 1")?  // Monday 9 AM
```

---

## Best Practices Demonstrated

1. **Separation of Concerns**: Each task has a single, well-defined responsibility
2. **Resource Management**: Proper cleanup and resource pooling
3. **Graceful Degradation**: Tasks fail gracefully without crashing the scheduler
4. **Observability**: Comprehensive logging and event emission
5. **Configuration**: Externalized configuration for timeouts and retries
6. **Testing**: Examples include simulation of real-world scenarios
7. **Documentation**: Clear comments explaining the why, not just the what

---

## Performance Considerations

Examples demonstrate:
- Efficient batch processing
- Rate limiting to prevent API abuse
- Connection pooling for databases
- Lazy loading and caching strategies
- Memory-conscious data structures
- Async I/O for concurrent operations

---

## Security Considerations

Examples show:
- Safe handling of credentials (not hardcoded)
- Rate limiting to prevent abuse
- Input validation patterns
- Safe error message handling (no sensitive data leakage)

---

## Next Steps

After reviewing these examples:

1. **Start Simple**: Begin with `basic_scheduler.rs` and `enhanced_features.rs`
2. **Pick Your Use Case**: Choose the example closest to your needs
3. **Customize**: Adapt the patterns to your specific requirements
4. **Test**: Use the event bus to monitor task execution
5. **Deploy**: Follow production best practices from the examples

## Additional Resources

- [Main README]README.md - Getting started guide
- [API Documentation]https://docs.rs/cronline - Complete API reference
- [GitHub Issues]https://github.com/TickTockBent/Cronline/issues - Report bugs or request features

---

**Note**: All examples are self-contained and can run independently. They use simulated operations (no real databases, APIs, etc.) to demonstrate concepts without external dependencies.