Load jobs from job files in job directory

This commit is contained in:
Kienan Stewart 2022-09-25 17:50:01 -04:00
parent 82bd7e2d5c
commit da4e79463f
1 changed files with 45 additions and 21 deletions

View File

@ -82,23 +82,51 @@ fn main() {
std::process::exit(1); std::process::exit(1);
} }
let mut jobs = Vec::new(); let mut jobs: Vec<ThreadJob> = Vec::new();
let some_job = Job::new("https://www.rust-lang.org", "a.download-link", &conf);
let other_job = Job::new( // Load all jobs from job directory
"https://arstechnica.com/", let job_dir = conf.job_dir.clone();
"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)", for entry in std::fs::read_dir(job_dir).expect("Failed to iterate over job directory") {
&conf let _entry = match entry {
); Err(why) => {
jobs.push(ThreadJob { println!("Skipping '{}': Error reading file in job directory", why);
job: some_job, continue;
handle: None, },
last_result: None, Ok(value) => value,
}); };
jobs.push(ThreadJob { let md = _entry.metadata().expect("Failed to read file metadata");
job: other_job, if !md.is_file() {
handle: None, println!("Skipping '{}': not a file", _entry.path().display());
last_result: None, 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; let max_running_tasks = 5;
loop { loop {
@ -164,10 +192,6 @@ fn main() {
break; 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; break;
} }