Skip to content

Commit 19e5267

Browse files
committed
fixed logging
1 parent 5ab314b commit 19e5267

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

src/cache/menu_cache.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::{
77
use chrono::{DateTime, Utc};
88
use firestore::FirestoreDb;
99
use futures::{stream::FuturesUnordered, StreamExt};
10+
use log::info;
1011
use tokio::io::AsyncReadExt;
1112

1213
const CACHES_COLLECTION: &str = "caches";
@@ -23,6 +24,8 @@ struct InDbMenuCache {
2324
data: Vec<u8>,
2425
}
2526

27+
pub static REFRESH_INTERVAL: chrono::Duration = chrono::Duration::minutes(15);
28+
2629
impl<'a> MenuCache<'a> {
2730
async fn from_async(cache: InDbMenuCache) -> Self {
2831
if cache.data.is_empty() {
@@ -31,10 +34,15 @@ impl<'a> MenuCache<'a> {
3134
locations: Locations::default(),
3235
};
3336
}
34-
let mut uncompressed =
37+
let mut decompress =
3538
async_compression::tokio::bufread::GzipDecoder::new(cache.data.as_slice());
39+
info!("Size of data compressed: {}", cache.data.len());
3640
let mut dst = String::with_capacity(cache.data.len() * 8);
37-
uncompressed.read_to_string(&mut dst).await;
41+
let _len = decompress
42+
.read_to_string(&mut dst)
43+
.await
44+
.expect("should succeed");
45+
info!("Size of data uncompressed: {}", dst.len());
3846
let locations: Locations =
3947
serde_json::from_str(&dst).expect("Data parse should always be valid");
4048
MenuCache {
@@ -59,14 +67,22 @@ impl<'a> MenuCache<'a> {
5967
}
6068

6169
pub async fn maybe_refresh(&mut self) -> Result<bool, Error> {
62-
if Utc::now().signed_duration_since(self.cached_at) > chrono::Duration::minutes(15) {
70+
if self.get_time_since_refresh() > chrono::Duration::minutes(15) {
6371
self.refresh().await?;
6472
Ok(true)
6573
} else {
6674
Ok(false)
6775
}
6876
}
6977

78+
pub fn get_time_since_refresh(&self) -> chrono::Duration {
79+
Utc::now().signed_duration_since(self.cached_at)
80+
}
81+
82+
pub fn get_time_until_refresh(&self) -> chrono::Duration {
83+
REFRESH_INTERVAL - self.get_time_since_refresh()
84+
}
85+
7086
async fn fetch_from_db() -> Result<Self, crate::error::Error> {
7187
let db = FirestoreDb::new("ucsc-menu").await?;
7288
let cache: InDbMenuCache = db
@@ -158,6 +174,7 @@ mod tests {
158174

159175
#[tokio::test]
160176
async fn test_open() {
177+
pretty_env_logger::init();
161178
let _mc = MenuCache::open().await.unwrap();
162179
}
163180

src/cache/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod menu_cache;
22
mod multithreaded_cache;
33

4+
pub use menu_cache::REFRESH_INTERVAL;
45
pub use multithreaded_cache::MultithreadedCache as Multithreaded;

src/main.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ mod fetch;
1111
mod parse;
1212
mod transpose;
1313

14-
use std::{env, net::SocketAddr, str::FromStr, sync::Arc, time::Duration};
14+
use std::{
15+
env,
16+
net::SocketAddr,
17+
str::FromStr,
18+
sync::Arc,
19+
time::{Duration, Instant},
20+
};
1521

1622
use axum::{
1723
body::Body,
@@ -63,9 +69,14 @@ async fn refresh<'a>() -> Response {
6369
.get_or_init(|| async { Multithreaded::new().await.unwrap() })
6470
.await;
6571
let _res = cache.refresh().await;
72+
let c = cache.get().await;
6673
Response::builder()
6774
.status(201)
68-
.body(Body::from("OK"))
75+
.body(Body::from(format!(
76+
"Last refresh: {}\nNext refresh: {}",
77+
c.get_time_since_refresh(),
78+
c.get_time_until_refresh(),
79+
)))
6980
.unwrap()
7081
}
7182

@@ -83,7 +94,7 @@ async fn main() {
8394
.deflate(true)
8495
.gzip(true)
8596
.zstd(true);
86-
pretty_env_logger::init_custom_env("ucsc_menu=info");
97+
pretty_env_logger::init();
8798

8899
let app = Router::new()
89100
.route(
@@ -105,12 +116,19 @@ async fn main() {
105116
tokio::spawn(async move {
106117
let client = make_client();
107118
log::info!("Forcing refresh");
119+
let start = Instant::now();
108120
let _res = client
109121
.put(format!("http://{addr}/request-refresh"))
110122
.send()
111123
.await;
112-
log::info!("Forcing refresh done");
113-
sleep(Duration::from_secs(15 * 60)).await;
124+
log::info!("Forcing refresh done, took {:?}", start.elapsed());
125+
sleep(Duration::from_secs(
126+
cache::REFRESH_INTERVAL
127+
.num_seconds()
128+
.try_into()
129+
.expect("refresh interval to be positive"),
130+
))
131+
.await;
114132
});
115133
let listener = TcpListener::bind(addr)
116134
.await

0 commit comments

Comments
 (0)