Fix spin lock

This commit is contained in:
Gregory Schier
2024-07-27 09:05:43 -07:00
parent 54090614ad
commit f925a0cc54
3 changed files with 10 additions and 5 deletions

View File

@@ -1,7 +1,7 @@
extern crate core; extern crate core;
use log::info;
use crate::manager::PluginManager; use crate::manager::PluginManager;
use log::info;
use tauri::plugin::{Builder, TauriPlugin}; use tauri::plugin::{Builder, TauriPlugin};
use tauri::{Manager, RunEvent, Runtime, State}; use tauri::{Manager, RunEvent, Runtime, State};
use tokio::sync::Mutex; use tokio::sync::Mutex;
@@ -24,11 +24,12 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
}) })
}) })
.on_event(|app, e| match e { .on_event(|app, e| match e {
RunEvent::ExitRequested { code, .. } => { // TODO: Also exit when app is force-quit (eg. cmd+r in IntelliJ runner)
RunEvent::Exit => {
tauri::async_runtime::block_on(async move { tauri::async_runtime::block_on(async move {
info!("Exiting plugin runtime due to app exit {:?}", code); info!("Exiting plugin runtime due to app exit");
let manager: State<Mutex<PluginManager>> = app.state(); let manager: State<Mutex<PluginManager>> = app.state();
manager.lock().await.cleanup(); manager.lock().await.cleanup().await;
}); });
} }
_ => {} _ => {}

View File

@@ -1,3 +1,4 @@
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;
@@ -30,8 +31,10 @@ impl PluginManager {
PluginManager { client, kill_tx } PluginManager { client, kill_tx }
} }
pub 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

@@ -75,6 +75,7 @@ pub async fn node_start<R: Runtime>(
// Check on child // Check on child
tokio::spawn(async move { tokio::spawn(async move {
loop { loop {
// Small sleep so we don't spin lock the CPU
tokio::time::sleep(Duration::from_millis(50)).await; tokio::time::sleep(Duration::from_millis(50)).await;
if let Ok(Some(status)) = child.try_wait() { if let Ok(Some(status)) = child.try_wait() {
error!("Plugin runtime exited status={}", status); error!("Plugin runtime exited status={}", status);