Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
j-lanson committed Aug 19, 2024
1 parent 1904547 commit cc22b76
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
16 changes: 10 additions & 6 deletions hipcheck/src/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ pub fn dummy() {
}

pub async fn initialize_plugins(
plugins: Vec<(PluginContext, Value)>,
plugins: Vec<PluginContextWithConfig>,
) -> Result<Vec<PluginTransport>> {
let mut set = tokio::task::JoinSet::new();
for (p, c) in plugins {
for (p, c) in plugins
.into_iter()
.map(Into::<(PluginContext, Value)>::into)
{
set.spawn(p.initialize(c));
}
let mut out: Vec<PluginTransport> = vec![];
Expand All @@ -46,23 +49,24 @@ struct HcPluginCore {
impl HcPluginCore {
// When this object is returned, the plugins are all connected but the
// initialization protocol over the gRPC still needs to be completed
pub async fn new(executor: PluginExecutor, plugins: Vec<(Plugin, Value)>) -> Result<Self> {
pub async fn new(executor: PluginExecutor, plugins: Vec<(PluginWithConfig)>) -> Result<Self> {
// Separate plugins and configs so we can start plugins async
let mut conf_map = HashMap::<String, Value>::new();
let plugins = plugins
.into_iter()
.map(|(p, c)| {
.map(|pc| {
let (p, c) = pc.into();
conf_map.insert(p.name.clone(), c);
p
})
.collect();
let ctxs = executor.start_plugins(plugins).await?;
// Rejoin plugin ctx with its config
let mapped_ctxs: Vec<(PluginContext, Value)> = ctxs
let mapped_ctxs: Vec<PluginContextWithConfig> = ctxs
.into_iter()
.map(|c| {
let conf = conf_map.remove(&c.plugin.name).unwrap();
(c, conf)
PluginContextWithConfig(c, conf)
})
.collect();
// Use configs to initialize corresponding plugin
Expand Down
13 changes: 13 additions & 0 deletions hipcheck/src/plugin/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,3 +327,16 @@ impl PluginTransport {
raw.try_into().map(Some)
}
}

pub struct PluginWithConfig(pub Plugin, pub Value);
impl From<PluginWithConfig> for (Plugin, Value) {
fn from(value: PluginWithConfig) -> Self {
(value.0, value.1)
}
}
pub struct PluginContextWithConfig(pub PluginContext, pub Value);
impl From<PluginContextWithConfig> for (PluginContext, Value) {
fn from(value: PluginContextWithConfig) -> Self {
(value.0, value.1)
}
}

0 comments on commit cc22b76

Please sign in to comment.