Better wait for kill signal

This commit is contained in:
Gregory Schier
2024-07-27 09:13:34 -07:00
parent f925a0cc54
commit def9a3cfd2
2 changed files with 7 additions and 19 deletions

View File

@@ -1,4 +1,3 @@
use std::time::Duration;
use log::{debug, info}; use log::{debug, info};
use tauri::{AppHandle, Manager, Runtime}; use tauri::{AppHandle, Manager, Runtime};
use tokio::sync::watch::Sender; use tokio::sync::watch::Sender;
@@ -33,8 +32,6 @@ impl PluginManager {
pub async fn cleanup(&mut self) { pub async fn cleanup(&mut self) {
self.kill_tx.send_replace(true); self.kill_tx.send_replace(true);
// Give it a sec to get the tx and kill
tokio::time::sleep(Duration::from_millis(500)).await;
} }
pub async fn run_import(&mut self, data: &str) -> Result<HookResponse, String> { pub async fn run_import(&mut self, data: &str) -> Result<HookResponse, String> {

View File

@@ -3,12 +3,12 @@ use std::process::Command;
use std::time::Duration; use std::time::Duration;
use command_group::CommandGroup; use command_group::CommandGroup;
use log::{debug, error, info}; use log::{debug, info};
use rand::distributions::{Alphanumeric, DistString}; use rand::distributions::{Alphanumeric, DistString};
use serde; use serde;
use serde::Deserialize; use serde::Deserialize;
use tauri::path::BaseDirectory;
use tauri::{AppHandle, Manager, Runtime}; use tauri::{AppHandle, Manager, Runtime};
use tauri::path::BaseDirectory;
use tauri_plugin_shell::ShellExt; use tauri_plugin_shell::ShellExt;
use tokio::fs; use tokio::fs;
use tokio::sync::watch::Receiver; use tokio::sync::watch::Receiver;
@@ -70,23 +70,14 @@ pub async fn node_start<R: Runtime>(
.group_spawn() .group_spawn()
.expect("yaaknode failed to start"); .expect("yaaknode failed to start");
let kill_rx = kill_rx.clone(); let mut kill_rx = kill_rx.clone();
// Check on child // Check on child
tokio::spawn(async move { tokio::spawn(async move {
loop { kill_rx.wait_for(|b| *b == true).await.expect("Kill channel errored");
// Small sleep so we don't spin lock the CPU info!("Killing plugin runtime");
tokio::time::sleep(Duration::from_millis(50)).await; child.kill().expect("Failed to kill plugin runtime");
if let Ok(Some(status)) = child.try_wait() { return;
error!("Plugin runtime exited status={}", status);
// TODO: Try restarting plugin runtime
break;
} else if *kill_rx.borrow() {
info!("Stopping plugin runtime");
child.kill().expect("Failed to kill plugin runtime");
break;
}
}
}); });
let start = std::time::Instant::now(); let start = std::time::Instant::now();