Own the memory for url & selector in Job structs
This commit is contained in:
parent
b1dee1f5a8
commit
20870b6444
47
src/main.rs
47
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<Instant>,
|
||||
}
|
||||
|
||||
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<String, &'static str>
|
||||
|
@ -27,7 +40,7 @@ struct ThreadJob<'a> {
|
|||
last_result: Option<String>,
|
||||
}
|
||||
|
||||
impl ThreadJob<'_> {
|
||||
impl ThreadJob {
|
||||
fn lru_not_running(&self, b: &ThreadJob) -> Option<Ordering> {
|
||||
// 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());
|
||||
|
|
Loading…
Reference in New Issue