fix(content): isolate concurrent install tasks
This commit is contained in:
1 parent
95a25836a0
commit
2f982435f9
5 files changed
+117
-42
No files matched your search
@@ -1,5 +1,6 @@
|
||||
use std::collections::{HashMap, HashSet, VecDeque};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
|
||||
use crate::instance::content::provider::{ContentProvider, FingerprintQuery, ProviderRegistry};
|
||||
use crate::instance::{
|
||||
@@ -10,6 +11,8 @@ use crate::net::modrinth::{
|
||||
DependencyType, ProjectInfo, VersionDependency, VersionInfo, VersionType,
|
||||
};
|
||||
|
||||
static NEXT_INSTALL_ID: AtomicU64 = AtomicU64::new(1);
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct InstallRoot {
|
||||
pub provider: String,
|
||||
@@ -82,7 +85,6 @@ pub async fn install(
|
||||
manifest_path: &Path,
|
||||
minecraft_dir: &Path,
|
||||
plan: &DependencyPlan,
|
||||
transaction_id: u64,
|
||||
) -> Result<InstallResult, NetError> {
|
||||
let root = plan
|
||||
.items
|
||||
@@ -91,10 +93,7 @@ pub async fn install(
|
||||
for item in &plan.items {
|
||||
tokio::fs::create_dir_all(&item.destination).await?;
|
||||
}
|
||||
let staging = minecraft_dir.join(format!(".rmcl-install-{transaction_id}"));
|
||||
if staging.exists() {
|
||||
tokio::fs::remove_dir_all(&staging).await?;
|
||||
}
|
||||
let staging = staging_directory(minecraft_dir);
|
||||
tokio::fs::create_dir(&staging).await?;
|
||||
|
||||
let result = install_staged(registry, manifest_path, minecraft_dir, &staging, plan).await;
|
||||
@@ -114,6 +113,13 @@ pub async fn install(
|
||||
})
|
||||
}
|
||||
|
||||
fn staging_directory(minecraft_dir: &Path) -> PathBuf {
|
||||
minecraft_dir.join(format!(
|
||||
".rmcl-install-{}",
|
||||
NEXT_INSTALL_ID.fetch_add(1, Ordering::Relaxed)
|
||||
))
|
||||
}
|
||||
|
||||
struct StagedFile {
|
||||
item: usize,
|
||||
source: PathBuf,
|
||||
|
||||
@@ -11,6 +11,12 @@ use crate::net::modrinth::{
|
||||
DependencyType, DiscoveryResults, ProjectInfo, VersionDependency, VersionType,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn concurrent_installs_use_distinct_staging_directories() {
|
||||
let minecraft = Path::new("/instance/minecraft");
|
||||
assert_ne!(staging_directory(minecraft), staging_directory(minecraft));
|
||||
}
|
||||
|
||||
struct FakeProvider {
|
||||
versions: HashMap<String, VersionInfo>,
|
||||
compatible: HashMap<String, Vec<String>>,
|
||||
@@ -299,7 +305,7 @@ async fn datapack_dependencies_are_routed_to_the_world_or_instance_by_kind() {
|
||||
assert_eq!(plan.items[1].kind, ContentKind::Mod);
|
||||
assert_eq!(plan.items[1].destination, minecraft.join("mods"));
|
||||
|
||||
install(®istry, &manifest_path, &minecraft, &plan, 7)
|
||||
install(®istry, &manifest_path, &minecraft, &plan)
|
||||
.await
|
||||
.unwrap();
|
||||
let manifest = ContentManifest::load(&manifest_path).unwrap();
|
||||
@@ -936,7 +942,7 @@ async fn dependency_install_commits_files_and_manifest_together() {
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let installed = install(®istry, &manifest_path, &minecraft, &plan, 1)
|
||||
let installed = install(®istry, &manifest_path, &minecraft, &plan)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
@@ -1000,7 +1006,7 @@ async fn failed_dependency_download_leaves_no_partial_install() {
|
||||
.unwrap();
|
||||
|
||||
assert!(
|
||||
install(®istry, &manifest_path, &minecraft, &plan, 2)
|
||||
install(®istry, &manifest_path, &minecraft, &plan)
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user