Skip to content

Commit 254bf4d

Browse files
committed
some renaming + cleaner code
Signed-off-by: Aminu 'Seun Joshua <seun.aminujoshua@gmail.com>
1 parent 513c1a7 commit 254bf4d

File tree

3 files changed

+47
-46
lines changed

3 files changed

+47
-46
lines changed

crates/runtime-factors/src/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl RuntimeFactorsBuilder for FactorsBuilder {
5050
executor.add_hooks(StdioLoggingExecutorHooks::new(
5151
config.follow_components.clone(),
5252
runtime_config.log_dir(),
53-
config.refresh_logs,
53+
config.truncate_logs,
5454
));
5555
executor.add_hooks(SqlStatementExecutorHook::new(
5656
args.sqlite_statements.clone(),

crates/trigger/src/cli.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub use stdio::StdioLoggingExecutorHooks;
2727
pub use summary::{KeyValueDefaultStoreSummaryHook, SqliteDefaultStoreSummaryHook};
2828

2929
pub const APP_LOG_DIR: &str = "APP_LOG_DIR";
30-
pub const SPIN_REFRESH_LOGS: &str = "SPIN_REFRESH_LOGS";
30+
pub const SPIN_TRUNCATE_LOGS: &str = "SPIN_TRUNCATE_LOGS";
3131
pub const DISABLE_WASMTIME_CACHE: &str = "DISABLE_WASMTIME_CACHE";
3232
pub const FOLLOW_LOG_OPT: &str = "FOLLOW_ID";
3333
pub const WASMTIME_CACHE_FILE: &str = "WASMTIME_CACHE_FILE";
@@ -55,12 +55,12 @@ pub struct FactorsTriggerCommand<T: Trigger<B::Factors>, B: RuntimeFactorsBuilde
5555
)]
5656
pub log: Option<PathBuf>,
5757

58-
/// If set, Spin deletes the log files before starting the application.
58+
/// If set, Spin truncates the log files before starting the application.
5959
#[clap(
60-
name = SPIN_REFRESH_LOGS,
61-
long = "refresh-logs",
60+
name = SPIN_TRUNCATE_LOGS,
61+
long = "truncate-logs",
6262
)]
63-
pub refresh_logs: bool,
63+
pub truncate_logs: bool,
6464

6565
/// Disable Wasmtime cache.
6666
#[clap(
@@ -147,8 +147,8 @@ pub struct FactorsConfig {
147147
pub follow_components: FollowComponents,
148148
/// Log directory for component stdout/stderr.
149149
pub log_dir: UserProvidedPath,
150-
/// If set, Spin deletes the log files before starting the application.
151-
pub refresh_logs: bool,
150+
/// If set, Spin truncates the log files before starting the application.
151+
pub truncate_logs: bool,
152152
}
153153

154154
/// An empty implementation of clap::Args to be used as TriggerExecutor::RunConfig
@@ -230,7 +230,7 @@ impl<T: Trigger<B::Factors>, B: RuntimeFactorsBuilder> FactorsTriggerCommand<T,
230230
local_app_dir: local_app_dir.clone(),
231231
follow_components,
232232
log_dir,
233-
refresh_logs: self.refresh_logs,
233+
truncate_logs: self.truncate_logs,
234234
};
235235

236236
let run_fut = builder

crates/trigger/src/cli/stdio.rs

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,19 @@ impl FollowComponents {
4242
pub struct StdioLoggingExecutorHooks {
4343
follow_components: FollowComponents,
4444
log_dir: Option<PathBuf>,
45-
refresh_log: bool,
45+
truncate_log: bool,
4646
}
4747

4848
impl StdioLoggingExecutorHooks {
4949
pub fn new(
5050
follow_components: FollowComponents,
5151
log_dir: Option<PathBuf>,
52-
refresh_log: bool,
52+
truncate_log: bool,
5353
) -> Self {
5454
Self {
5555
follow_components,
5656
log_dir,
57-
refresh_log,
57+
truncate_log,
5858
}
5959
}
6060

@@ -65,7 +65,7 @@ impl StdioLoggingExecutorHooks {
6565
log_dir: Option<&Path>,
6666
) -> Result<ComponentStdioWriter> {
6767
let sanitized_component_id = sanitize_filename::sanitize(component_id);
68-
let log_path = santized_log_path(log_dir, &sanitized_component_id, log_suffix);
68+
let log_path = sanitized_log_path(log_dir, &sanitized_component_id, log_suffix);
6969
let log_path = log_path.as_deref();
7070

7171
let follow = self.follow_components.should_follow(component_id);
@@ -94,6 +94,35 @@ impl StdioLoggingExecutorHooks {
9494
_ => Ok(()),
9595
}
9696
}
97+
98+
fn truncate_log_files<F: RuntimeFactors>(
99+
&self,
100+
configured_app: &spin_factors::ConfiguredApp<F>,
101+
) {
102+
let sanitized_component_ids: Vec<String> = configured_app
103+
.app()
104+
.components()
105+
.map(|c| sanitize_filename::sanitize(c.locked.id.clone()))
106+
.collect();
107+
108+
for sanitized_component_id in sanitized_component_ids {
109+
if let Some(stdout_log_path) = sanitized_log_path(
110+
self.log_dir.as_deref(),
111+
&sanitized_component_id,
112+
STDOUT_LOG_FILE_SUFFIX,
113+
) {
114+
let _ = std::fs::File::create(stdout_log_path);
115+
}
116+
117+
if let Some(stderr_log_path) = sanitized_log_path(
118+
self.log_dir.as_deref(),
119+
&sanitized_component_id,
120+
STDERR_LOG_FILE_SUFFIX,
121+
) {
122+
let _ = std::fs::File::create(stderr_log_path);
123+
}
124+
}
125+
}
97126
}
98127

99128
#[async_trait]
@@ -104,43 +133,15 @@ impl<F: RuntimeFactors, U> ExecutorHooks<F, U> for StdioLoggingExecutorHooks {
104133
) -> anyhow::Result<()> {
105134
self.validate_follows(configured_app.app())?;
106135

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-
if let Err(err) = std::fs::File::create(stdout_log_path) {
121-
//TODO: figure out if we want to return error here
122-
tracing::warn!("Failed to refresh stdout log file: {}", err);
123-
}
124-
}
125-
126-
if let Some(stderr_log_path) = santized_log_path(
127-
self.log_dir.as_deref(),
128-
&sanitized_component_id,
129-
STDERR_LOG_FILE_SUFFIX,
130-
) {
131-
if let Err(err) = std::fs::File::create(stderr_log_path) {
132-
//TODO: figure out if we want to return error here
133-
tracing::warn!("Failed to refresh stderr log file: {}", err);
134-
}
135-
}
136-
}
137-
}
138-
139136
if let Some(dir) = &self.log_dir {
140137
// Ensure log dir exists if set
141138
std::fs::create_dir_all(dir)
142139
.with_context(|| format!("Failed to create log dir {}", quoted_path(dir)))?;
143140

141+
if self.truncate_log {
142+
self.truncate_log_files(configured_app);
143+
}
144+
144145
println!("Logging component stdio to {}", quoted_path(dir.join("")))
145146
}
146147
Ok(())
@@ -370,7 +371,7 @@ fn bullet_list<S: std::fmt::Display>(items: impl IntoIterator<Item = S>) -> Stri
370371
.join("\n")
371372
}
372373

373-
fn santized_log_path(
374+
fn sanitized_log_path(
374375
log_dir: Option<&Path>,
375376
sanitized_component_id: &str,
376377
log_suffix: &str,

0 commit comments

Comments
 (0)