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

11
Cargo.lock generated
View file

@ -928,6 +928,7 @@ dependencies = [
"serde",
"serde_json",
"serde_yaml",
"signal-hook",
"thiserror",
"tokio",
"wikitext-parser",
@ -1600,6 +1601,16 @@ dependencies = [
"digest",
]
[[package]]
name = "signal-hook"
version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8621587d4798caf8eb44879d42e56b9a93ea5dcd315a6487c357130095b62801"
dependencies = [
"libc",
"signal-hook-registry",
]
[[package]]
name = "signal-hook-registry"
version = "1.4.1"

View file

@ -19,6 +19,7 @@ regex = "1.10.2"
serde = { version = "1.0.139", features = ["derive"] }
serde_json = "1.0.108"
serde_yaml = "0.8.25"
signal-hook = "0.3.17"
thiserror = "1.0.50"
tokio = { version = "1.34.0", features = ["full"] }
wikitext-parser = "0.3.3"

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);