From 20870b644482d7b930ea57f22aa2997d0248ab03 Mon Sep 17 00:00:00 2001 From: Kienan Stewart Date: Sat, 24 Sep 2022 11:57:07 -0400 Subject: [PATCH] Own the memory for url & selector in Job structs --- src/main.rs | 47 +++++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/src/main.rs b/src/main.rs index 68fb73b..555f395 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use std::cmp::Ordering; use std::env; use std::path::Path; +use std::str::FromStr; use std::thread; use std::time::Duration; use std::time::Instant; @@ -10,15 +11,27 @@ use thirtyfour_sync::WebDriverCommands; mod conf; use conf::Conf; -struct Job<'a> { - url: &'a str, - selector: &'a str, +struct Job { + url: String, + selector: String, every: Duration, last_run: Option, } -struct ThreadJob<'a> { - job: Job<'a>, +impl Job { + fn new(url: &str, selector: &str, conf: &Conf) -> Job { + let job = Job { + url: String::from_str(url).unwrap(), + selector: String::from_str(selector).unwrap(), + every: conf.check_interval, + last_run: None, + }; + return job; + } +} + +struct ThreadJob { + job: Job, handle: Option< std::thread::JoinHandle< Result @@ -27,7 +40,7 @@ struct ThreadJob<'a> { last_result: Option, } -impl ThreadJob<'_> { +impl ThreadJob { fn lru_not_running(&self, b: &ThreadJob) -> Option { // the "greater" value is one that is running, but we // don't recheck the thread state every time. If there's @@ -87,18 +100,12 @@ fn main() { } let mut jobs = Vec::new(); - let some_job = Job { - url: "https://www.rust-lang.org", - selector: "a.download-link", - every: Duration::new(60, 0), - last_run: None, - }; - let other_job = Job { - url: "https://arstechnica.com/", - selector: "li.split-feature:nth-child(1) > header:nth-child(4) > h2:nth-child(1) > a:nth-child(1)", - every: Duration::new(120, 0), - last_run: None, - }; + let some_job = Job::new("https://www.rust-lang.org", "a.download-link", &conf); + let other_job = Job::new( + "https://arstechnica.com/", + "li.split-feature:nth-child(1) > header:nth-child(4) > h2:nth-child(1) > a:nth-child(1)", + &conf + ); jobs.push(ThreadJob { job: some_job, handle: None, @@ -125,7 +132,7 @@ fn main() { continue; } let fragment = scraper::Html::parse_document(source.unwrap().unwrap().as_str()); - let selector = scraper::Selector::parse(tj.job.selector).expect("Failed to parse selector"); + let selector = scraper::Selector::parse(&tj.job.selector.as_str()).expect("Failed to parse selector"); let mut result = String::from(""); for element in fragment.select(&selector) { result.push_str(element.inner_html().as_str()); @@ -160,7 +167,7 @@ fn main() { let driver = conf.driver_url.clone(); let url = tj.job.url.clone(); tj.handle = Some(thread::spawn(move || { - return get_source(driver.as_str(), url); + return get_source(driver.as_str(), url.as_str()); })); println!("Started thread for '{}'", tj.job.url); tj.job.last_run = Some(Instant::now());