Reload plugins on change

This commit is contained in:
Gregory Schier
2024-09-09 11:34:52 -07:00
parent 3bf192953d
commit c0707bb246
18 changed files with 240 additions and 118 deletions

View File

@@ -11,6 +11,7 @@ use std::str::FromStr;
use std::time::Duration;
use base64::Engine;
use chrono::Utc;
use fern::colors::ColoredLevelConfig;
use log::{debug, error, info, warn};
use rand::random;
@@ -61,6 +62,7 @@ use yaak_plugin_runtime::events::{
InternalEvent, InternalEventPayload, PluginBootResponse, RenderHttpRequestResponse,
SendHttpRequestResponse,
};
use yaak_plugin_runtime::handle::PluginHandle;
use yaak_templates::{Parser, Tokens};
mod analytics;
@@ -1170,7 +1172,11 @@ async fn cmd_create_workspace(name: &str, w: WebviewWindow) -> Result<Workspace,
}
#[tauri::command]
async fn cmd_create_plugin(directory: &str, url: Option<String>, w: WebviewWindow) -> Result<Plugin, String> {
async fn cmd_create_plugin(
directory: &str,
url: Option<String>,
w: WebviewWindow,
) -> Result<Plugin, String> {
upsert_plugin(
&w,
Plugin {
@@ -1456,6 +1462,12 @@ async fn cmd_list_plugins(w: WebviewWindow) -> Result<Vec<Plugin>, String> {
list_plugins(&w).await.map_err(|e| e.to_string())
}
#[tauri::command]
async fn cmd_reload_plugins(plugin_manager: State<'_, PluginManager>) -> Result<(), String> {
plugin_manager.reload_all().await;
Ok(())
}
#[tauri::command]
async fn cmd_plugin_info(
id: &str,
@@ -1734,9 +1746,6 @@ pub fn run() {
cmd_delete_http_response,
cmd_delete_workspace,
cmd_dismiss_notification,
cmd_parse_template,
cmd_template_tokens_to_string,
cmd_render_template,
cmd_duplicate_grpc_request,
cmd_duplicate_http_request,
cmd_export_data,
@@ -1752,7 +1761,6 @@ pub fn run() {
cmd_grpc_go,
cmd_grpc_reflect,
cmd_http_request_actions,
cmd_template_functions,
cmd_import_data,
cmd_list_cookie_jars,
cmd_list_environments,
@@ -1763,16 +1771,21 @@ pub fn run() {
cmd_list_http_requests,
cmd_list_http_responses,
cmd_list_plugins,
cmd_plugin_info,
cmd_list_workspaces,
cmd_metadata,
cmd_new_nested_window,
cmd_new_window,
cmd_parse_template,
cmd_plugin_info,
cmd_reload_plugins,
cmd_render_template,
cmd_save_response,
cmd_send_ephemeral_request,
cmd_send_http_request,
cmd_set_key_value,
cmd_set_update_mode,
cmd_template_functions,
cmd_template_tokens_to_string,
cmd_track_event,
cmd_update_cookie_jar,
cmd_update_environment,
@@ -1996,17 +2009,25 @@ fn monitor_plugin_events<R: Runtime>(app_handle: &AppHandle<R>) {
while let Some(event) = rx.recv().await {
let app_handle = app_handle.clone();
let plugin = plugin_manager
.get_plugin(event.plugin_ref_id.as_str())
.await
.unwrap();
// We might have recursive back-and-forth calls between app and plugin, so we don't
// want to block here
tauri::async_runtime::spawn(async move {
handle_plugin_event(&app_handle, &event).await;
handle_plugin_event(&app_handle, &event, &plugin).await;
});
}
});
}
async fn handle_plugin_event<R: Runtime>(app_handle: &AppHandle<R>, event: &InternalEvent) {
async fn handle_plugin_event<R: Runtime>(
app_handle: &AppHandle<R>,
event: &InternalEvent,
plugin_handle: &PluginHandle,
) {
// info!("Got event to app {}", event.id);
let response_event: Option<InternalEventPayload> = match event.clone().payload {
InternalEventPayload::CopyTextRequest(req) => {
@@ -2064,6 +2085,27 @@ async fn handle_plugin_event<R: Runtime>(app_handle: &AppHandle<R>, event: &Inte
},
))
}
InternalEventPayload::ReloadResponse(_) => {
let w = get_focused_window_no_lock(app_handle).expect("No focused window");
let plugins = list_plugins(&w).await.unwrap();
for plugin in plugins {
if plugin.directory != plugin_handle.dir {
continue;
}
upsert_plugin(
&w,
Plugin {
// TODO: Add reloaded_at field to use instead
updated_at: Utc::now().naive_utc(),
..plugin
},
)
.await
.unwrap();
}
None
}
InternalEventPayload::SendHttpRequestRequest(req) => {
let w = get_focused_window_no_lock(app_handle).expect("No focused window");
let url = w.url().unwrap();
@@ -2124,7 +2166,6 @@ fn get_focused_window_no_lock<R: Runtime>(app_handle: &AppHandle<R>) -> Option<W
// TODO: Getting the focused window doesn't seem to work on Windows, so
// we'll need to pass the window label into plugin events instead.
if app_handle.webview_windows().len() == 1 {
debug!("Returning only webview window");
let w = app_handle
.webview_windows()
.iter()

View File

@@ -2,7 +2,10 @@ use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use ts_rs::TS;
use yaak_models::models::{CookieJar, Environment, Folder, GrpcConnection, GrpcEvent, GrpcRequest, HttpRequest, HttpResponse, KeyValue, Plugin, Settings, Workspace};
use yaak_models::models::{
CookieJar, Environment, Folder, GrpcConnection, GrpcEvent, GrpcRequest, HttpRequest,
HttpResponse, KeyValue, Plugin, Settings, Workspace,
};
#[derive(Debug, Clone, Serialize, Deserialize, TS)]
#[serde(rename_all = "camelCase")]
@@ -21,6 +24,9 @@ pub enum InternalEventPayload {
BootRequest(PluginBootRequest),
BootResponse(PluginBootResponse),
ReloadRequest(EmptyResponse),
ReloadResponse(EmptyResponse),
ImportRequest(ImportRequest),
ImportResponse(ImportResponse),

View File

@@ -0,0 +1,66 @@
use crate::events::{EmptyResponse, InternalEvent, InternalEventPayload, PluginBootResponse};
use crate::server::plugin_runtime::EventStreamEvent;
use crate::util::gen_id;
use std::sync::Arc;
use tokio::sync::{mpsc, Mutex};
#[derive(Clone)]
pub struct PluginHandle {
pub ref_id: String,
pub dir: String,
pub(crate) to_plugin_tx: Arc<Mutex<mpsc::Sender<tonic::Result<EventStreamEvent>>>>,
pub(crate) boot_resp: Arc<Mutex<Option<PluginBootResponse>>>,
}
impl PluginHandle {
pub async fn name(&self) -> String {
match &*self.boot_resp.lock().await {
None => "__NOT_BOOTED__".to_string(),
Some(r) => r.name.to_owned(),
}
}
pub async fn info(&self) -> Option<PluginBootResponse> {
let resp = &*self.boot_resp.lock().await;
resp.clone()
}
pub fn build_event_to_send(
&self,
payload: &InternalEventPayload,
reply_id: Option<String>,
) -> InternalEvent {
InternalEvent {
id: gen_id(),
plugin_ref_id: self.ref_id.clone(),
reply_id,
payload: payload.clone(),
}
}
pub async fn reload(&self) -> crate::error::Result<()> {
let event = self.build_event_to_send(&InternalEventPayload::ReloadRequest(EmptyResponse{}), None);
self.send(&event).await
}
pub async fn send(&self, event: &InternalEvent) -> crate::error::Result<()> {
// info!(
// "Sending event to plugin {} {:?}",
// event.id,
// self.name().await
// );
self.to_plugin_tx
.lock()
.await
.send(Ok(EventStreamEvent {
event: serde_json::to_string(&event)?,
}))
.await?;
Ok(())
}
pub async fn boot(&self, resp: &PluginBootResponse) {
let mut boot_resp = self.boot_resp.lock().await;
*boot_resp = Some(resp.clone());
}
}

View File

@@ -4,3 +4,5 @@ pub mod manager;
mod nodejs;
pub mod plugin;
mod server;
pub mod handle;
mod util;

View File

@@ -15,6 +15,7 @@ use std::time::Duration;
use tauri::{AppHandle, Runtime};
use tokio::sync::mpsc;
use tokio::sync::watch::Sender;
use crate::handle::PluginHandle;
pub struct PluginManager {
kill_tx: Sender<bool>,
@@ -38,6 +39,10 @@ impl PluginManager {
PluginManager { kill_tx, server }
}
pub async fn reload_all(&self) {
self.server.reload_plugins().await
}
pub async fn subscribe(&self) -> (String, mpsc::Receiver<InternalEvent>) {
self.server.subscribe().await
}
@@ -68,6 +73,10 @@ impl PluginManager {
self.server.plugin_by_dir(dir).await.ok()?.info().await
}
pub async fn get_plugin(&self, ref_id: &str) -> Result<PluginHandle> {
self.server.plugin_by_ref_id(ref_id).await
}
pub async fn get_http_request_actions(&self) -> Result<Vec<GetHttpRequestActionsResponse>> {
let reply_events = self
.server

View File

@@ -1,7 +1,7 @@
use rand::distributions::{Alphanumeric, DistString};
use std::collections::HashMap;
use std::pin::Pin;
use std::sync::Arc;
use log::warn;
use tokio::sync::mpsc::Receiver;
use tokio::sync::{mpsc, Mutex};
use tonic::codegen::tokio_stream::wrappers::ReceiverStream;
@@ -10,8 +10,10 @@ use tonic::{Request, Response, Status, Streaming};
use crate::error::Error::PluginNotFoundErr;
use crate::error::Result;
use crate::events::{PluginBootRequest, PluginBootResponse, InternalEvent, InternalEventPayload};
use crate::events::{InternalEvent, InternalEventPayload, PluginBootRequest, PluginBootResponse};
use crate::handle::PluginHandle;
use crate::server::plugin_runtime::plugin_runtime_server::PluginRuntime;
use crate::util::gen_id;
use plugin_runtime::EventStreamEvent;
use yaak_models::queries::generate_id;
@@ -22,62 +24,6 @@ pub mod plugin_runtime {
type ResponseStream =
Pin<Box<dyn Stream<Item = std::result::Result<EventStreamEvent, Status>> + Send>>;
#[derive(Clone)]
pub struct PluginHandle {
dir: String,
to_plugin_tx: Arc<Mutex<mpsc::Sender<tonic::Result<EventStreamEvent>>>>,
ref_id: String,
boot_resp: Arc<Mutex<Option<PluginBootResponse>>>,
}
impl PluginHandle {
pub async fn name(&self) -> String {
match &*self.boot_resp.lock().await {
None => "__NOT_BOOTED__".to_string(),
Some(r) => r.name.to_owned(),
}
}
pub async fn info(&self) -> Option<PluginBootResponse> {
let resp = &*self.boot_resp.lock().await;
resp.clone()
}
pub fn build_event_to_send(
&self,
payload: &InternalEventPayload,
reply_id: Option<String>,
) -> InternalEvent {
InternalEvent {
id: gen_id(),
plugin_ref_id: self.ref_id.clone(),
reply_id,
payload: payload.clone(),
}
}
pub async fn send(&self, event: &InternalEvent) -> Result<()> {
// info!(
// "Sending event to plugin {} {:?}",
// event.id,
// self.name().await
// );
self.to_plugin_tx
.lock()
.await
.send(Ok(EventStreamEvent {
event: serde_json::to_string(&event)?,
}))
.await?;
Ok(())
}
pub async fn boot(&self, resp: &PluginBootResponse) {
let mut boot_resp = self.boot_resp.lock().await;
*boot_resp = Some(resp.clone());
}
}
#[derive(Clone)]
pub struct PluginRuntimeGrpcServer {
plugin_ref_to_plugin: Arc<Mutex<HashMap<String, PluginHandle>>>,
@@ -96,6 +42,15 @@ impl PluginRuntimeGrpcServer {
}
}
pub async fn plugins(&self) -> Vec<PluginHandle> {
self.plugin_ref_to_plugin
.lock()
.await
.iter()
.map(|p| p.1.to_owned())
.collect::<Vec<PluginHandle>>()
}
pub async fn subscribe(&self) -> (String, Receiver<InternalEvent>) {
let (tx, rx) = mpsc::channel(128);
let rx_id = generate_id();
@@ -115,23 +70,15 @@ impl PluginRuntimeGrpcServer {
pub async fn remove_plugin(&self, id: &str) {
match self.plugin_ref_to_plugin.lock().await.remove(id) {
None => {
println!("Tried to remove non-existing plugin {}", id);
}
Some(plugin) => {
println!("Removed plugin {} {}", id, plugin.name().await);
}
None => println!("Tried to remove non-existing plugin {}", id),
Some(plugin) => println!("Removed plugin {} {}", id, plugin.name().await),
};
}
pub async fn boot_plugin(&self, id: &str, resp: &PluginBootResponse) {
match self.plugin_ref_to_plugin.lock().await.get(id) {
None => {
println!("Tried booting non-existing plugin {}", id);
}
Some(plugin) => {
plugin.clone().boot(resp).await;
}
None => println!("Tried booting non-existing plugin {}", id),
Some(plugin) => plugin.clone().boot(resp).await,
}
}
@@ -162,7 +109,7 @@ impl PluginRuntimeGrpcServer {
Some(p) => Ok(p.to_owned()),
}
}
pub async fn plugin_by_dir(&self, dir: &str) -> Result<PluginHandle> {
let plugins = self.plugin_ref_to_plugin.lock().await;
for p in plugins.values() {
@@ -299,6 +246,14 @@ impl PluginRuntimeGrpcServer {
Ok(events)
}
pub async fn reload_plugins(&self) {
for (_, plugin) in self.plugin_ref_to_plugin.lock().await.clone() {
if let Err(e) = plugin.reload().await {
warn!("Failed to reload plugin {} {}", plugin.dir, e)
}
}
}
async fn load_plugins(
&self,
to_plugin_tx: mpsc::Sender<tonic::Result<EventStreamEvent>>,
@@ -396,7 +351,3 @@ impl PluginRuntime for PluginRuntimeGrpcServer {
))
}
}
fn gen_id() -> String {
Alphanumeric.sample_string(&mut rand::thread_rng(), 5)
}

View File

@@ -0,0 +1,5 @@
use rand::distributions::{Alphanumeric, DistString};
pub fn gen_id() -> String {
Alphanumeric.sample_string(&mut rand::thread_rng(), 5)
}