Skip to content

Commit 69fe11e

Browse files
committed
refactor: creates instead of delete
Signed-off-by: Aminu 'Seun Joshua <[email protected]>
1 parent 9d09742 commit 69fe11e

File tree

4 files changed

+51
-23
lines changed

4 files changed

+51
-23
lines changed

crates/runtime-config/src/lib.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ where
9999
local_app_dir: Option<PathBuf>,
100100
provided_state_dir: UserProvidedPath,
101101
provided_log_dir: UserProvidedPath,
102-
refresh_logs: bool,
103102
) -> anyhow::Result<Self> {
104103
let toml = match runtime_config_path {
105104
Some(runtime_config_path) => {
@@ -121,14 +120,13 @@ where
121120
let toml_resolver =
122121
TomlResolver::new(&toml, local_app_dir, provided_state_dir, provided_log_dir);
123122

124-
Self::new(toml_resolver, runtime_config_path, refresh_logs)
123+
Self::new(toml_resolver, runtime_config_path)
125124
}
126125

127126
/// Creates a new resolved runtime configuration from a TOML table.
128127
pub fn new(
129128
toml_resolver: TomlResolver<'_>,
130129
runtime_config_path: Option<&Path>,
131-
refresh_logs: bool,
132130
) -> anyhow::Result<Self> {
133131
let runtime_config_dir = runtime_config_path
134132
.and_then(Path::parent)
@@ -144,10 +142,6 @@ where
144142
let toml = toml_resolver.toml();
145143
let log_dir = toml_resolver.log_dir()?;
146144

147-
if refresh_logs {
148-
let _ = Self::resolve_log_dir(&log_dir);
149-
}
150-
151145
let max_instance_memory = toml_resolver.max_instance_memory()?;
152146

153147
let source = TomlRuntimeConfigSource::new(
@@ -173,14 +167,6 @@ where
173167
})
174168
}
175169

176-
fn resolve_log_dir(log_dir: &Option<PathBuf>) -> std::io::Result<()> {
177-
if let Some(path) = log_dir {
178-
std::fs::remove_dir_all(path)
179-
} else {
180-
Ok(())
181-
}
182-
}
183-
184170
/// The fully resolved state directory.
185171
pub fn state_dir(&self) -> Option<PathBuf> {
186172
self.state_dir.clone()
@@ -508,7 +494,6 @@ mod tests {
508494
ResolvedRuntimeConfig::<TestFactorsRuntimeConfig>::new(
509495
toml_resolver(&toml),
510496
Some(path.as_ref()),
511-
false,
512497
)
513498
}
514499
};

crates/runtime-factors/src/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ impl RuntimeFactorsBuilder for FactorsBuilder {
2828
config.local_app_dir.clone().map(PathBuf::from),
2929
config.state_dir.clone(),
3030
config.log_dir.clone(),
31-
config.refresh_logs,
3231
)?;
3332

3433
runtime_config.summarize(config.runtime_config_file.as_deref());
@@ -51,6 +50,7 @@ impl RuntimeFactorsBuilder for FactorsBuilder {
5150
executor.add_hooks(StdioLoggingExecutorHooks::new(
5251
config.follow_components.clone(),
5352
runtime_config.log_dir(),
53+
config.refresh_logs,
5454
));
5555
executor.add_hooks(SqlStatementExecutorHook::new(
5656
args.sqlite_statements.clone(),

crates/trigger/src/cli.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ pub struct FactorsConfig {
147147
pub follow_components: FollowComponents,
148148
/// Log directory for component stdout/stderr.
149149
pub log_dir: UserProvidedPath,
150-
/// Whether to refresh logs when restarted.
150+
/// If set, Spin deletes the log files before starting the application.
151151
pub refresh_logs: bool,
152152
}
153153

crates/trigger/src/cli/stdio.rs

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ use spin_factors::RuntimeFactors;
1212
use spin_factors_executor::ExecutorHooks;
1313
use tokio::io::AsyncWrite;
1414

15+
pub const STDOUT_LOG_FILE_SUFFIX: &str = "stdout";
16+
pub const STDERR_LOG_FILE_SUFFIX: &str = "stderr";
17+
1518
/// Which components should have their logs followed on stdout/stderr.
1619
#[derive(Clone, Debug, Default)]
1720
pub enum FollowComponents {
@@ -39,13 +42,19 @@ impl FollowComponents {
3942
pub struct StdioLoggingExecutorHooks {
4043
follow_components: FollowComponents,
4144
log_dir: Option<PathBuf>,
45+
refresh_log: bool,
4246
}
4347

4448
impl StdioLoggingExecutorHooks {
45-
pub fn new(follow_components: FollowComponents, log_dir: Option<PathBuf>) -> Self {
49+
pub fn new(
50+
follow_components: FollowComponents,
51+
log_dir: Option<PathBuf>,
52+
refresh_log: bool,
53+
) -> Self {
4654
Self {
4755
follow_components,
4856
log_dir,
57+
refresh_log,
4958
}
5059
}
5160

@@ -56,8 +65,7 @@ impl StdioLoggingExecutorHooks {
5665
log_dir: Option<&Path>,
5766
) -> Result<ComponentStdioWriter> {
5867
let sanitized_component_id = sanitize_filename::sanitize(component_id);
59-
let log_path = log_dir
60-
.map(|log_dir| log_dir.join(format!("{sanitized_component_id}_{log_suffix}.txt",)));
68+
let log_path = santized_log_path(log_dir, &sanitized_component_id, log_suffix);
6169
let log_path = log_path.as_deref();
6270

6371
let follow = self.follow_components.should_follow(component_id);
@@ -95,6 +103,33 @@ impl<F: RuntimeFactors, U> ExecutorHooks<F, U> for StdioLoggingExecutorHooks {
95103
configured_app: &spin_factors::ConfiguredApp<F>,
96104
) -> anyhow::Result<()> {
97105
self.validate_follows(configured_app.app())?;
106+
107+
if self.refresh_log {
108+
let sanitized_component_ids: Vec<String> = configured_app
109+
.app()
110+
.components()
111+
.map(|c| sanitize_filename::sanitize(c.locked.id.clone()))
112+
.collect();
113+
114+
for sanitized_component_id in sanitized_component_ids {
115+
if let Some(stdout_log_path) = santized_log_path(
116+
self.log_dir.as_deref(),
117+
&sanitized_component_id,
118+
STDOUT_LOG_FILE_SUFFIX,
119+
) {
120+
let _ = std::fs::File::create(stdout_log_path);
121+
}
122+
123+
if let Some(stderr_log_path) = santized_log_path(
124+
self.log_dir.as_deref(),
125+
&sanitized_component_id,
126+
STDERR_LOG_FILE_SUFFIX,
127+
) {
128+
let _ = std::fs::File::create(stderr_log_path);
129+
}
130+
}
131+
}
132+
98133
if let Some(dir) = &self.log_dir {
99134
// Ensure log dir exists if set
100135
std::fs::create_dir_all(dir)
@@ -115,12 +150,12 @@ impl<F: RuntimeFactors, U> ExecutorHooks<F, U> for StdioLoggingExecutorHooks {
115150
};
116151
wasi_builder.stdout_pipe(self.component_stdio_writer(
117152
&component_id,
118-
"stdout",
153+
STDOUT_LOG_FILE_SUFFIX,
119154
self.log_dir.as_deref(),
120155
)?);
121156
wasi_builder.stderr_pipe(self.component_stdio_writer(
122157
&component_id,
123-
"stderr",
158+
STDERR_LOG_FILE_SUFFIX,
124159
self.log_dir.as_deref(),
125160
)?);
126161
Ok(())
@@ -328,3 +363,11 @@ fn bullet_list<S: std::fmt::Display>(items: impl IntoIterator<Item = S>) -> Stri
328363
.collect::<Vec<_>>()
329364
.join("\n")
330365
}
366+
367+
fn santized_log_path(
368+
log_dir: Option<&Path>,
369+
sanitized_component_id: &str,
370+
log_suffix: &str,
371+
) -> Option<PathBuf> {
372+
log_dir.map(|log_dir| log_dir.join(format!("{sanitized_component_id}_{log_suffix}.txt",)))
373+
}

0 commit comments

Comments
 (0)