refactor: better profile deletion handling

This commit is contained in:
zhom
2025-08-13 08:26:24 +04:00
parent f622c77a3e
commit 83a7c0e394
+42 -18
View File
@@ -741,25 +741,33 @@ impl ProfileManager {
} }
} }
// Update the process ID if we found a different one // Only persist status changes if the profile metadata still exists on disk
if let Some(pid) = found_pid { let profiles_dir = self.get_profiles_dir();
if inner_profile.process_id != Some(pid) { let profile_uuid_dir = profiles_dir.join(profile.id.to_string());
inner_profile.process_id = Some(pid); let metadata_file = profile_uuid_dir.join("metadata.json");
let metadata_exists = metadata_file.exists();
if metadata_exists {
// Update the process ID if we found a different one
if let Some(pid) = found_pid {
if inner_profile.process_id != Some(pid) {
inner_profile.process_id = Some(pid);
if let Err(e) = self.save_profile(&inner_profile) {
println!("Warning: Failed to update profile with new PID: {e}");
}
}
} else if inner_profile.process_id.is_some() {
// Clear the PID if no process found
inner_profile.process_id = None;
if let Err(e) = self.save_profile(&inner_profile) { if let Err(e) = self.save_profile(&inner_profile) {
println!("Warning: Failed to update profile with new PID: {e}"); println!("Warning: Failed to clear profile PID: {e}");
} }
} }
} else if inner_profile.process_id.is_some() {
// Clear the PID if no process found
inner_profile.process_id = None;
if let Err(e) = self.save_profile(&inner_profile) {
println!("Warning: Failed to clear profile PID: {e}");
}
}
// Emit profile update event to frontend // Emit profile update event to frontend
if let Err(e) = app_handle.emit("profile-updated", &inner_profile) { if let Err(e) = app_handle.emit("profile-updated", &inner_profile) {
println!("Warning: Failed to emit profile update event: {e}"); println!("Warning: Failed to emit profile update event: {e}");
}
} }
Ok(is_running) Ok(is_running)
@@ -783,7 +791,13 @@ impl ProfileManager {
Ok(Some(camoufox_process)) => { Ok(Some(camoufox_process)) => {
// Found a running instance, update profile with process info if changed // Found a running instance, update profile with process info if changed
let process_id_changed = profile.process_id != camoufox_process.processId; let process_id_changed = profile.process_id != camoufox_process.processId;
if process_id_changed { // Only write status changes if metadata still exists
let profiles_dir = self.get_profiles_dir();
let profile_uuid_dir = profiles_dir.join(profile.id.to_string());
let metadata_file = profile_uuid_dir.join("metadata.json");
let metadata_exists = metadata_file.exists();
if process_id_changed && metadata_exists {
let mut updated_profile = profile.clone(); let mut updated_profile = profile.clone();
updated_profile.process_id = camoufox_process.processId; updated_profile.process_id = camoufox_process.processId;
if let Err(e) = self.save_profile(&updated_profile) { if let Err(e) = self.save_profile(&updated_profile) {
@@ -804,7 +818,12 @@ impl ProfileManager {
} }
Ok(None) => { Ok(None) => {
// No running instance found, clear process ID if set // No running instance found, clear process ID if set
if profile.process_id.is_some() { let profiles_dir = self.get_profiles_dir();
let profile_uuid_dir = profiles_dir.join(profile.id.to_string());
let metadata_file = profile_uuid_dir.join("metadata.json");
let metadata_exists = metadata_file.exists();
if profile.process_id.is_some() && metadata_exists {
let mut updated_profile = profile.clone(); let mut updated_profile = profile.clone();
updated_profile.process_id = None; updated_profile.process_id = None;
if let Err(e) = self.save_profile(&updated_profile) { if let Err(e) = self.save_profile(&updated_profile) {
@@ -821,7 +840,12 @@ impl ProfileManager {
Err(e) => { Err(e) => {
// Error checking status, assume not running and clear process ID // Error checking status, assume not running and clear process ID
println!("Warning: Failed to check Camoufox status via nodecar: {e}"); println!("Warning: Failed to check Camoufox status via nodecar: {e}");
if profile.process_id.is_some() { let profiles_dir = self.get_profiles_dir();
let profile_uuid_dir = profiles_dir.join(profile.id.to_string());
let metadata_file = profile_uuid_dir.join("metadata.json");
let metadata_exists = metadata_file.exists();
if profile.process_id.is_some() && metadata_exists {
let mut updated_profile = profile.clone(); let mut updated_profile = profile.clone();
updated_profile.process_id = None; updated_profile.process_id = None;
if let Err(e) = self.save_profile(&updated_profile) { if let Err(e) = self.save_profile(&updated_profile) {