Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(shell) add wait fn to get event from child process #1947

Draft
wants to merge 4 commits into
base: v2
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions plugins/shell/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::{
scope::ExecuteArgs,
Shell,
};
use std::process::ExitStatus;

type ChildId = u32;

Expand Down Expand Up @@ -302,6 +303,20 @@ pub fn kill<R: Runtime>(
Ok(())
}

#[tauri::command]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use try_wait and if it's ready then wait for the response

pub fn wait<R: Runtime>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will block the whole app to wait for the process

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use async to avoid blocking thread

_window: Window<R>,
shell: State<'_, Shell<R>>,
pid: ChildId,
) -> crate::Result<ExitStatus> {
if let Some(child) = shell.children.lock().unwrap().get(&pid) {
let exitstatus: ExitStatus = child.wait()?;
Ok(exitstatus)
} else {
Err(crate::Error::UnknowChildProcess)
}
}

#[tauri::command]
pub async fn open<R: Runtime>(
_window: Window<R>,
Expand Down
2 changes: 2 additions & 0 deletions plugins/shell/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub enum Error {
/// Utf8 error.
#[error(transparent)]
Utf8(#[from] std::string::FromUtf8Error),
#[error("Child process does'nt exist")]
UnknowChildProcess,
}

impl Serialize for Error {
Expand Down
6 changes: 6 additions & 0 deletions plugins/shell/src/process/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ impl CommandChild {
pub fn pid(&self) -> u32 {
self.inner.id()
}

/// Waits for the child to exit completely, returning the status that it exited with.
pub fn wait(self) -> crate::Result<std::process::ExitStatus> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will block the whole app to wait for the process to exit, we need to use something async / non blocking

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use async fn

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async fn will also block it

let exitstatus = self.inner.wait()?;
Ok(exitstatus)
}
}

/// Describes the result of a process after it has terminated.
Expand Down