Skip to content

Commit 4753301

Browse files
committed
Feed runtime all the way through
Signed-off-by: Ryan Levick <ryan.levick@fermyon.com>
1 parent 98027a2 commit 4753301

File tree

8 files changed

+103
-102
lines changed

8 files changed

+103
-102
lines changed

Diff for: Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ spin-common = { path = "crates/common" }
4949
spin-doctor = { path = "crates/doctor" }
5050
spin-expressions = { path = "crates/expressions" }
5151
spin-factors = { path = "crates/factors" }
52+
spin-factors-executor = { path = "crates/factors-executor" }
5253
spin-http = { path = "crates/http" }
5354
spin-loader = { path = "crates/loader" }
5455
spin-locked-app = { path = "crates/locked-app" }

Diff for: crates/runtime-config/src/lib.rs

+31-53
Original file line numberDiff line numberDiff line change
@@ -52,73 +52,43 @@ where
5252
T: for<'a, 'b> TryFrom<TomlRuntimeConfigSource<'a, 'b>>,
5353
for<'a, 'b> <T as TryFrom<TomlRuntimeConfigSource<'a, 'b>>>::Error: Into<anyhow::Error>,
5454
{
55-
/// Creates a new resolved runtime configuration from an optional runtime config source TOML file.
56-
pub fn from_optional_file(
57-
runtime_config_path: Option<&Path>,
58-
local_app_dir: Option<PathBuf>,
59-
provided_state_dir: UserProvidedPath,
60-
provided_log_dir: UserProvidedPath,
61-
use_gpu: bool,
62-
) -> anyhow::Result<Self> {
63-
match runtime_config_path {
64-
Some(runtime_config_path) => Self::from_file(
65-
runtime_config_path,
66-
local_app_dir,
67-
provided_state_dir,
68-
provided_log_dir,
69-
use_gpu,
70-
),
71-
None => Self::new(
72-
Default::default(),
73-
None,
74-
local_app_dir,
75-
provided_state_dir,
76-
provided_log_dir,
77-
use_gpu,
78-
),
79-
}
80-
}
81-
8255
/// Creates a new resolved runtime configuration from a runtime config source TOML file.
8356
///
8457
/// `provided_state_dir` is the explicitly provided state directory, if any.
8558
pub fn from_file(
86-
runtime_config_path: &Path,
59+
runtime_config_path: Option<&Path>,
8760
local_app_dir: Option<PathBuf>,
8861
provided_state_dir: UserProvidedPath,
8962
provided_log_dir: UserProvidedPath,
9063
use_gpu: bool,
9164
) -> anyhow::Result<Self> {
92-
let file = std::fs::read_to_string(runtime_config_path).with_context(|| {
93-
format!(
94-
"failed to read runtime config file '{}'",
95-
runtime_config_path.display()
96-
)
97-
})?;
98-
let toml = toml::from_str(&file).with_context(|| {
99-
format!(
100-
"failed to parse runtime config file '{}' as toml",
101-
runtime_config_path.display()
102-
)
103-
})?;
104-
105-
Self::new(
106-
toml,
107-
Some(runtime_config_path),
108-
local_app_dir,
109-
provided_state_dir,
110-
provided_log_dir,
111-
use_gpu,
112-
)
65+
let toml = match runtime_config_path {
66+
Some(runtime_config_path) => {
67+
let file = std::fs::read_to_string(runtime_config_path).with_context(|| {
68+
format!(
69+
"failed to read runtime config file '{}'",
70+
runtime_config_path.display()
71+
)
72+
})?;
73+
toml::from_str(&file).with_context(|| {
74+
format!(
75+
"failed to parse runtime config file '{}' as toml",
76+
runtime_config_path.display()
77+
)
78+
})?
79+
}
80+
None => Default::default(),
81+
};
82+
let toml_resolver =
83+
TomlResolver::new(&toml, local_app_dir, provided_state_dir, provided_log_dir);
84+
85+
Self::new(toml_resolver, runtime_config_path, use_gpu)
11386
}
11487

11588
/// Creates a new resolved runtime configuration from a TOML table.
11689
pub fn new(
117-
toml: toml::Table,
90+
toml_resolver: TomlResolver<'_>,
11891
runtime_config_path: Option<&Path>,
119-
local_app_dir: Option<PathBuf>,
120-
provided_state_dir: UserProvidedPath,
121-
provided_log_dir: UserProvidedPath,
12292
use_gpu: bool,
12393
) -> anyhow::Result<Self> {
12494
let runtime_config_dir = runtime_config_path
@@ -189,6 +159,14 @@ where
189159
}
190160
}
191161

162+
impl From<ResolvedRuntimeConfig<spin_trigger::TriggerFactorsRuntimeConfig>>
163+
for spin_trigger::TriggerFactorsRuntimeConfig
164+
{
165+
fn from(value: ResolvedRuntimeConfig<spin_trigger::TriggerFactorsRuntimeConfig>) -> Self {
166+
value.runtime_config
167+
}
168+
}
169+
192170
#[derive(Clone, Debug)]
193171
/// Resolves runtime configuration from a TOML file.
194172
pub struct TomlResolver<'a> {

Diff for: crates/sqlite/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use tokio::sync::OnceCell;
2020
///
2121
/// This type implements how Spin CLI's SQLite implementation is configured
2222
/// through the runtime config toml as well as the behavior of the "default" label.
23+
#[derive(Clone, Debug)]
2324
pub struct RuntimeConfigResolver {
2425
default_database_dir: Option<PathBuf>,
2526
local_database_dir: PathBuf,

Diff for: crates/trigger/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ spin-factor-variables = { path = "../factor-variables" }
3939
spin-factor-wasi = { path = "../factor-wasi" }
4040
spin-factors = { path = "../factors" }
4141
spin-factors-executor = { path = "../factors-executor" }
42+
spin-runtime-config = { path = "../runtime-config" }
4243
spin-telemetry = { path = "../telemetry" }
4344
terminal = { path = "../terminal" }
4445
tokio = { version = "1.23", features = ["fs", "rt"] }

Diff for: crates/trigger/src/cli.rs

+21-21
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod summary;
55
use std::future::Future;
66
use std::path::PathBuf;
77

8+
pub use crate::stdio::StdioLoggingExecutorHooks;
89
use anyhow::{Context, Result};
910
use clap::{Args, IntoApp, Parser};
1011
use spin_app::App;
@@ -14,12 +15,10 @@ use spin_common::url::parse_file_url;
1415
use spin_core::async_trait;
1516
use spin_factors::RuntimeFactors;
1617
use spin_factors_executor::{ComponentLoader, FactorsExecutor};
17-
use sqlite_statements::SqlStatementExecutorHook;
18-
use summary::{
19-
summarize_runtime_config, KeyValueDefaultStoreSummaryHook, SqliteDefaultStoreSummaryHook,
20-
};
18+
pub use sqlite_statements::SqlStatementExecutorHook;
19+
pub use summary::KeyValueDefaultStoreSummaryHook;
2120

22-
use crate::stdio::{FollowComponents, StdioLoggingExecutorHooks};
21+
use crate::stdio::FollowComponents;
2322
use crate::{Trigger, TriggerApp};
2423
pub use launch_metadata::LaunchMetadata;
2524

@@ -315,7 +314,7 @@ impl<T: Trigger<B::Factors>, B: RuntimeFactorsBuilder> TriggerAppBuilder<T, B> {
315314
};
316315
self.trigger.add_to_linker(core_engine_builder.linker())?;
317316

318-
let (factors, runtime_config) = B::build(common_options, options)?;
317+
let (factors, runtime_config) = B::build(&common_options, &options)?;
319318

320319
// TODO: port the rest of the component loader logic
321320
struct SimpleComponentLoader;
@@ -377,19 +376,12 @@ impl<T: Trigger<B::Factors>, B: RuntimeFactorsBuilder> TriggerAppBuilder<T, B> {
377376
}
378377

379378
let mut executor = FactorsExecutor::new(core_engine_builder, factors)?;
380-
381-
executor.add_hooks(StdioLoggingExecutorHooks::new(
382-
options.follow_components,
383-
log_dir,
384-
));
385-
executor.add_hooks(KeyValueDefaultStoreSummaryHook);
386-
executor.add_hooks(SqliteDefaultStoreSummaryHook);
387-
executor.add_hooks(SqlStatementExecutorHook::new(options.sqlite_statements));
379+
B::configure_app(&mut executor, &runtime_config, &common_options, &options)?;
388380

389381
let configured_app = {
390382
let _sloth_guard = warn_if_wasm_build_slothful();
391383
executor
392-
.load_app(app, runtime_config, SimpleComponentLoader)
384+
.load_app(app, runtime_config.into(), SimpleComponentLoader)
393385
.await?
394386
};
395387

@@ -411,14 +403,22 @@ impl<T: Trigger<B::Factors>, B: RuntimeFactorsBuilder> TriggerAppBuilder<T, B> {
411403
pub trait RuntimeFactorsBuilder {
412404
type Options: clap::Args;
413405
type Factors: RuntimeFactors;
406+
type RuntimeConfig: Into<<Self::Factors as RuntimeFactors>::RuntimeConfig>;
414407

415408
fn build(
416-
common_options: CommonTriggerOptions,
417-
options: Self::Options,
418-
) -> anyhow::Result<(
419-
Self::Factors,
420-
<Self::Factors as RuntimeFactors>::RuntimeConfig,
421-
)>;
409+
common_options: &CommonTriggerOptions,
410+
options: &Self::Options,
411+
) -> anyhow::Result<(Self::Factors, Self::RuntimeConfig)>;
412+
413+
fn configure_app<U: Send + 'static>(
414+
executor: &mut FactorsExecutor<Self::Factors, U>,
415+
runtime_config: &Self::RuntimeConfig,
416+
common_options: &CommonTriggerOptions,
417+
options: &Self::Options,
418+
) -> anyhow::Result<()> {
419+
let _ = (executor, runtime_config, common_options, options);
420+
Ok(())
421+
}
422422
}
423423

424424
pub mod help {

Diff for: crates/trigger/src/factors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ pub struct TriggerAppOptions {
9292
/// default store. Any existing value will be overwritten.
9393
/// Can be used multiple times.
9494
#[clap(long = "key-value", parse(try_from_str = parse_kv))]
95-
key_values: Vec<(String, String)>,
95+
pub key_values: Vec<(String, String)>,
9696

9797
/// Run a SQLite statement such as a migration against the default database.
9898
/// To run from a file, prefix the filename with @ e.g. spin up --sqlite @migration.sql
9999
#[clap(long = "sqlite")]
100-
sqlite_statements: Vec<String>,
100+
pub sqlite_statements: Vec<String>,
101101
}

Diff for: src/bin/spin.rs

+45-26
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ use spin_cli::commands::{
1717
watch::WatchCommand,
1818
};
1919
use spin_cli::{build_info::*, subprocess::ExitStatusError};
20+
use spin_factors_executor::FactorsExecutor;
2021
use spin_runtime_config::ResolvedRuntimeConfig;
2122
use spin_trigger::cli::help::HelpArgsOnlyTrigger;
22-
use spin_trigger::cli::{CommonTriggerOptions, FactorsTriggerCommand, RuntimeFactorsBuilder};
23+
use spin_trigger::cli::{
24+
CommonTriggerOptions, FactorsTriggerCommand, KeyValueDefaultStoreSummaryHook,
25+
RuntimeFactorsBuilder, SqlStatementExecutorHook, StdioLoggingExecutorHooks,
26+
};
2327
use spin_trigger::{TriggerAppOptions, TriggerFactors, TriggerFactorsRuntimeConfig};
2428
use spin_trigger_http::HttpTrigger;
2529
use spin_trigger_redis::RedisTrigger;
@@ -177,42 +181,57 @@ struct Builder;
177181
impl RuntimeFactorsBuilder for Builder {
178182
type Options = TriggerAppOptions;
179183
type Factors = TriggerFactors;
184+
type RuntimeConfig = ResolvedRuntimeConfig<TriggerFactorsRuntimeConfig>;
180185

181186
fn build(
182-
common_options: CommonTriggerOptions,
183-
options: Self::Options,
184-
) -> anyhow::Result<(
185-
Self::Factors,
186-
<Self::Factors as spin_factors::RuntimeFactors>::RuntimeConfig,
187-
)> {
188-
let runtime_config_path = common_options.runtime_config_file;
189-
let local_app_dir = common_options.local_app_dir.map(PathBuf::from);
187+
common_options: &CommonTriggerOptions,
188+
options: &Self::Options,
189+
) -> anyhow::Result<(Self::Factors, Self::RuntimeConfig)> {
190+
let runtime_config_path = common_options.runtime_config_file.clone();
191+
let local_app_dir = common_options.local_app_dir.clone().map(PathBuf::from);
190192
// Hardcode `use_gpu` to true for now
191193
let use_gpu = true;
192-
let runtime_config =
193-
ResolvedRuntimeConfig::<TriggerFactorsRuntimeConfig>::from_optional_file(
194-
runtime_config_path.as_deref(),
195-
local_app_dir,
196-
common_options.state_dir,
197-
common_options.log_dir,
198-
use_gpu,
199-
)?;
200-
201-
// runtime_config
202-
// .set_initial_key_values(&options.initial_key_values)
203-
// .await?;
194+
let runtime_config = ResolvedRuntimeConfig::<TriggerFactorsRuntimeConfig>::from_file(
195+
runtime_config_path.as_deref(),
196+
local_app_dir,
197+
common_options.state_dir.clone(),
198+
common_options.log_dir.clone(),
199+
use_gpu,
200+
)?;
204201

205-
let log_dir = runtime_config.log_dir();
206202
let factors = TriggerFactors::new(
207203
runtime_config.state_dir(),
208-
common_options.working_dir,
204+
common_options.working_dir.clone(),
209205
options.allow_transient_write,
210-
runtime_config.key_value_resolver,
211-
runtime_config.sqlite_resolver,
206+
runtime_config.key_value_resolver.clone(),
207+
runtime_config.sqlite_resolver.clone(),
212208
use_gpu,
213209
)
214210
.context("failed to create factors")?;
215-
Ok((factors, runtime_config.runtime_config))
211+
Ok((factors, runtime_config))
212+
}
213+
214+
fn configure_app<U: Send + 'static>(
215+
executor: &mut FactorsExecutor<Self::Factors, U>,
216+
runtime_config: &Self::RuntimeConfig,
217+
common_options: &CommonTriggerOptions,
218+
options: &Self::Options,
219+
) -> anyhow::Result<()> {
220+
executor.add_hooks(SqlStatementExecutorHook::new(
221+
options.sqlite_statements.clone(),
222+
));
223+
executor.add_hooks(StdioLoggingExecutorHooks::new(
224+
common_options.follow_components.clone(),
225+
runtime_config.log_dir(),
226+
));
227+
executor.add_hooks(KeyValueDefaultStoreSummaryHook);
228+
// TODO: implement initial key values as a hook
229+
// runtime_config
230+
// .set_initial_key_values(&options.initial_key_values)
231+
// .await?;
232+
// builder.hooks(SummariseRuntimeConfigHook::new(&self.runtime_config_file));
233+
// builder.hooks(SqlitePersistenceMessageHook);
234+
Ok(())
216235
}
217236
}
218237

0 commit comments

Comments
 (0)