From da4e79463f1b16afd52f6caa61ea00f94df39732 Mon Sep 17 00:00:00 2001 From: Kienan Stewart Date: Sun, 25 Sep 2022 17:50:01 -0400 Subject: [PATCH] Load jobs from job files in job directory --- src/main.rs | 66 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 21 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1114318..e5ebd8f 100644 --- a/src/main.rs +++ b/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 = 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; }