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?)
}

View File

@@ -29,8 +29,6 @@ fn main() {
api::profile::profile_list,
api::profile::profile_run,
api::profile::profile_run_wait,
api::profile::profile_kill,
api::profile::profile_wait_for,
api::auth::auth_authenticate_begin_flow,
api::auth::auth_authenticate_await_completion,
api::auth::auth_refresh,
@@ -47,6 +45,17 @@ fn main() {
api::tags::tags_get_tag_bundle,
api::settings::settings_get,
api::settings::settings_set,
api::process::process_get_all_pids,
api::process::process_get_all_running_pids,
api::process::process_get_pids_by_profile_path,
api::process::process_get_all_running_profile_paths,
api::process::process_get_all_running_profiles,
api::process::process_get_exit_status_by_pid,
api::process::process_has_finished_by_pid,
api::process::process_get_stderr_by_pid,
api::process::process_get_stdout_by_pid,
api::process::process_kill_by_pid,
api::process::process_wait_for_by_pid,
])
.run(tauri::generate_context!())
.expect("error while running tauri application");

View File

@@ -0,0 +1,65 @@
/**
* All theseus API calls return serialized values (both return values and errors);
* So, for example, addDefaultInstance creates a blank Profile object, where the Rust struct is serialized,
* and deserialized into a usable JS object.
*/
import { invoke } from '@tauri-apps/api/tauri'
/// Gets if a process has finished by PID
/// Returns bool
export async function has_finished_by_pid(pid) {
return await invoke('process_has_finished_by_pid', { pid })
}
/// Gets process exit status by PID
/// Returns u32
export async function get_exit_status_by_pid(pid) {
return await invoke('process_get_exit_status_by_pid', { pid })
}
/// Gets all process IDs
/// Returns [u32]
export async function get_all_pids() {
return await invoke('process_get_all_pids')
}
/// Gets all running process IDs
/// Returns [u32]
export async function get_all_running_pids() {
return await invoke('process_get_all_running_pids')
}
/// Gets all running process IDs with a given profile path
/// Returns [u32]
export async function get_pids_by_profile_path(profile_path) {
return await invoke('process_get_pids_by_profile_path', { profile_path })
}
/// Gets all running process IDs with a given profile path
/// Returns [u32]
export async function get_all_running_profile_paths(profile_path) {
return await invoke('process_get_all_running_profile_paths', { profile_path })
}
/// Gets all running process IDs with a given profile path
/// Returns [u32]
export async function get_all_running_profiles(profile_path) {
return await invoke('process_get_all_running_profiles', { profile_path })
}
/// Gets process stderr by PID
/// Returns String
export async function get_stderr_by_pid(pid) {
return await invoke('process_get_stderr_by_pid', { pid })
}
/// Gets process stdout by PID
/// Returns String
export async function get_stdout_by_pid(pid) {
return await invoke('process_get_stdout_by_pid', { pid })
}
/// Kills a process by PID
export async function kill_by_pid(pid) {
return await invoke('process_kill_by_pid', { pid })
}

View File

@@ -66,13 +66,3 @@ export async function run(path, credentials) {
export async function run_wait(path, credentials) {
return await invoke('profile_run_wait', { path, credentials })
}
// Tries to kill a running minecraft process (if PID is still stored)
export async function kill(child_pid) {
return await invoke('profile_kill', { child_pid })
}
// Wait for a running minecraft process (a Child)
export async function wait_for(child_pid) {
return await invoke('profile_wait_for', { child_pid })
}