Save progress when interrupted

This commit is contained in:
mtkennerly 2024-04-20 20:50:10 -04:00
parent 5f6c087097
commit b4ce27a1b9
No known key found for this signature in database
GPG key ID: E764BE00BE6E6408
5 changed files with 40 additions and 1 deletions

View file

@ -6,6 +6,13 @@ mod schema;
mod steam;
mod wiki;
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};
use once_cell::sync::Lazy;
use crate::{
manifest::{Manifest, ManifestOverride},
resource::ResourceFile,
@ -14,6 +21,11 @@ use crate::{
};
pub const REPO: &str = env!("CARGO_MANIFEST_DIR");
static CANCEL: Lazy<Arc<AtomicBool>> = Lazy::new(|| Arc::new(AtomicBool::new(false)));
pub fn should_cancel() -> bool {
CANCEL.load(Ordering::Relaxed)
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
@ -90,6 +102,8 @@ impl Error {
async fn main() {
let cli = cli::parse();
signal_hook::flag::register(signal_hook::consts::SIGINT, (*CANCEL).clone()).unwrap();
let mut wiki_cache = WikiCache::load().unwrap();
let mut wiki_meta_cache = WikiMetaCache::load().unwrap();
let mut steam_cache = SteamCache::load().unwrap();

View file

@ -3,6 +3,7 @@ use std::{collections::BTreeMap, process::Command};
use crate::{
manifest::{placeholder, Os},
resource::ResourceFile,
should_cancel,
wiki::WikiCache,
Error, State, REPO,
};
@ -36,6 +37,10 @@ impl SteamCache {
});
for app_id in app_ids {
if should_cancel() {
break;
}
let latest = SteamCacheEntry::fetch_from_id(app_id)?;
self.0.insert(
app_id,

View file

@ -7,7 +7,7 @@ use wikitext_parser::{Attribute, TextPiece};
use crate::{
manifest::{placeholder, Os, Store, Tag},
resource::ResourceFile,
Error, Regularity, State,
should_cancel, Error, Regularity, State,
};
const SAVE_INTERVAL: u32 = 100;
@ -212,6 +212,10 @@ impl WikiCache {
.as_array()
.ok_or(Error::WikiData("query.categorymembers"))?
{
if should_cancel() {
break;
}
let title = page["title"]
.as_str()
.ok_or(Error::WikiData("query.categorymembers[].title"))?;
@ -277,6 +281,10 @@ impl WikiCache {
});
for title in &titles {
if should_cancel() {
break;
}
let cached = self.0.get(title).cloned().unwrap_or_default();
println!("Wiki: {}", title);