Plugin runtime v2 (#62)

This commit is contained in:
Gregory Schier
2024-08-08 21:30:59 -07:00
committed by GitHub
parent f967820f12
commit 063e6cf00c
64 changed files with 1539 additions and 705 deletions

View File

@@ -1,14 +1,12 @@
use std::path::PathBuf;
use std::time::Duration;
use std::net::SocketAddr;
use crate::error::Result;
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::process::CommandEvent;
use tauri_plugin_shell::ShellExt;
use tokio::fs;
use tokio::sync::watch::Receiver;
#[derive(Deserialize, Default)]
@@ -17,57 +15,48 @@ struct PortFile {
port: i32,
}
pub struct StartResp {
pub addr: String,
}
pub async fn node_start<R: Runtime>(
pub async fn start_nodejs_plugin_runtime<R: Runtime>(
app: &AppHandle<R>,
temp_dir: &PathBuf,
addr: SocketAddr,
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");
) -> Result<()> {
let plugin_runtime_main = app
.path()
.resolve("plugin-runtime", BaseDirectory::Resource)
.expect("failed to resolve plugin runtime resource")
.resolve("plugin-runtime", BaseDirectory::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,
);
info!("Starting plugin runtime main={}", 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)
.sidecar("yaaknode")?
.env("PORT", addr.port().to_string())
.args(&[plugin_runtime_main]);
println!("Waiting on plugin runtime");
let (_, child) = cmd.spawn().expect("yaaknode failed to start");
let (mut child_rx, child) = cmd.spawn()?;
println!("Spawned plugin runtime");
let mut kill_rx = kill_rx.clone();
tokio::spawn(async move {
while let Some(event) = child_rx.recv().await {
match event {
CommandEvent::Stderr(line) => {
print!("{}", String::from_utf8(line).unwrap());
}
CommandEvent::Stdout(line) => {
print!("{}", String::from_utf8(line).unwrap());
}
_ => {}
}
}
});
// Check on child
tokio::spawn(async move {
kill_rx
@@ -77,26 +66,7 @@ pub async fn node_start<R: Runtime>(
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 }
Ok(())
}