Stream update badges during content index validation

Publish the fingerprinted inventory before the validating save pass so checks start while validation continues, and keep streamed badges through the final save without duplicate scans. Deduplicate running scans by exact inventory, guard retired workers from overwriting successors, and cancel scoped checks on forget/reset. Rename the long phase to Validating content files.
This commit is contained in:
objz committed 2026-10-02 11:58:27 +02:00
1 parent ce0bb34401
commit 65edd0b31b
7 files changed
+555 -53

No files matched your search

+1 -1
View File
@@ -1529,4 +1529,4 @@ fn planned_install(
#[cfg(test)]
#[path = "../tests/content/dependencies.rs"]
mod tests;
pub(crate) mod tests;
+39 -16
View File
@@ -20,6 +20,7 @@ pub struct ReconcileResult {
pub instance_name: String,
pub instance_created: chrono::DateTime<chrono::Utc>,
pub manifest: ContentManifest,
pub complete: bool,
pub error: Option<String>,
}
@@ -118,14 +119,15 @@ async fn reconcile_worker() {
};
let instance = (job.instance.name.clone(), job.instance.created);
let rerun_job = job.clone();
let result = reconcile(job, &task).await;
if let Ok(mut results) = PENDING_RECONCILIATIONS.lock() {
results.retain(|pending| {
(pending.instance_name.as_str(), pending.instance_created)
!= (instance.0.as_str(), instance.1)
});
results.push(result);
}
let result = reconcile(
job.instance,
job.instances_dir,
job.client,
&task,
publish_reconciliation,
)
.await;
publish_reconciliation(result);
if let Ok(mut coordinator) = RECONCILE_COORDINATOR.lock() {
if coordinator.rerun.remove(&instance) {
coordinator.queue.push_back(rerun_job);
@@ -133,16 +135,27 @@ async fn reconcile_worker() {
coordinator.scheduled.remove(&instance);
}
}
}
}
pub(crate) fn publish_reconciliation(result: ReconcileResult) {
if let Ok(mut results) = PENDING_RECONCILIATIONS.lock() {
results.retain(|pending| {
pending.instance_name != result.instance_name
|| pending.instance_created != result.instance_created
});
results.push(result);
crate::feedback::request_redraw();
}
}
async fn reconcile(job: ReconcileJob, task: &ProgressTask) -> ReconcileResult {
let ReconcileJob {
instance,
instances_dir,
client,
} = job;
pub(crate) async fn reconcile(
instance: InstanceConfig,
instances_dir: PathBuf,
client: crate::net::HttpClient,
task: &ProgressTask,
mut publish: impl FnMut(ReconcileResult),
) -> ReconcileResult {
let instance_name = instance.name;
let instance_created = instance.created;
task.set_action("Checking content");
@@ -187,9 +200,16 @@ async fn reconcile(job: ReconcileJob, task: &ProgressTask) -> ReconcileResult {
)
.await
};
task.set_action("Saving content index");
task.set_sub_action("Validating content files");
task.set_action("Validating content files");
task.set_sub_action("Rechecking local files");
task.set_progress(0, inventory.manifest.files.len() as u64);
publish(ReconcileResult {
instance_name: instance_name.clone(),
instance_created,
manifest: inventory.manifest.clone(),
complete: false,
error: None,
});
let save_progress = task.handle();
let saved = tokio::task::spawn_blocking(move || {
save_reconciled_manifest(
@@ -216,6 +236,7 @@ async fn reconcile(job: ReconcileJob, task: &ProgressTask) -> ReconcileResult {
instance_name,
instance_created,
manifest: saved.unwrap_or_default(),
complete: true,
error,
}
}
@@ -223,12 +244,14 @@ async fn reconcile(job: ReconcileJob, task: &ProgressTask) -> ReconcileResult {
instance_name,
instance_created,
manifest: ContentManifest::default(),
complete: true,
error: Some(error.to_string()),
},
Err(error) => ReconcileResult {
instance_name,
instance_created,
manifest: ContentManifest::default(),
complete: true,
error: Some(error.to_string()),
},
}
+122 -12
View File
@@ -83,6 +83,24 @@ pub struct BulkUpdatePlan {
pub static PENDING_UPDATE_SNAPSHOTS: LazyLock<Arc<Mutex<Vec<PendingUpdateSnapshot>>>> =
LazyLock::new(|| Arc::new(Mutex::new(Vec::new())));
struct RunningCheck {
instance: InstanceConfig,
inventory: Vec<ProviderProject>,
task: tokio::task::JoinHandle<()>,
}
impl RunningCheck {
fn matches(&self, instance: &InstanceConfig, manifest: &ContentManifest) -> bool {
self.instance.created == instance.created
&& self.instance.game_version == instance.game_version
&& self.instance.loader == instance.loader
&& self.inventory == resolved_inventory(manifest)
}
}
static RUNNING_CHECKS: LazyLock<Mutex<HashMap<std::path::PathBuf, RunningCheck>>> =
LazyLock::new(|| Mutex::new(HashMap::new()));
impl UpdateSnapshot {
pub fn load(path: &std::path::Path) -> Option<Self> {
std::fs::read(path)
@@ -124,29 +142,121 @@ pub fn spawn(
manifest: ContentManifest,
path: std::path::PathBuf,
priority: Vec<ProviderProject>,
previous: Option<UpdateSnapshot>,
) {
tokio::spawn(async move {
let previous =
UpdateSnapshot::load(&path).filter(|previous| previous.applies_to(&instance));
let registry = Arc::new(super::provider::ProviderRegistry::configured(
spawn_with_registry(
instance,
manifest,
path,
priority,
previous,
Arc::new(super::provider::ProviderRegistry::configured(
crate::net::HttpClient::new(),
));
)),
);
}
pub(crate) fn is_running(
instance: &InstanceConfig,
manifest: &ContentManifest,
path: &std::path::Path,
) -> bool {
RUNNING_CHECKS.lock().is_ok_and(|checks| {
checks
.get(path)
.is_some_and(|check| !check.task.is_finished() && check.matches(instance, manifest))
})
}
pub(crate) fn cancel(path: Option<&std::path::Path>) {
if let Ok(mut checks) = RUNNING_CHECKS.lock() {
checks.retain(|check_path, check| {
if path.is_none_or(|path| path == check_path) {
check.task.abort();
if let Ok(mut pending) = PENDING_UPDATE_SNAPSHOTS.lock() {
pending.retain(|pending| {
pending.instance_name != check.instance.name
|| pending.instance_created != check.instance.created
});
}
false
} else {
true
}
});
}
}
pub(crate) fn spawn_with_registry(
instance: InstanceConfig,
manifest: ContentManifest,
path: std::path::PathBuf,
priority: Vec<ProviderProject>,
previous: Option<UpdateSnapshot>,
registry: Arc<super::provider::ProviderRegistry>,
) -> bool {
let Ok(mut checks) = RUNNING_CHECKS.lock() else {
return false;
};
if let Some(check) = checks.get(&path) {
if !check.task.is_finished() && check.matches(&instance, &manifest) {
return false;
}
check.task.abort();
}
let check_path = path.clone();
let check_instance = instance.clone();
let inventory = resolved_inventory(&manifest);
let task = tokio::spawn(async move {
let previous = previous
.or_else(|| UpdateSnapshot::load(&path))
.filter(|previous| previous.applies_to(&instance));
let snapshot = scan_with_registry(
&instance,
&manifest,
&priority,
registry,
previous.as_ref(),
|snapshot| publish_snapshot(&instance, snapshot.clone()),
|snapshot| publish_current_snapshot(&instance, snapshot.clone(), &path, false),
)
.await;
if let Ok(bytes) = serde_json::to_vec_pretty(&snapshot)
&& let Err(error) = crate::storage::write_atomic(&path, &bytes)
{
tracing::debug!("Could not cache content update snapshot: {error}");
}
publish_snapshot(&instance, snapshot);
publish_current_snapshot(&instance, snapshot, &path, true);
});
checks.insert(
check_path,
RunningCheck {
instance: check_instance,
inventory,
task,
},
);
true
}
fn publish_current_snapshot(
instance: &InstanceConfig,
snapshot: UpdateSnapshot,
path: &std::path::Path,
complete: bool,
) {
let Ok(checks) = RUNNING_CHECKS.lock() else {
return;
};
if checks
.get(path)
.is_none_or(|check| check.task.id() != tokio::task::id())
{
return;
}
// Hold the check registry through persistence and publication so a replaced
// worker cannot overwrite its successor's snapshot.
if complete
&& let Ok(bytes) = serde_json::to_vec_pretty(&snapshot)
&& let Err(error) = crate::storage::write_atomic(path, &bytes)
{
tracing::debug!("Could not cache content update snapshot: {error}");
}
publish_snapshot(instance, snapshot);
}
fn publish_snapshot(instance: &InstanceConfig, snapshot: UpdateSnapshot) {
@@ -417,6 +417,17 @@ fn provider(versions: Vec<VersionInfo>) -> FakeProvider {
}
}
pub(crate) fn controlled_update_registry(
versions: Vec<VersionInfo>,
started: tokio::sync::mpsc::UnboundedSender<String>,
pauses: HashMap<String, std::sync::Arc<tokio::sync::Semaphore>>,
) -> ProviderRegistry {
let mut provider = provider(versions);
provider.compatible_started = Some(started);
provider.compatible_pause = pauses;
provider.registry()
}
#[test]
fn update_checks_prioritize_list_rows_and_publish_before_the_rest_finish() {
use crate::instance::content::updates::{AvailableUpdate, UpdateSnapshot, scan_with_registry};