Skip to content

Commit

Permalink
Pass loader into TriggerAppBuilder
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Levick <ryan.levick@fermyon.com>
  • Loading branch information
rylev committed Sep 17, 2024
1 parent 49694dd commit b2692de
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion crates/componentize/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ mod tests {
let out_dir = PathBuf::from(std::env::var_os("OUT_DIR").unwrap());
let mut cmd = process::Command::new("cargo");
cmd.arg("build")
.current_dir(&format!("tests/{name}"))
.current_dir(format!("tests/{name}"))
.arg("--release")
.arg("--target=wasm32-wasi")
.env("CARGO_TARGET_DIR", out_dir);
Expand Down
25 changes: 19 additions & 6 deletions crates/trigger/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ use spin_common::sloth;
use spin_common::ui::quoted_path;
use spin_common::url::parse_file_url;
use spin_factors::RuntimeFactors;
use spin_factors_executor::FactorsExecutor;
use spin_factors_executor::{ComponentLoader, FactorsExecutor};

use crate::loader::ComponentLoader;
use crate::{Trigger, TriggerApp};
use crate::{loader::ComponentLoader as ComponentLoaderImpl, Trigger, TriggerApp};
pub use initial_kv_setter::InitialKvSetterHook;
pub use launch_metadata::LaunchMetadata;
pub use sqlite_statements::SqlStatementExecutorHook;
Expand Down Expand Up @@ -220,7 +219,19 @@ impl<T: Trigger<B::Factors>, B: RuntimeFactorsBuilder> FactorsTriggerCommand<T,
follow_components,
log_dir,
};
let run_fut = builder.run(app, common_options, self.builder_args).await?;

// Create a component loader and enable AOT loading if the feature is enabled.
#[allow(unused_mut)]
let mut loader = ComponentLoaderImpl::new();
#[cfg(feature = "unsafe-aot-compilation")]
// Safety: This is safe because the feature is enabled.
unsafe {
loader.enable_loading_aot_compiled_components()
};

let run_fut = builder
.run(app, common_options, self.builder_args, &loader)
.await?;

let (abortable, abort_handle) = futures::future::abortable(run_fut);
ctrlc::set_handler(move || abort_handle.abort())?;
Expand Down Expand Up @@ -302,6 +313,7 @@ impl<T: Trigger<B::Factors>, B: RuntimeFactorsBuilder> TriggerAppBuilder<T, B> {
app: App,
common_options: FactorsConfig,
options: B::CliArgs,
loader: &impl ComponentLoader,
) -> anyhow::Result<TriggerApp<T, B::Factors>> {
let mut core_engine_builder = {
self.trigger.update_core_config(&mut self.engine_config)?;
Expand All @@ -319,7 +331,7 @@ impl<T: Trigger<B::Factors>, B: RuntimeFactorsBuilder> TriggerAppBuilder<T, B> {
let configured_app = {
let _sloth_guard = warn_if_wasm_build_slothful();
executor
.load_app(app, runtime_config.into(), &ComponentLoader::default())
.load_app(app, runtime_config.into(), loader)
.await?
};

Expand All @@ -332,8 +344,9 @@ impl<T: Trigger<B::Factors>, B: RuntimeFactorsBuilder> TriggerAppBuilder<T, B> {
app: App,
common_options: FactorsConfig,
options: B::CliArgs,
loader: &impl ComponentLoader,
) -> anyhow::Result<impl Future<Output = anyhow::Result<()>>> {
let configured_app = self.build(app, common_options, options).await?;
let configured_app = self.build(app, common_options, options, loader).await?;
Ok(self.trigger.run(configured_app))
}
}
Expand Down
6 changes: 6 additions & 0 deletions crates/trigger/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ use spin_factors::AppComponent;

#[derive(Default)]
pub struct ComponentLoader {
_private: (),
#[cfg(feature = "unsafe-aot-compilation")]
aot_compilation_enabled: bool,
}

impl ComponentLoader {
/// Create a new `ComponentLoader`
pub fn new() -> Self {
Self::default()
}

/// Updates the TriggerLoader to load AOT precompiled components
///
/// **Warning: This feature may bypass important security guarantees of the
Expand Down
3 changes: 2 additions & 1 deletion tests/testing-framework/src/runtimes/in_process_spin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::Arc;

use anyhow::Context as _;
use spin_runtime_factors::{FactorsBuilder, TriggerAppArgs, TriggerFactors};
use spin_trigger::cli::TriggerAppBuilder;
use spin_trigger::{cli::TriggerAppBuilder, loader::ComponentLoader};
use spin_trigger_http::{HttpServer, HttpTrigger};
use test_environment::{
http::{Request, Response},
Expand Down Expand Up @@ -111,6 +111,7 @@ async fn initialize_trigger(
app,
spin_trigger::cli::FactorsConfig::default(),
TriggerAppArgs::default(),
&ComponentLoader::new(),
)
.await?;
let server = builder.trigger.into_server(trigger_app)?;
Expand Down

0 comments on commit b2692de

Please sign in to comment.