Skip to content

added flag for clearing log files at restart #3203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions crates/runtime-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ where
local_app_dir: Option<PathBuf>,
provided_state_dir: UserProvidedPath,
provided_log_dir: UserProvidedPath,
refresh_logs: bool,
) -> anyhow::Result<Self> {
let toml = match runtime_config_path {
Some(runtime_config_path) => {
Expand All @@ -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)
}
Expand Down Expand Up @@ -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,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit strange to see this in a struct called TomlResolver - it's not like it has anything to do with resolving TOML. But I guess maybe the log directory is already non-TOML-related and the struct has just grown beyond its original name? Not sure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somewhat, yes.

All but refresh_logs still source the default value from table: TomlKeyTracker<'a>, so the initial name makes sense. However, with other elements added, a change of name would be better, something like RuntimeConfigurationResolver. Would that be fine, or what would you suggest?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should have come back to this comment after writing the one about the location of the delete code - sorry.

We should first figure out a good place for the delete code to live, whether in this struct impl or elsewhere. Then that will guide us as to whether this flag needs to live here or not.

}

impl<'a> TomlResolver<'a> {
Expand All @@ -201,12 +209,14 @@ impl<'a> TomlResolver<'a> {
local_app_dir: Option<PathBuf>,
state_dir: UserProvidedPath,
log_dir: UserProvidedPath,
refresh_logs: bool,
) -> Self {
Self {
table: TomlKeyTracker::new(table),
local_app_dir,
state_dir,
log_dir,
refresh_logs,
}
}

Expand Down Expand Up @@ -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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to end up duplicating the check and the deletion code. It seems like something you could do outside the match, if the match successfully resolves a path.

But I see this is in a function whose job is to "get the configured log directory." Getting path to a directory should not have a potential side effect of deleting that directory - that's super surprising behaviour.

(Similarly, notice that this function does not create the directory. It just gets the path.)

So I think this code needs to move somewhere else, at which point hopefully we will know where the log directory is (if we have one) and can clear it without resorting to repeated guards or code.


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),
}
Expand Down Expand Up @@ -619,6 +639,7 @@ mod tests {
None,
UserProvidedPath::Default,
UserProvidedPath::Default,
false,
)
}

Expand Down
1 change: 1 addition & 0 deletions crates/runtime-factors/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
12 changes: 12 additions & 0 deletions crates/trigger/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -54,6 +55,14 @@ pub struct FactorsTriggerCommand<T: Trigger<B::Factors>, B: RuntimeFactorsBuilde
)]
pub log: Option<PathBuf>,

/// Whether to refresh logs when restarted.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found the word "refresh" a bit odd here, although I can see the connection when I squint. At least in help text, I think we should be more explicit about what the flag does, e.g.

Suggested change
/// Whether to refresh logs when restarted.
/// If set, Spin deletes the log files before starting the application.

(or 'clears the log files'? Not sure which is better)

#[clap(
name = REFRESH_LOGS,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe consider a SPIN_ prefix for this? Going by the original issue, some folks will be treating this as a user preference and setting it in .profile rather than on the spin up command line, so it would be good to have it make sense out of the Spin context perhaps?

(sorry forgot to think of this while reviewing)

short = 'r',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't spend a valuable shortcode on this.

long = "refresh-logs",
)]
pub refresh_logs: bool,

/// Disable Wasmtime cache.
#[clap(
name = DISABLE_WASMTIME_CACHE,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -220,6 +231,7 @@ impl<T: Trigger<B::Factors>, B: RuntimeFactorsBuilder> FactorsTriggerCommand<T,
local_app_dir: local_app_dir.clone(),
follow_components,
log_dir,
refresh_logs: self.refresh_logs,
};

let run_fut = builder
Expand Down
Loading