Add bin using crate cron_job.
This commit is contained in:
		
							parent
							
								
									acbfacea7d
								
							
						
					
					
						commit
						4c9568a9dc
					
				
					 1 changed files with 32 additions and 0 deletions
				
			
		
							
								
								
									
										32
									
								
								src/bin/using-crate-cron_job.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								src/bin/using-crate-cron_job.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,32 @@
 | 
				
			||||||
 | 
					use cron_job::{CronJob, Job};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn main() {
 | 
				
			||||||
 | 
					    // Create HelloJob
 | 
				
			||||||
 | 
					    let hello_job = HelloJob {
 | 
				
			||||||
 | 
					        name: "John".into(),
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					    // Create CronJob
 | 
				
			||||||
 | 
					    let mut cron = CronJob::default();
 | 
				
			||||||
 | 
					    // Run function every second
 | 
				
			||||||
 | 
					    cron.new_job("* * * * * *", run_every_second);
 | 
				
			||||||
 | 
					    // Say hello every second
 | 
				
			||||||
 | 
					    cron.new_job("* * * * * *", hello_job);
 | 
				
			||||||
 | 
					    // Start jobs
 | 
				
			||||||
 | 
					    cron.start().expect("Failed start jobs.");
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					// The function to be executed every second.
 | 
				
			||||||
 | 
					fn run_every_second() {
 | 
				
			||||||
 | 
					    println!("1 second");
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// The job to be executed
 | 
				
			||||||
 | 
					struct HelloJob {
 | 
				
			||||||
 | 
					    name: String,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Very important, implement the Job trait and its functions.
 | 
				
			||||||
 | 
					impl Job for HelloJob {
 | 
				
			||||||
 | 
					    fn run(&mut self) {
 | 
				
			||||||
 | 
					        println!("Hello, {}!", self.name);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue