mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-18 15:34:09 +01:00
Model and DB refactor (#61)
- [x] Move from `sqlx` to `rusqlite` - [x] Generate TS types from Rust models
This commit is contained in:
102
src-tauri/yaak_plugin_runtime/src/nodejs.rs
Normal file
102
src-tauri/yaak_plugin_runtime/src/nodejs.rs
Normal file
@@ -0,0 +1,102 @@
|
||||
use std::path::PathBuf;
|
||||
use std::time::Duration;
|
||||
|
||||
use log::info;
|
||||
use rand::distributions::{Alphanumeric, DistString};
|
||||
use serde;
|
||||
use serde::Deserialize;
|
||||
use tauri::path::BaseDirectory;
|
||||
use tauri::{AppHandle, Manager, Runtime};
|
||||
use tauri_plugin_shell::ShellExt;
|
||||
use tokio::fs;
|
||||
use tokio::sync::watch::Receiver;
|
||||
|
||||
#[derive(Deserialize, Default)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
struct PortFile {
|
||||
port: i32,
|
||||
}
|
||||
|
||||
pub struct StartResp {
|
||||
pub addr: String,
|
||||
}
|
||||
|
||||
pub async fn node_start<R: Runtime>(
|
||||
app: &AppHandle<R>,
|
||||
temp_dir: &PathBuf,
|
||||
kill_rx: &Receiver<bool>,
|
||||
) -> StartResp {
|
||||
let port_file_path = temp_dir.join(Alphanumeric.sample_string(&mut rand::thread_rng(), 10));
|
||||
|
||||
let plugins_dir = app
|
||||
.path()
|
||||
.resolve("plugins", BaseDirectory::Resource)
|
||||
.expect("failed to resolve plugin directory resource");
|
||||
|
||||
let plugin_runtime_main = app
|
||||
.path()
|
||||
.resolve("plugin-runtime", BaseDirectory::Resource)
|
||||
.expect("failed to resolve plugin runtime resource")
|
||||
.join("index.cjs");
|
||||
|
||||
// HACK: Remove UNC prefix for Windows paths to pass to sidecar
|
||||
|
||||
let plugins_dir = dunce::simplified(plugins_dir.as_path())
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
let plugin_runtime_main = dunce::simplified(plugin_runtime_main.as_path())
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
|
||||
info!(
|
||||
"Starting plugin runtime\n → port_file={}\n → plugins_dir={}\n → runtime_dir={}",
|
||||
port_file_path.to_string_lossy(),
|
||||
plugins_dir,
|
||||
plugin_runtime_main,
|
||||
);
|
||||
|
||||
let cmd = app
|
||||
.shell()
|
||||
.sidecar("yaaknode")
|
||||
.expect("yaaknode not found")
|
||||
.env("YAAK_GRPC_PORT_FILE_PATH", port_file_path.clone())
|
||||
.env("YAAK_PLUGINS_DIR", plugins_dir)
|
||||
.args(&[plugin_runtime_main]);
|
||||
|
||||
println!("Waiting on plugin runtime");
|
||||
let (_, child) = cmd.spawn().expect("yaaknode failed to start");
|
||||
|
||||
let mut kill_rx = kill_rx.clone();
|
||||
|
||||
// Check on child
|
||||
tokio::spawn(async move {
|
||||
kill_rx
|
||||
.wait_for(|b| *b == true)
|
||||
.await
|
||||
.expect("Kill channel errored");
|
||||
info!("Killing plugin runtime");
|
||||
child.kill().expect("Failed to kill plugin runtime");
|
||||
info!("Killed plugin runtime");
|
||||
return;
|
||||
});
|
||||
|
||||
let start = std::time::Instant::now();
|
||||
let port_file_contents = loop {
|
||||
if start.elapsed().as_millis() > 30000 {
|
||||
panic!("Failed to read port file in time");
|
||||
}
|
||||
|
||||
match fs::read_to_string(port_file_path.clone()).await {
|
||||
Ok(s) => break s,
|
||||
Err(_) => {
|
||||
tokio::time::sleep(Duration::from_millis(500)).await;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let port_file: PortFile = serde_json::from_str(port_file_contents.as_str()).unwrap();
|
||||
info!("Started plugin runtime on :{}", port_file.port);
|
||||
let addr = format!("http://localhost:{}", port_file.port);
|
||||
|
||||
StartResp { addr }
|
||||
}
|
||||
Reference in New Issue
Block a user