-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscheduler_example.rs
More file actions
63 lines (52 loc) · 1.89 KB
/
scheduler_example.rs
File metadata and controls
63 lines (52 loc) · 1.89 KB
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
//! Scheduler example - Periodic task execution
//!
//! This example demonstrates how to use the scheduler for periodic operations.
use multigit::daemon::scheduler::{Schedule, Scheduler};
use std::sync::{Arc, Mutex};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
println!("MultiGit Scheduler Example\n");
// 1. Parse schedule from string
let schedule = Schedule::from_duration_str("5s")?;
println!(
"✓ Created schedule: every {} seconds",
schedule.interval_seconds()
);
// 2. Create scheduler
let scheduler = Scheduler::new(schedule.interval_seconds());
println!("✓ Scheduler created\n");
// 3. Create shared counter to track executions
let counter = Arc::new(Mutex::new(0));
let counter_clone = counter.clone();
// 4. Get scheduler handle for stopping
let handle = scheduler.stop_handle();
// 5. Spawn a task to stop scheduler after 12 seconds
let stop_handle = handle.clone();
tokio::spawn(async move {
tokio::time::sleep(tokio::time::Duration::from_secs(12)).await;
println!("\n⏹ Stopping scheduler...");
stop_handle.stop();
});
// 6. Start scheduler with task
println!("▶️ Starting scheduler (will run for 12 seconds)...\n");
scheduler
.start(move || {
let counter = counter_clone.clone();
async move {
let mut count = counter.lock().unwrap();
*count += 1;
println!(
" Tick #{}: Task executed at {:?}",
*count,
std::time::SystemTime::now()
);
Ok(())
}
})
.await?;
// 7. Show results
let final_count = *counter.lock().unwrap();
println!("\n✅ Scheduler stopped");
println!(" Total executions: {}", final_count);
Ok(())
}