This commit is contained in:
JMARyA 2025-03-10 18:43:32 +01:00
parent 18c663fcdb
commit 46cb21dc2a
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
8 changed files with 575 additions and 6 deletions

View file

@ -28,7 +28,7 @@ let res: (i32, i32) = rally(items, |item: &i32| {
});
// Run background tasks without blocking the main thread
background(|| {
background!(|| {
// Background task logic
println!("This is a background task!");
});
@ -52,6 +52,11 @@ let value: &str = retry(|| {
None
}
})
// Delayed execution
delay(Duration::from_secs(4), || {
println!("I will run in 4 seconds from now on!");
});
```
### Service Management
@ -74,6 +79,54 @@ fn run_services() {
}
```
#### Cron Tasks
The ServiceManager also supports running functions periodically or on time:
```rust
fn main() {
let s = ServiceManager::new();
// Init Cron Manager
let cron = Cron::new();
// Add Cron Task
cron.add_task("4_sec", Schedule::Every(Duration::from_secs(4)), || {
println!("I run every 4 at {}", chrono::Utc::now());
});
cron.add_task("2_sec", Schedule::Every(Duration::from_secs(2)), || {
println!("I run every 2 seconds at {}", chrono::Utc::now());
});
cron.add_task(
"daily",
Schedule::Every(Duration::from_secs(60 * 60 * 24)),
|| {
println!("I run daily");
},
);
// Start running the Cron Manager
let (s, cron) = s.register_cron(cron.into());
let s = s.spawn();
defer!(|| {
s.join().unwrap();
});
// Add another Cron Task after running the manager dynamically
cron.add_task(
"future_task",
Schedule::At(datetime_in(Duration::from_secs(2))),
|| {
println!("I am in the future");
},
);
// Functionally the same as above
cron.run_at(datetime_in(Duration::from_secs(3)), || {
println!("The Future");
});
}
```
### Worker Unions
You can annotate a function with `#[worker]` which gives them superpowers. These functions can be queued and dispatched by the system, and their results are returned when completed.