add basic logging

This commit is contained in:
xenofem 2022-04-16 00:03:09 -04:00
parent 69e44b9cbe
commit 7a3fd3f2ca
4 changed files with 54 additions and 1 deletions

View file

@ -2,6 +2,7 @@ use std::time::{Duration, Instant, SystemTime};
use futures::{sink::SinkExt, TryStreamExt};
use lazy_static::lazy_static;
use log::info;
use reqwest::Url;
use scraper::Selector;
use time::PrimitiveDateTime;
@ -50,6 +51,8 @@ impl PdfFetcher {
}
pub async fn fetch(&mut self) -> Result<pdf::file::File<Vec<u8>>, Error> {
info!("Fetching data PDF");
let cache_modtime = match tokio::fs::File::open(CACHED_PDF_PATH).await {
Ok(file) => Some(
file.metadata()
@ -65,6 +68,7 @@ impl PdfFetcher {
if let Some(instant) = self.last_checked {
if now - instant < *MIN_CHECK_INTERVAL {
return if cache_modtime.is_some() {
info!("Already checked origin recently, not rechecking");
self.cached_pdf()
} else {
Err(Error::TooSoon)
@ -111,6 +115,8 @@ impl PdfFetcher {
.map_or(true, |(cache, origin)| origin > cache);
if outdated {
info!("Cached PDF is outdated, downloading latest version");
let mut pdf_stream = self
.client
.get(pdf_url)
@ -124,6 +130,8 @@ impl PdfFetcher {
sink.send_all(&mut pdf_stream).await?;
<dyn futures::Sink<bytes::Bytes, Error = std::io::Error> + Unpin>::close(&mut sink)
.await?;
} else {
info!("Cached PDF is already up to date");
}
self.last_checked = Some(now);
self.cached_pdf()