Child process manager api (#64)

* child process api

* added hook to js

* process API + restructured process state storage

* formatting

* added path-pid check and fixed probs

* prettier

* added profile checking function

---------

Co-authored-by: Wyatt <wyatt@modrinth.com>
This commit is contained in:
Wyatt Verchere
2023-04-05 10:07:59 -07:00
committed by GitHub
parent 8169d3ad49
commit d5505d3298
14 changed files with 574 additions and 97 deletions

View File

@@ -3,10 +3,12 @@ use serde::{Serialize, Serializer};
use thiserror::Error;
pub mod auth;
pub mod process;
pub mod profile;
pub mod profile_create;
pub mod tags;
pub mod settings;
pub mod tags;
pub type Result<T> = std::result::Result<T, TheseusGuiError>;

View File

@@ -0,0 +1,72 @@
use std::path::{Path, PathBuf};
use crate::api::Result;
use theseus::prelude::*;
// Checks if a process has finished by process PID
#[tauri::command]
pub async fn process_has_finished_by_pid(pid: u32) -> Result<bool> {
Ok(process::has_finished_by_pid(pid).await?)
}
// Gets process exit status by process PID
#[tauri::command]
pub async fn process_get_exit_status_by_pid(pid: u32) -> Result<Option<i32>> {
Ok(process::get_exit_status_by_pid(pid).await?)
}
// Gets all process PIDs
#[tauri::command]
pub async fn process_get_all_pids() -> Result<Vec<u32>> {
Ok(process::get_all_pids().await?)
}
// Gets all running process PIDs
#[tauri::command]
pub async fn process_get_all_running_pids() -> Result<Vec<u32>> {
Ok(process::get_all_running_pids().await?)
}
// Gets all process PIDs by profile path
#[tauri::command]
pub async fn process_get_pids_by_profile_path(
profile_path: &Path,
) -> Result<Vec<u32>> {
Ok(process::get_pids_by_profile_path(profile_path).await?)
}
// Gets the Profile paths of each *running* stored process in the state
#[tauri::command]
pub async fn process_get_all_running_profile_paths() -> Result<Vec<PathBuf>> {
Ok(process::get_all_running_profile_paths().await?)
}
// Gets the Profiles (cloned) of each *running* stored process in the state
#[tauri::command]
pub async fn process_get_all_running_profiles() -> Result<Vec<Profile>> {
Ok(process::get_all_running_profiles().await?)
}
// Gets process stderr by process PID
#[tauri::command]
pub async fn process_get_stderr_by_pid(pid: u32) -> Result<String> {
Ok(process::get_stderr_by_pid(pid).await?)
}
// Gets process stdout by process PID
#[tauri::command]
pub async fn process_get_stdout_by_pid(pid: u32) -> Result<String> {
Ok(process::get_stdout_by_pid(pid).await?)
}
// Kill a process by process PID
#[tauri::command]
pub async fn process_kill_by_pid(pid: u32) -> Result<()> {
Ok(process::kill_by_pid(pid).await?)
}
// Wait for a process to finish by process PID
#[tauri::command]
pub async fn process_wait_for_by_pid(pid: u32) -> Result<()> {
Ok(process::wait_for_by_pid(pid).await?)
}

View File

@@ -76,7 +76,7 @@ pub async fn profile_run(
credentials: theseus::auth::Credentials,
) -> Result<u32> {
let proc_lock = profile::run(path, &credentials).await?;
let pid = proc_lock.read().await.id().ok_or_else(|| {
let pid = proc_lock.read().await.child.id().ok_or_else(|| {
theseus::Error::from(theseus::ErrorKind::LauncherError(
"Process failed to stay open.".to_string(),
))
@@ -93,31 +93,5 @@ pub async fn profile_run_wait(
) -> Result<()> {
let proc_lock = profile::run(path, &credentials).await?;
let mut proc = proc_lock.write().await;
Ok(profile::wait_for(&mut proc).await?)
}
// Wait for a running minecraft process (a Child)
// invoke('profile_wait_for', pid)
#[tauri::command]
pub async fn profile_wait_for(pid: u32) -> Result<()> {
let st = State::get().await?;
if let Some(proc_lock) = st.children.read().await.get(&pid) {
let mut proc = proc_lock.write().await;
return Ok(profile::wait_for(&mut proc).await?);
}
// If child is gone from state, it's not tracked or already finished
Ok(())
}
// Tries to kill a running minecraft process (if PID is still stored)
// invoke('profile_kill', pid)
#[tauri::command]
pub async fn profile_kill(pid: u32) -> Result<()> {
let st = State::get().await?;
if let Some(proc_lock) = st.children.read().await.get(&pid) {
let mut proc = proc_lock.write().await;
return Ok(profile::kill(&mut proc).await?);
}
// If child is gone from state, it's not tracked or already finished
Ok(())
Ok(process::wait_for(&mut proc).await?)
}