✨ cron
This commit is contained in:
parent
18c663fcdb
commit
46cb21dc2a
8 changed files with 575 additions and 6 deletions
55
README.md
55
README.md
|
@ -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.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue