Skip to content
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

chore: Change daemon log location to be in repo #7468

Merged
merged 3 commits into from Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 36 additions & 3 deletions crates/turborepo-lib/src/commands/daemon.rs
Expand Up @@ -123,10 +123,14 @@ pub async fn daemon_client(command: &DaemonCommand, base: &CommandBase) -> Resul
}
}
DaemonCommand::Logs => {
let mut client = connector.connect().await?;
let status = client.status().await?;
let log_file = log_filename(&status.log_file)?;
let log_file = if let Ok(log_file) = get_log_file_from_daemon(connector).await {
log_file
} else {
get_log_file_from_folder(base).await?
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need the fallback?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I found it really annoying that you can't see logs when the daemon has crashed

Copy link
Contributor

Choose a reason for hiding this comment

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

I think I commented on the wrong line. I meant, do we need the global location ?

};

let tail = which("tail").map_err(|_| DaemonError::TailNotInstalled)?;

std::process::Command::new(tail)
.arg("-f")
.arg(log_file)
Expand Down Expand Up @@ -158,6 +162,35 @@ pub async fn daemon_client(command: &DaemonCommand, base: &CommandBase) -> Resul
Ok(())
}

async fn get_log_file_from_daemon(connector: DaemonConnector) -> Result<String, DaemonError> {
let mut client = connector.connect().await?;
let status = client.status().await?;
Ok(log_filename(&status.log_file)?)
}

async fn get_log_file_from_folder(base: &CommandBase) -> Result<String, DaemonError> {
warn!("couldn't connect to daemon, looking for old log files");
let log_folder = base.repo_root.join_components(&[".turbo", "daemon"]);
let Ok(dir) = std::fs::read_dir(log_folder) else {
return Err(DaemonError::LogFileNotFound);
};

let (latest_file, _) = dir
.flatten()
.filter_map(|entry| {
let modified_time = entry.metadata().ok()?.modified().ok()?;
Some((entry, modified_time))
})
.max_by(|(_, mt1), (_, mt2)| mt1.cmp(mt2))
.ok_or(DaemonError::LogFileNotFound)?;

Ok(latest_file
.path()
.to_str()
.expect("log file should be utf-8")
.to_string())
}

async fn clean(
pid_file: &AbsoluteSystemPath,
sock_file: &AbsoluteSystemPath,
Expand Down
3 changes: 3 additions & 0 deletions crates/turborepo-lib/src/daemon/client.rs
Expand Up @@ -218,6 +218,9 @@ pub enum DaemonError {

#[error("`tail` is not installed. Please install it to use this feature.")]
TailNotInstalled,

#[error("could not find log file")]
LogFileNotFound,
}

impl From<Status> for DaemonError {
Expand Down
15 changes: 6 additions & 9 deletions crates/turborepo-lib/src/daemon/mod.rs
Expand Up @@ -57,14 +57,11 @@ fn daemon_file_root(repo_hash: &str) -> AbsoluteSystemPathBuf {
.join_component(repo_hash)
}

fn daemon_log_file_and_folder(repo_hash: &str) -> (AbsoluteSystemPathBuf, AbsoluteSystemPathBuf) {
let directories = directories::ProjectDirs::from("com", "turborepo", "turborepo")
.expect("user has a home dir");

let folder = AbsoluteSystemPathBuf::new(directories.data_dir().to_str().expect("UTF-8 path"))
.expect("absolute");

let log_folder = folder.join_component("logs");
fn daemon_log_file_and_folder(
repo_root: &AbsoluteSystemPath,
repo_hash: &str,
) -> (AbsoluteSystemPathBuf, AbsoluteSystemPathBuf) {
let log_folder = repo_root.join_components(&[".turbo", "daemon"]);
let log_file = log_folder.join_component(format!("{}-turbo.log", repo_hash).as_str());

(log_file, log_folder)
Expand All @@ -74,7 +71,7 @@ impl Paths {
pub fn from_repo_root(repo_root: &AbsoluteSystemPath) -> Self {
let repo_hash = repo_hash(repo_root);
let daemon_root = daemon_file_root(&repo_hash);
let (log_file, log_folder) = daemon_log_file_and_folder(&repo_hash);
let (log_file, log_folder) = daemon_log_file_and_folder(repo_root, &repo_hash);
Self {
pid_file: daemon_root.join_component("turbod.pid"),
lock_file: daemon_root.join_component("turbod.lock"),
Expand Down