From c9f715ddeddc3c02fec40c366b56739561f68255 Mon Sep 17 00:00:00 2001 From: stubbfelnewpc Date: Thu, 2 Jul 2020 20:34:05 +0200 Subject: [PATCH] add workingthread --- server/lib/src/lib.rs | 2 +- server/podman/src/lib.rs | 44 ++++++++++++++++++++++++++++++---------- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/server/lib/src/lib.rs b/server/lib/src/lib.rs index f4b39ec..7dd3430 100644 --- a/server/lib/src/lib.rs +++ b/server/lib/src/lib.rs @@ -36,7 +36,7 @@ pub enum IdeState { DELETED, } -#[derive(Serialize, Deserialize, Hash, JsonSchema)] +#[derive(Serialize, Deserialize, Hash, JsonSchema, Clone)] pub struct OpenGitParam { pub inquirer: String, pub clone_url: String, diff --git a/server/podman/src/lib.rs b/server/podman/src/lib.rs index 23ff85b..c920000 100644 --- a/server/podman/src/lib.rs +++ b/server/podman/src/lib.rs @@ -11,6 +11,7 @@ use rocket::http::Status; use std::fs; use std::path::{Path, PathBuf}; use std::process::Command; +use std::thread; // podman create --name test -p 3000:3000 -v $PWD:/nixide -w /nixide docker.io/nixos/nix nix-shell --pure start-ide.nix --run run_ide.sh @@ -38,6 +39,13 @@ pub struct PodmanContainerList { container: Vec, } +struct ThreadRunningParam { + ide_folder: String, + container: PodmanContainer, + param: OpenGitParam, + working_folder: PathBuf, +} + pub struct PodmanEngine { working_folder: PathBuf, list: PodmanContainerList, @@ -85,6 +93,7 @@ struct PodmanIdeStatus { ide_state: IdeState, } +#[derive(Clone)] pub struct PodmanEngineTraitImpl { working_folder: PathBuf, } @@ -131,21 +140,20 @@ impl NixIdeManageServiceEngine for PodmanEngineTraitImpl { ide_param, }; - eng.list.container.push(container); + eng.list.container.push(container.clone()); eng.save() .map_err(|_| Status::new(500, "internal error: can't save curent state"))?; - fetch_sources(&format!("{}/repo", ide_folder), param) - .map_err(|_| Status::new(500, "internal error"))?; - match eng.find_mut_first_container(ide_id) { - Some(i) => { - i.state = PodmanState::StartingContainer; - eng.save().map_err(|_| { - Status::new(500, "internal error: can't save curent state") - })?; - } - _ => {} + let thread_param = ThreadRunningParam { + ide_folder, + container, + param: param.clone(), + working_folder: eng.working_folder, }; + + thread::spawn(move || { + working_thread(thread_param); + }); Ok(IdeState::OPENING) } @@ -246,6 +254,20 @@ fn fetch_sources(repo_path: &str, param: &OpenGitParam) -> Result<(), std::io::E } } +fn working_thread(thread_param: ThreadRunningParam) { + let ide_folder = thread_param.ide_folder; + let param = thread_param.param; + fetch_sources(&format!("{}/repo", ide_folder), ¶m).unwrap(); + let mut eng = PodmanEngine::_new(thread_param.working_folder); + let ide_id = thread_param.container.ide_id; + match eng.find_mut_first_container(&ide_id) { + Some(i) => { + i.state = PodmanState::StartingContainer; + eng.save().unwrap(); + } + _ => {} + }; +} #[test] fn test_fetch_sources() { let now = std::time::SystemTime::now()