You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Schedule background tasks with `#[vespera::cron]`. Uses [tokio-cron-scheduler](https://crates.io/crates/tokio-cron-scheduler) under the hood.
517
+
518
+
### Enable Feature
519
+
520
+
```toml
521
+
[dependencies]
522
+
vespera = { version = "0.1", features = ["cron"] }
523
+
```
524
+
525
+
### Define Cron Jobs
526
+
527
+
Place `#[vespera::cron("...")]` on any `pub async fn` with zero parameters. The function can live anywhere in your project — no special directory required.
528
+
529
+
```rust
530
+
// src/cron/cleanup.rs, src/tasks.rs, or even src/routes/users.rs — anywhere works
531
+
#[vespera::cron("1/10 * * * * *")]
532
+
pubasyncfncleanup_sessions() {
533
+
println!("Running cleanup every 10 seconds");
534
+
}
535
+
536
+
#[vespera::cron("0 0 * * * *")]
537
+
pubasyncfnhourly_report() {
538
+
println!("Running hourly report");
539
+
}
540
+
```
541
+
542
+
### How It Works
543
+
544
+
1.`#[cron("...")]` registers the job at compile time (like `#[route]`)
545
+
2.`vespera!()` auto-discovers all registered cron jobs — no extra parameters needed
546
+
3. A background scheduler spawns via `tokio::spawn` when the app starts
547
+
548
+
```rust
549
+
// No cron-specific config — just works
550
+
letapp=vespera!(docs_url="/docs");
551
+
```
552
+
553
+
### Cron Expression Format
554
+
555
+
Uses 6-field cron expressions (`sec min hour day month weekday`):
556
+
557
+
| Expression | Schedule |
558
+
|-----------|----------|
559
+
|`0 */5 * * * *`| Every 5 minutes |
560
+
|`0 0 * * * *`| Every hour |
561
+
|`0 0 0 * * *`| Daily at midnight |
562
+
|`1/10 * * * * *`| Every 10 seconds |
563
+
|`0 30 9 * * Mon-Fri`| Weekdays at 9:30 AM |
564
+
565
+
### Requirements
566
+
567
+
- Functions must be `pub async fn`
568
+
- Functions must take **no parameters** (no `State`, no extractors)
0 commit comments