Skip to content

Commit 0a252ba

Browse files
committed
added flag for clearing log files at restart
Signed-off-by: Aminu 'Seun Joshua <[email protected]>
1 parent 2860166 commit 0a252ba

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

crates/runtime-config/src/lib.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ where
9999
local_app_dir: Option<PathBuf>,
100100
provided_state_dir: UserProvidedPath,
101101
provided_log_dir: UserProvidedPath,
102+
refresh_logs: bool,
102103
) -> anyhow::Result<Self> {
103104
let toml = match runtime_config_path {
104105
Some(runtime_config_path) => {
@@ -117,8 +118,13 @@ where
117118
}
118119
None => Default::default(),
119120
};
120-
let toml_resolver =
121-
TomlResolver::new(&toml, local_app_dir, provided_state_dir, provided_log_dir);
121+
let toml_resolver = TomlResolver::new(
122+
&toml,
123+
local_app_dir,
124+
provided_state_dir,
125+
provided_log_dir,
126+
refresh_logs,
127+
);
122128

123129
Self::new(toml_resolver, runtime_config_path)
124130
}
@@ -192,6 +198,8 @@ pub struct TomlResolver<'a> {
192198
state_dir: UserProvidedPath,
193199
/// Explicitly provided log directory.
194200
log_dir: UserProvidedPath,
201+
/// Whether to refresh logs when restarted.
202+
refresh_logs: bool,
195203
}
196204

197205
impl<'a> TomlResolver<'a> {
@@ -201,12 +209,14 @@ impl<'a> TomlResolver<'a> {
201209
local_app_dir: Option<PathBuf>,
202210
state_dir: UserProvidedPath,
203211
log_dir: UserProvidedPath,
212+
refresh_logs: bool,
204213
) -> Self {
205214
Self {
206215
table: TomlKeyTracker::new(table),
207216
local_app_dir,
208217
state_dir,
209218
log_dir,
219+
refresh_logs,
210220
}
211221
}
212222

@@ -268,7 +278,17 @@ impl<'a> TomlResolver<'a> {
268278
}
269279

270280
match log_dir {
281+
UserProvidedPath::Provided(p) if self.refresh_logs => Ok(Some({
282+
let _ = std::fs::remove_dir_all(&p);
283+
284+
std::path::absolute(p)?
285+
})),
271286
UserProvidedPath::Provided(p) => Ok(Some(std::path::absolute(p)?)),
287+
UserProvidedPath::Default if self.refresh_logs => Ok(self.state_dir()?.map(|p| {
288+
let _ = std::fs::remove_dir_all(&p);
289+
290+
p.join("logs")
291+
})),
272292
UserProvidedPath::Default => Ok(self.state_dir()?.map(|p| p.join("logs"))),
273293
UserProvidedPath::Unset => Ok(None),
274294
}
@@ -619,6 +639,7 @@ mod tests {
619639
None,
620640
UserProvidedPath::Default,
621641
UserProvidedPath::Default,
642+
false,
622643
)
623644
}
624645

crates/runtime-factors/src/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ 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,
3132
)?;
3233

3334
runtime_config.summarize(config.runtime_config_file.as_deref());

crates/trigger/src/cli.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +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 REFRESH_LOGS: &str = "REFRESH_LOGS";
3031
pub const DISABLE_WASMTIME_CACHE: &str = "DISABLE_WASMTIME_CACHE";
3132
pub const FOLLOW_LOG_OPT: &str = "FOLLOW_ID";
3233
pub const WASMTIME_CACHE_FILE: &str = "WASMTIME_CACHE_FILE";
@@ -54,6 +55,14 @@ pub struct FactorsTriggerCommand<T: Trigger<B::Factors>, B: RuntimeFactorsBuilde
5455
)]
5556
pub log: Option<PathBuf>,
5657

58+
/// Whether to refresh logs when restarted.
59+
#[clap(
60+
name = REFRESH_LOGS,
61+
short = 'r',
62+
long = "refresh-logs",
63+
)]
64+
pub refresh_logs: bool,
65+
5766
/// Disable Wasmtime cache.
5867
#[clap(
5968
name = DISABLE_WASMTIME_CACHE,
@@ -139,6 +148,8 @@ pub struct FactorsConfig {
139148
pub follow_components: FollowComponents,
140149
/// Log directory for component stdout/stderr.
141150
pub log_dir: UserProvidedPath,
151+
/// Whether to refresh logs when restarted.
152+
pub refresh_logs: bool,
142153
}
143154

144155
/// An empty implementation of clap::Args to be used as TriggerExecutor::RunConfig
@@ -220,6 +231,7 @@ impl<T: Trigger<B::Factors>, B: RuntimeFactorsBuilder> FactorsTriggerCommand<T,
220231
local_app_dir: local_app_dir.clone(),
221232
follow_components,
222233
log_dir,
234+
refresh_logs: self.refresh_logs,
223235
};
224236

225237
let run_fut = builder

0 commit comments

Comments
 (0)