Skip to content

Commit

Permalink
feat: temporary code for actually interacting with a plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
j-lanson committed Aug 20, 2024
1 parent cec8dba commit 562a18e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
42 changes: 41 additions & 1 deletion hipcheck/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use crate::cache::HcCache;
use crate::context::Context as _;
use crate::error::Error;
use crate::error::Result;
use crate::plugin::{HcPluginCore, Plugin, PluginExecutor};
use crate::session::session::Session;
use crate::setup::{resolve_and_transform_source, SourceType};
use crate::shell::verbosity::Verbosity;
Expand Down Expand Up @@ -136,7 +137,7 @@ fn main() -> ExitCode {
Some(FullCommands::Check(args)) => return cmd_check(&args, &config),
Some(FullCommands::Schema(args)) => cmd_schema(&args),
Some(FullCommands::Setup(args)) => return cmd_setup(&args, &config),
Some(FullCommands::Ready) => cmd_ready(&config),
Some(FullCommands::Ready) => cmd_plugin(&config),
Some(FullCommands::Update(args)) => cmd_update(&args),
Some(FullCommands::Cache(args)) => return cmd_cache(args, &config),
Some(FullCommands::PrintConfig) => cmd_print_config(config.config()),
Expand Down Expand Up @@ -621,6 +622,45 @@ fn check_github_token() -> StdResult<(), EnvVarCheckError> {
})
}

fn cmd_plugin(config: &CliConfig) {
use tokio::runtime::Runtime;
let tgt_dir = "./target/debug";
let entrypoint = pathbuf![tgt_dir, "dummy_rand_data"];
let plugin = Plugin {
name: "rand_data".to_owned(),
entrypoint: entrypoint.display().to_string(),
};
let plugin_executor = PluginExecutor::new(
/* max_spawn_attempts */ 3,
/* max_conn_attempts */ 5,
/* port_range */ 40000..u16::MAX,
/* backoff_interval_micros */ 1000,
/* jitter_percent */ 10,
)
.unwrap();
let rt = Runtime::new().unwrap();
rt.block_on(async move {
println!("Started executor");
let mut core =
match HcPluginCore::new(plugin_executor, vec![(plugin, serde_json::json!(null))]).await
{
Ok(c) => c,
Err(e) => {
println!("{e}");
return;
}
};
match core.run().await {
Ok(_) => {
println!("HcCore run completed");
}
Err(e) => {
println!("HcCore run failed with '{e}'");
}
};
});
}

fn cmd_ready(config: &CliConfig) {
let ready = ReadyChecks {
hipcheck_version_check: check_hipcheck_version(),
Expand Down
5 changes: 4 additions & 1 deletion hipcheck/src/plugin/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use futures::Future;
use rand::Rng;
use std::collections::HashSet;
use std::ops::Range;
use std::process::Command;
use std::process::{Command, Stdio};
use tokio::time::{sleep_until, Duration, Instant};

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -69,6 +69,9 @@ impl PluginExecutor {
// Spawn plugin process
let Ok(mut proc) = Command::new(&plugin.entrypoint)
.args(["--port", port_str.as_str()])
// @Temporary - directly forward stdout/stderr from plugin to shell
.stdout(std::io::stdout())
.stderr(std::io::stderr())
.spawn()
else {
spawn_attempts += 1;
Expand Down
24 changes: 24 additions & 0 deletions hipcheck/src/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,28 @@ impl HcPluginCore {
// Now we have a set of started and initialized plugins to interact with
Ok(HcPluginCore { executor, plugins })
}
// @Temporary
pub async fn run(&mut self) -> Result<()> {
let channel = self.plugins.get_mut(&"rand_data".to_owned()).unwrap();
match channel
.send(Query {
id: 1,
request: true,
publisher: "".to_owned(),
plugin: "".to_owned(),
query: "rand_data".to_owned(),
key: serde_json::json!(7),
output: serde_json::json!(null),
})
.await
{
Ok(q) => q,
Err(e) => {
println!("Failed: {e}");
}
};
let resp = channel.recv().await?;
println!("Plugin response: {resp:?}");
Ok(())
}
}

0 comments on commit 562a18e

Please sign in to comment.