diff --git a/containerd-shim-spin/src/engine.rs b/containerd-shim-spin/src/engine.rs index f35b003..961037f 100644 --- a/containerd-shim-spin/src/engine.rs +++ b/containerd-shim-spin/src/engine.rs @@ -1,5 +1,5 @@ use std::{ - collections::hash_map::DefaultHasher, + collections::{hash_map::DefaultHasher, HashSet}, env, hash::{Hash, Hasher}, }; @@ -137,28 +137,24 @@ impl SpinEngine { configure_application_variables_from_environment_variables(&locked_app)?; let trigger_cmds = get_supported_triggers(&locked_app) .with_context(|| format!("Couldn't find trigger executor for {app_source:?}"))?; - let _telemetry_guard = spin_telemetry::init(version!().to_string())?; - self.run_trigger( - ctx, - trigger_cmds.iter().map(|s| s.as_ref()).collect(), - locked_app, - app_source, - ) - .await + self.run_trigger(ctx, &trigger_cmds, locked_app, app_source) + .await } async fn run_trigger( &self, ctx: &impl RuntimeContext, - trigger_types: Vec<&str>, + trigger_types: &HashSet, app: LockedApp, app_source: Source, ) -> Result<()> { - let mut futures_list = Vec::with_capacity(trigger_types.len()); + let mut futures_list = Vec::new(); + let mut trigger_type_map = Vec::new(); + for trigger_type in trigger_types.iter() { - let f = match trigger_type.to_owned() { + let f = match trigger_type.as_str() { HttpTrigger::TRIGGER_TYPE => { let http_trigger = build_trigger::(app.clone(), app_source.clone()).await?; @@ -204,17 +200,17 @@ impl SpinEngine { } }; - futures_list.push(f) + trigger_type_map.push(trigger_type.clone()); + futures_list.push(f); } info!(" >>> notifying main thread we are about to start"); // exit as soon as any of the trigger completes/exits let (result, index, rest) = future::select_all(futures_list).await; - info!( - " >>> trigger type '{trigger_type}' exited", - trigger_type = trigger_types[index] - ); + let trigger_type = &trigger_type_map[index]; + + info!(" >>> trigger type '{trigger_type}' exited"); drop(rest); diff --git a/containerd-shim-spin/src/trigger.rs b/containerd-shim-spin/src/trigger.rs index d4b3824..efd18ca 100644 --- a/containerd-shim-spin/src/trigger.rs +++ b/containerd-shim-spin/src/trigger.rs @@ -91,7 +91,9 @@ async fn write_locked_app(locked_app: &LockedApp, working_dir: &Path) -> Result< /// - sqs /// - mqtt /// - command -pub(crate) fn get_supported_triggers(locked_app: &LockedApp) -> anyhow::Result> { +/// +/// Note: this function returns a `HashSet` of supported trigger types. Duplicates are removed. +pub(crate) fn get_supported_triggers(locked_app: &LockedApp) -> anyhow::Result> { let supported_triggers: HashSet<&str> = HashSet::from([ RedisTrigger::TRIGGER_TYPE, HttpTrigger::TRIGGER_TYPE, @@ -100,17 +102,17 @@ pub(crate) fn get_supported_triggers(locked_app: &LockedApp) -> anyhow::Result = Vec::with_capacity(locked_app.triggers.len()); - - for trigger in &locked_app.triggers { - let trigger_type = &trigger.trigger_type; - if !supported_triggers.contains(trigger_type.as_str()) { - anyhow::bail!( - "Only Http, Redis, MQTT, SQS, and Command triggers are currently supported. Found unsupported trigger: {:?}", - trigger_type - ); - } - types.push(trigger_type.clone()); - } - Ok(types) + locked_app.triggers.iter() + .map(|trigger| { + let trigger_type = &trigger.trigger_type; + if !supported_triggers.contains(trigger_type.as_str()) { + Err(anyhow!( + "Only Http, Redis, MQTT, SQS, and Command triggers are currently supported. Found unsupported trigger: {:?}", + trigger_type + )) + } else { + Ok(trigger_type.clone()) + } + }) + .collect::>>() }