Load jobs from job files in job directory
This commit is contained in:
		
							parent
							
								
									82bd7e2d5c
								
							
						
					
					
						commit
						da4e79463f
					
				
							
								
								
									
										66
									
								
								src/main.rs
								
								
								
								
							
							
						
						
									
										66
									
								
								src/main.rs
								
								
								
								
							|  | @ -82,23 +82,51 @@ fn main() { | |||
|         std::process::exit(1); | ||||
|     } | ||||
| 
 | ||||
|     let mut jobs = Vec::new(); | ||||
|     let some_job = Job::new("https://www.rust-lang.org", "a.download-link", &conf); | ||||
|     let other_job = Job::new( | ||||
|         "https://arstechnica.com/", | ||||
|         "section.listing:nth-child(2) > ul:nth-child(1) > li:nth-child(3) > header:nth-child(3) > h2:nth-child(1) > a:nth-child(1)", | ||||
|         &conf | ||||
|     ); | ||||
|     jobs.push(ThreadJob { | ||||
|         job: some_job, | ||||
|         handle: None, | ||||
|         last_result: None, | ||||
|     }); | ||||
|     jobs.push(ThreadJob { | ||||
|         job: other_job, | ||||
|         handle: None, | ||||
|         last_result: None, | ||||
|     }); | ||||
|     let mut jobs: Vec<ThreadJob> = Vec::new(); | ||||
| 
 | ||||
|     // Load all jobs from job directory
 | ||||
|     let job_dir = conf.job_dir.clone(); | ||||
|     for entry in std::fs::read_dir(job_dir).expect("Failed to iterate over job directory") { | ||||
|         let _entry = match entry { | ||||
|             Err(why) => { | ||||
|                 println!("Skipping '{}': Error reading file in job directory", why); | ||||
|                 continue; | ||||
|             }, | ||||
|             Ok(value) => value, | ||||
|         }; | ||||
|         let md = _entry.metadata().expect("Failed to read file metadata"); | ||||
|         if !md.is_file() { | ||||
|             println!("Skipping '{}': not a file", _entry.path().display()); | ||||
|             continue; | ||||
|         } | ||||
|         match _entry.path().extension() { | ||||
|             Some(x) => { | ||||
|                 if ! "job".eq(x) { | ||||
|                     println!("Skipping '{}': does not have '.job' extension", | ||||
|                              _entry.path().display()); | ||||
|                     continue; | ||||
|                 } | ||||
|             }, | ||||
|             None => { | ||||
|                 println!("Skipping '{}': does not have '.job' extension", | ||||
|                          _entry.path().display()); | ||||
|                 continue; | ||||
|             } | ||||
|         }; | ||||
| 
 | ||||
|         let job = match Job::from_file(&_entry.path(), &conf) { | ||||
|             Err(why) => { | ||||
|                 println!("Failed to load job from '{}': {}", _entry.path().display(), why); | ||||
|                 continue; | ||||
|             }, | ||||
|             Ok(value) => value, | ||||
|         }; | ||||
|         jobs.push(ThreadJob { | ||||
|             job: job, | ||||
|             handle: None, | ||||
|             last_result: None, | ||||
|         }); | ||||
|     } | ||||
| 
 | ||||
|     let max_running_tasks = 5; | ||||
|     loop { | ||||
|  | @ -164,10 +192,6 @@ fn main() { | |||
|                         break; | ||||
|                     } | ||||
|                 } | ||||
|                 else { | ||||
|                     let time_since_last_run = Instant::now().duration_since(tj.job.last_run.unwrap()); | ||||
|                     println!("Job '{}' - {}s since last run", tj.job.url, time_since_last_run.as_secs()); | ||||
|                 } | ||||
|             } | ||||
|             break; | ||||
|         } | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue