background updating works
This commit is contained in:
parent
f357316797
commit
af79f6d2a3
2 changed files with 17 additions and 8 deletions
|
@ -7,7 +7,7 @@ use actix_web::{
|
||||||
use reqwest::{Client, Url, UrlError};
|
use reqwest::{Client, Url, UrlError};
|
||||||
|
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use std::sync::{Arc, MutexGuard, RwLock, RwLockWriteGuard};
|
use std::sync::{Arc, RwLock};
|
||||||
use tera::{Context, Tera};
|
use tera::{Context, Tera};
|
||||||
|
|
||||||
#[derive(Clone, Serialize, Default, Debug)]
|
#[derive(Clone, Serialize, Default, Debug)]
|
||||||
|
@ -45,13 +45,21 @@ pub fn json_endpoint(state: Data<State>) -> HttpResponse {
|
||||||
HttpResponse::Ok().json(&state.statuses)
|
HttpResponse::Ok().json(&state.statuses)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_state(mut state: RwLockWriteGuard<QueryResults>) -> RwLockWriteGuard<QueryResults> {
|
pub fn update_state(state: State) {
|
||||||
if EpochTimestamp::now() - state.last_update >= state.refresh_time {
|
let mut new_timestamp = None;
|
||||||
state.last_update = EpochTimestamp::now();
|
let mut new_statuses = None;
|
||||||
state.statuses = update_status(&state.config);
|
{
|
||||||
|
let read_state = state.read().unwrap();
|
||||||
|
if EpochTimestamp::now() - read_state.last_update >= read_state.refresh_time {
|
||||||
|
new_timestamp = Some(EpochTimestamp::now());
|
||||||
|
new_statuses = Some(update_status(&read_state.config));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if new_timestamp.is_some() {
|
||||||
|
let mut write_state = state.try_write().expect("Could not unlock");
|
||||||
|
write_state.last_update = new_timestamp.unwrap();
|
||||||
|
write_state.statuses = new_statuses.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
state
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_status(config: &Config) -> Vec<Status> {
|
fn update_status(config: &Config) -> Vec<Status> {
|
||||||
|
|
|
@ -62,7 +62,8 @@ fn main() {
|
||||||
tokio::spawn(
|
tokio::spawn(
|
||||||
Interval::new_interval(Duration::from_millis(5000))
|
Interval::new_interval(Duration::from_millis(5000))
|
||||||
.for_each(move |_| {
|
.for_each(move |_| {
|
||||||
let _ = update_state(clone_state.write().unwrap());
|
let state = Arc::clone(&clone_state);
|
||||||
|
update_state(state);
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
.map_err(|_| ()),
|
.map_err(|_| ()),
|
||||||
|
|
Loading…
Reference in a new issue