Add two bins using crate cron_tab.
This commit is contained in:
		
							parent
							
								
									43979f201b
								
							
						
					
					
						commit
						c0a285af36
					
				
					 2 changed files with 65 additions and 0 deletions
				
			
		
							
								
								
									
										36
									
								
								src/bin/using-crate-cron_tab-async.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								src/bin/using-crate-cron_tab-async.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,36 @@
 | 
				
			||||||
 | 
					use std::sync::Arc;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use chrono::{FixedOffset, Local, TimeZone};
 | 
				
			||||||
 | 
					use cron_tab::AsyncCron;
 | 
				
			||||||
 | 
					use tokio::sync::Mutex;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[tokio::main]
 | 
				
			||||||
 | 
					async fn main() {
 | 
				
			||||||
 | 
					    let local_tz = Local::from_offset(&FixedOffset::east(7));
 | 
				
			||||||
 | 
					    let mut cron = AsyncCron::new(local_tz);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let first_job_id = cron.add_fn("* * * * * *", print_now).await;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    cron.start().await;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let counter = Arc::new(Mutex::new(1));
 | 
				
			||||||
 | 
					    cron.add_fn("* * * * * *", move || {
 | 
				
			||||||
 | 
					        let counter = counter.clone();
 | 
				
			||||||
 | 
					        async move {
 | 
				
			||||||
 | 
					            let mut counter = counter.lock().await;
 | 
				
			||||||
 | 
					            *counter += 1;
 | 
				
			||||||
 | 
					            let now = Local::now().to_string();
 | 
				
			||||||
 | 
					            println!("{} counter value: {}", now, counter);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    })
 | 
				
			||||||
 | 
					    .await;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    std::thread::sleep(std::time::Duration::from_secs(10));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // stop cron
 | 
				
			||||||
 | 
					    cron.stop();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					async fn print_now() {
 | 
				
			||||||
 | 
					    println!("now: {}", Local::now().to_string());
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										29
									
								
								src/bin/using-crate-cron_tab-sync.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								src/bin/using-crate-cron_tab-sync.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,29 @@
 | 
				
			||||||
 | 
					use chrono::{FixedOffset, Local, TimeZone};
 | 
				
			||||||
 | 
					use cron_tab;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn main() {
 | 
				
			||||||
 | 
					    let local_tz = Local::from_offset(&FixedOffset::east(7));
 | 
				
			||||||
 | 
					    let mut cron = cron_tab::Cron::new(local_tz);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let first_job_id = cron.add_fn("* * * * * * *", print_now).unwrap();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // start cron in background
 | 
				
			||||||
 | 
					    cron.start();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    cron.add_fn("* * * * * *", move || {
 | 
				
			||||||
 | 
					        println!("add_fn {}", Local::now().to_string());
 | 
				
			||||||
 | 
					    })
 | 
				
			||||||
 | 
					    .unwrap();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // remove job_test
 | 
				
			||||||
 | 
					    cron.remove(first_job_id);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    std::thread::sleep(std::time::Duration::from_secs(10));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // stop cron
 | 
				
			||||||
 | 
					    cron.stop();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn print_now() {
 | 
				
			||||||
 | 
					    println!("now: {}", Local::now().to_string());
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue