Hi everyone, I'll be as short as possible. Any help would be appreciated.
I have an 'async fn timer_updatable' which collects the data and parses the site every 60 seconds. And sometimes (maybe 2 times per month) it panics because it can't collect the final structure (I think it happens because it parses the site from html and sometimes it can't match all the elements).
I decided to launch an infinite loop, so if timer_updatable fails or panics because of some Err this loop relaunches it again. And I can't figure out how to acomplish this task. Every time I try it launches lots of 'fn timer_updatable'
pub async fn retriable_timer(updatable_link : Arc<Mutex<Vec<GroupEvent>>>) -> () {
spawn_blocking(move || {
loop {
println!("Looper is spawned");
let cloned = Arc::clone(&updatable_link);
timer_updatable(cloned); // Async fn which collects the data and parses the site
}
});
}
Infinite async fn timer_updatable which reloads the data.
pub async fn timer_updatable(updatable_link : Arc<Mutex<Vec<GroupEvent>>>) -> () {
tokio::spawn(async move {
let mut timer : u8 = 60;
loop {
if timer == 0 {
let mut processed : Vec<JoinHandle<()>> = Vec::new();
println!("Started loading the data at {}\n", Local::now().format("[%Y-%m-%d][%H:%M:%S]"));
let mut answer = ParseManage::na_collected().await; // Get main page with a check for connection. Retry till it's fine.
let copied = answer.clone();
let to_fill : Arc<Mutex<Vec<DataStructs::MapsToMatch>>> = Arc::new(Mutex::new(Vec::new()));
for links in copied {
let fill_copy = Arc::clone(&to_fill);
let spawned_thread = tokio::spawn(async move {
println!("Spawned a thread for : {}", links.link);
let retrieved = ParseManage::retrieve_maps(links.link).await;
let mut unlocked = fill_copy.lock().await;
println!("{:#?}", retrieved);
unlocked.push(retrieved);
});
processed.push(spawned_thread)
}
futures::future::join_all(processed).await;
for elements in to_fill.lock().await.iter() {
for control in answer.iter_mut() {
if elements.link.to_string() == control.link.to_string() {
control.yandex_maps = elements.map.to_string();
control.subway_colored = elements.subway.clone();
}
}
}
let mut opened_value = updatable_link.lock().await;
*opened_value = answer;
println!("{:#?}", *opened_value);
drop(opened_value);
timer = 60
}
else {
sleep(Duration::from_secs(1)).await;
timer -= 1;
}
}
});
}