Skip to content

Commit

Permalink
fix(host): Recreates polyfill imports on update
Browse files Browse the repository at this point in the history
This fixes an issue where if you add a new custom interface to an actor
when updating it, it would fail to have the imports in place

Signed-off-by: Taylor Thomas <taylor@cosmonic.com>
  • Loading branch information
thomastaylor312 committed Mar 18, 2024
1 parent e114b77 commit 5b4f75b
Showing 1 changed file with 31 additions and 23 deletions.
54 changes: 31 additions & 23 deletions crates/host/src/wasmbus/mod.rs
Expand Up @@ -2269,29 +2269,8 @@ impl Host {
self.store_component_spec(&actor_id, &component_spec)
.await?;

let polyfilled_imports = component.polyfilled_imports().clone();
// Map the imports to pull out the result types of the functions for lookup when invoking them
let imports = polyfilled_imports
.iter()
.map(|(instance, funcs)| {
(
instance.clone(),
funcs
.iter()
.filter_map(|(name, func)| {
match func {
DynamicFunction::Static { results, .. } => {
Some((name.clone(), results.clone()))
}
// We do not support method imports (on resources) at this time.
DynamicFunction::Method { .. } => None,
}
})
.collect::<HashMap<_, _>>(),
)
})
.collect::<HashMap<_, _>>();

let imports = get_import_results(&component);
let handler = Handler {
nats: Arc::clone(&self.rpc_nats),
config_data: Arc::new(RwLock::new(config)),
Expand Down Expand Up @@ -2735,14 +2714,16 @@ impl Host {
}

let max = actor.max_instances;
let mut handler = actor.handler.clone();
handler.polyfilled_imports = get_import_results(&new_actor);
let Ok(new_actor) = self
.instantiate_actor(
&annotations,
new_actor_ref.to_string(),
actor_id.to_string(),
max,
new_actor.clone(),
actor.handler.clone(),
handler,
)
.await
else {
Expand Down Expand Up @@ -4330,6 +4311,33 @@ fn injector_to_headers(injector: &TraceContextInjector) -> async_nats::header::H
.collect()
}

fn get_import_results(
component: &wasmcloud_runtime::Component,
) -> HashMap<String, HashMap<String, Arc<[wrpc_types::Type]>>> {
let polyfilled_imports = component.polyfilled_imports().clone();
// Map the imports to pull out the result types of the functions for lookup when invoking them
polyfilled_imports
.iter()
.map(|(instance, funcs)| {
(
instance.clone(),
funcs
.iter()
.filter_map(|(name, func)| {
match func {
DynamicFunction::Static { results, .. } => {
Some((name.clone(), results.clone()))
}
// We do not support method imports (on resources) at this time.
DynamicFunction::Method { .. } => None,
}
})
.collect::<HashMap<_, _>>(),
)
})
.collect()
}

#[cfg(test)]
mod test {
// Ensure that the helper function to translate a list of links into a map of imports works as expected
Expand Down

0 comments on commit 5b4f75b

Please sign in to comment.