From 0a252ba38261eb9db498d8082f9b01e930d46ab0 Mon Sep 17 00:00:00 2001 From: Aminu 'Seun Joshua Date: Fri, 18 Jul 2025 17:51:15 +0100 Subject: [PATCH 1/2] added flag for clearing log files at restart Signed-off-by: Aminu 'Seun Joshua --- crates/runtime-config/src/lib.rs | 25 +++++++++++++++++++++++-- crates/runtime-factors/src/build.rs | 1 + crates/trigger/src/cli.rs | 12 ++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/crates/runtime-config/src/lib.rs b/crates/runtime-config/src/lib.rs index 8db0b536f..48ecd3e72 100644 --- a/crates/runtime-config/src/lib.rs +++ b/crates/runtime-config/src/lib.rs @@ -99,6 +99,7 @@ where local_app_dir: Option, provided_state_dir: UserProvidedPath, provided_log_dir: UserProvidedPath, + refresh_logs: bool, ) -> anyhow::Result { let toml = match runtime_config_path { Some(runtime_config_path) => { @@ -117,8 +118,13 @@ where } None => Default::default(), }; - let toml_resolver = - TomlResolver::new(&toml, local_app_dir, provided_state_dir, provided_log_dir); + let toml_resolver = TomlResolver::new( + &toml, + local_app_dir, + provided_state_dir, + provided_log_dir, + refresh_logs, + ); Self::new(toml_resolver, runtime_config_path) } @@ -192,6 +198,8 @@ pub struct TomlResolver<'a> { state_dir: UserProvidedPath, /// Explicitly provided log directory. log_dir: UserProvidedPath, + /// Whether to refresh logs when restarted. + refresh_logs: bool, } impl<'a> TomlResolver<'a> { @@ -201,12 +209,14 @@ impl<'a> TomlResolver<'a> { local_app_dir: Option, state_dir: UserProvidedPath, log_dir: UserProvidedPath, + refresh_logs: bool, ) -> Self { Self { table: TomlKeyTracker::new(table), local_app_dir, state_dir, log_dir, + refresh_logs, } } @@ -268,7 +278,17 @@ impl<'a> TomlResolver<'a> { } match log_dir { + UserProvidedPath::Provided(p) if self.refresh_logs => Ok(Some({ + let _ = std::fs::remove_dir_all(&p); + + std::path::absolute(p)? + })), UserProvidedPath::Provided(p) => Ok(Some(std::path::absolute(p)?)), + UserProvidedPath::Default if self.refresh_logs => Ok(self.state_dir()?.map(|p| { + let _ = std::fs::remove_dir_all(&p); + + p.join("logs") + })), UserProvidedPath::Default => Ok(self.state_dir()?.map(|p| p.join("logs"))), UserProvidedPath::Unset => Ok(None), } @@ -619,6 +639,7 @@ mod tests { None, UserProvidedPath::Default, UserProvidedPath::Default, + false, ) } diff --git a/crates/runtime-factors/src/build.rs b/crates/runtime-factors/src/build.rs index b1c837f8a..36730dd51 100644 --- a/crates/runtime-factors/src/build.rs +++ b/crates/runtime-factors/src/build.rs @@ -28,6 +28,7 @@ impl RuntimeFactorsBuilder for FactorsBuilder { config.local_app_dir.clone().map(PathBuf::from), config.state_dir.clone(), config.log_dir.clone(), + config.refresh_logs, )?; runtime_config.summarize(config.runtime_config_file.as_deref()); diff --git a/crates/trigger/src/cli.rs b/crates/trigger/src/cli.rs index db8e80a8f..6aada3d10 100644 --- a/crates/trigger/src/cli.rs +++ b/crates/trigger/src/cli.rs @@ -27,6 +27,7 @@ pub use stdio::StdioLoggingExecutorHooks; pub use summary::{KeyValueDefaultStoreSummaryHook, SqliteDefaultStoreSummaryHook}; pub const APP_LOG_DIR: &str = "APP_LOG_DIR"; +pub const REFRESH_LOGS: &str = "REFRESH_LOGS"; pub const DISABLE_WASMTIME_CACHE: &str = "DISABLE_WASMTIME_CACHE"; pub const FOLLOW_LOG_OPT: &str = "FOLLOW_ID"; pub const WASMTIME_CACHE_FILE: &str = "WASMTIME_CACHE_FILE"; @@ -54,6 +55,14 @@ pub struct FactorsTriggerCommand, B: RuntimeFactorsBuilde )] pub log: Option, + /// Whether to refresh logs when restarted. + #[clap( + name = REFRESH_LOGS, + short = 'r', + long = "refresh-logs", + )] + pub refresh_logs: bool, + /// Disable Wasmtime cache. #[clap( name = DISABLE_WASMTIME_CACHE, @@ -139,6 +148,8 @@ pub struct FactorsConfig { pub follow_components: FollowComponents, /// Log directory for component stdout/stderr. pub log_dir: UserProvidedPath, + /// Whether to refresh logs when restarted. + pub refresh_logs: bool, } /// An empty implementation of clap::Args to be used as TriggerExecutor::RunConfig @@ -220,6 +231,7 @@ impl, B: RuntimeFactorsBuilder> FactorsTriggerCommand Date: Mon, 21 Jul 2025 01:50:34 +0100 Subject: [PATCH 2/2] refactor: renaming + login Signed-off-by: Aminu 'Seun Joshua --- crates/runtime-config/src/lib.rs | 41 ++++++++++++++------------------ crates/trigger/src/cli.rs | 7 +++--- 2 files changed, 21 insertions(+), 27 deletions(-) diff --git a/crates/runtime-config/src/lib.rs b/crates/runtime-config/src/lib.rs index 48ecd3e72..31526f4bf 100644 --- a/crates/runtime-config/src/lib.rs +++ b/crates/runtime-config/src/lib.rs @@ -118,21 +118,17 @@ where } None => Default::default(), }; - let toml_resolver = TomlResolver::new( - &toml, - local_app_dir, - provided_state_dir, - provided_log_dir, - refresh_logs, - ); + let toml_resolver = + TomlResolver::new(&toml, local_app_dir, provided_state_dir, provided_log_dir); - Self::new(toml_resolver, runtime_config_path) + Self::new(toml_resolver, runtime_config_path, refresh_logs) } /// Creates a new resolved runtime configuration from a TOML table. pub fn new( toml_resolver: TomlResolver<'_>, runtime_config_path: Option<&Path>, + refresh_logs: bool, ) -> anyhow::Result { let runtime_config_dir = runtime_config_path .and_then(Path::parent) @@ -147,6 +143,11 @@ where let toml = toml_resolver.toml(); let log_dir = toml_resolver.log_dir()?; + + if refresh_logs { + let _ = Self::resolve_log_dir(&log_dir); + } + let max_instance_memory = toml_resolver.max_instance_memory()?; let source = TomlRuntimeConfigSource::new( @@ -172,6 +173,14 @@ where }) } + fn resolve_log_dir(log_dir: &Option) -> std::io::Result<()> { + if let Some(path) = log_dir { + std::fs::remove_dir_all(path) + } else { + Ok(()) + } + } + /// The fully resolved state directory. pub fn state_dir(&self) -> Option { self.state_dir.clone() @@ -198,8 +207,6 @@ pub struct TomlResolver<'a> { state_dir: UserProvidedPath, /// Explicitly provided log directory. log_dir: UserProvidedPath, - /// Whether to refresh logs when restarted. - refresh_logs: bool, } impl<'a> TomlResolver<'a> { @@ -209,14 +216,12 @@ impl<'a> TomlResolver<'a> { local_app_dir: Option, state_dir: UserProvidedPath, log_dir: UserProvidedPath, - refresh_logs: bool, ) -> Self { Self { table: TomlKeyTracker::new(table), local_app_dir, state_dir, log_dir, - refresh_logs, } } @@ -278,17 +283,7 @@ impl<'a> TomlResolver<'a> { } match log_dir { - UserProvidedPath::Provided(p) if self.refresh_logs => Ok(Some({ - let _ = std::fs::remove_dir_all(&p); - - std::path::absolute(p)? - })), UserProvidedPath::Provided(p) => Ok(Some(std::path::absolute(p)?)), - UserProvidedPath::Default if self.refresh_logs => Ok(self.state_dir()?.map(|p| { - let _ = std::fs::remove_dir_all(&p); - - p.join("logs") - })), UserProvidedPath::Default => Ok(self.state_dir()?.map(|p| p.join("logs"))), UserProvidedPath::Unset => Ok(None), } @@ -513,6 +508,7 @@ mod tests { ResolvedRuntimeConfig::::new( toml_resolver(&toml), Some(path.as_ref()), + false, ) } }; @@ -639,7 +635,6 @@ mod tests { None, UserProvidedPath::Default, UserProvidedPath::Default, - false, ) } diff --git a/crates/trigger/src/cli.rs b/crates/trigger/src/cli.rs index 6aada3d10..72639c819 100644 --- a/crates/trigger/src/cli.rs +++ b/crates/trigger/src/cli.rs @@ -27,7 +27,7 @@ pub use stdio::StdioLoggingExecutorHooks; pub use summary::{KeyValueDefaultStoreSummaryHook, SqliteDefaultStoreSummaryHook}; pub const APP_LOG_DIR: &str = "APP_LOG_DIR"; -pub const REFRESH_LOGS: &str = "REFRESH_LOGS"; +pub const SPIN_REFRESH_LOGS: &str = "SPIN_REFRESH_LOGS"; pub const DISABLE_WASMTIME_CACHE: &str = "DISABLE_WASMTIME_CACHE"; pub const FOLLOW_LOG_OPT: &str = "FOLLOW_ID"; pub const WASMTIME_CACHE_FILE: &str = "WASMTIME_CACHE_FILE"; @@ -55,10 +55,9 @@ pub struct FactorsTriggerCommand, B: RuntimeFactorsBuilde )] pub log: Option, - /// Whether to refresh logs when restarted. + /// If set, Spin deletes the log files before starting the application. #[clap( - name = REFRESH_LOGS, - short = 'r', + name = SPIN_REFRESH_LOGS, long = "refresh-logs", )] pub refresh_logs: bool,