Skip to content

Commit

Permalink
Convert log commands to record InvocationRecord
Browse files Browse the repository at this point in the history
Summary: Log commands don't have any InvocationRecord generated.  I would like to track details about who/how invokes log commands. Let's ensure they are written to buck2_builds table with no event log option (so scrapped from scribe?).

Reviewed By: iguridi

Differential Revision: D64044944

fbshipit-source-id: d4ec4a9fe3cc12224f8344dea218abe96a49223c
  • Loading branch information
ezgicicek authored and facebook-github-bot committed Nov 1, 2024
1 parent 78eafb4 commit e5bacef
Show file tree
Hide file tree
Showing 16 changed files with 59 additions and 43 deletions.
1 change: 1 addition & 0 deletions app/buck2_client/src/commands/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ impl CleanCommand {
clean(buck_out_dir, daemon_dir, console, Some(&lifecycle_lock)).await
},
)
.into()
}

pub fn sanitize_argv(&self, argv: Argv) -> SanitizedArgv {
Expand Down
1 change: 1 addition & 0 deletions app/buck2_client/src/commands/kill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ impl KillCommand {
)
.await
})
.into()
}

pub fn sanitize_argv(&self, argv: Argv) -> SanitizedArgv {
Expand Down
1 change: 1 addition & 0 deletions app/buck2_client/src/commands/killall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ impl KillallCommand {
.then_some(())
.ok_or(anyhow::anyhow!("Killall command failed"))
})
.into()
}

pub fn sanitize_argv(&self, argv: Argv) -> SanitizedArgv {
Expand Down
7 changes: 3 additions & 4 deletions app/buck2_client/src/commands/log/critical_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl CriticalPathCommand {
pub fn exec(self, _matches: &clap::ArgMatches, ctx: ClientCommandContext<'_>) -> ExitResult {
let Self { event_log, format } = self;

ctx.with_runtime(|ctx| async move {
ctx.instant_command_no_log("log-critical-path", |ctx| async move {
let log_path = event_log.get(&ctx).await?;

let (invocation, mut events) = log_path.unpack_stream().await?;
Expand Down Expand Up @@ -80,9 +80,8 @@ impl CriticalPathCommand {
}

anyhow::Ok(())
})?;

ExitResult::success()
})
.into()
}
}

Expand Down
7 changes: 3 additions & 4 deletions app/buck2_client/src/commands/log/diff/action_divergence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ fn print_divergence_msg(

impl ActionDivergenceCommand {
pub fn exec(self, _matches: &clap::ArgMatches, ctx: ClientCommandContext<'_>) -> ExitResult {
ctx.with_runtime(|ctx| async move {
ctx.instant_command_no_log("log-diff-action-divergence", |ctx| async move {
let options1 = EventLogOptions {
recent: self.recent1,
path: self.path1,
Expand Down Expand Up @@ -197,8 +197,7 @@ impl ActionDivergenceCommand {
buck2_client_ctx::println!("No divergent actions found.")?;
}
anyhow::Ok(())
})?;

ExitResult::success()
})
.into()
}
}
7 changes: 3 additions & 4 deletions app/buck2_client/src/commands/log/path_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl PathLogCommand {
all,
} = self;

ctx.with_runtime(|ctx| async move {
ctx.instant_command_no_log("log-path", |ctx| async move {
let paths = if all {
retrieve_all_logs(ctx.paths().context("Error identifying log dir")?)?
} else {
Expand All @@ -43,8 +43,7 @@ impl PathLogCommand {
buck2_client_ctx::println!("{}", path.path().display())?;
}
anyhow::Ok(())
})?;

ExitResult::success()
})
.into()
}
}
9 changes: 5 additions & 4 deletions app/buck2_client/src/commands/log/replay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ use buck2_client_ctx::client_ctx::ClientCommandContext;
use buck2_client_ctx::common::ui::CommonConsoleOptions;
use buck2_client_ctx::daemon::client::NoPartialResultHandler;
use buck2_client_ctx::events_ctx::EventsCtx;
use buck2_client_ctx::exit_result::ExitCode;
use buck2_client_ctx::exit_result::ExitResult;
use buck2_client_ctx::replayer::Replayer;
use buck2_client_ctx::signal_handler::with_simple_sigint_handler;
use buck2_client_ctx::subscribers::get::get_console_with_root;
use buck2_client_ctx::subscribers::subscribers::EventSubscribers;
use buck2_error::buck2_error_anyhow;

use crate::commands::log::options::EventLogOptions;

Expand Down Expand Up @@ -57,7 +57,7 @@ impl ReplayCommand {
override_args: _,
} = self;

ctx.with_runtime(|mut ctx| async move {
ctx.instant_command_no_log("log-replay", |mut ctx| async move {
let work = async {
let (replayer, invocation) =
Replayer::new(event_log.get(&ctx).await?, speed, preload).await?;
Expand Down Expand Up @@ -100,13 +100,14 @@ impl ReplayCommand {
}

// FIXME(JakobDegen)(easy): This should probably return failures if there were errors
ExitResult::success()
Ok(())
};

with_simple_sigint_handler(work)
.await
.unwrap_or_else(|| ExitResult::status(ExitCode::SignalInterrupt))
.unwrap_or_else(|| Err(buck2_error_anyhow!([], "Signal Interrupted")))
})
.into()
}
}

Expand Down
6 changes: 3 additions & 3 deletions app/buck2_client/src/commands/log/show_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl ShowLogCommand {
pub fn exec(self, _matches: &clap::ArgMatches, ctx: ClientCommandContext<'_>) -> ExitResult {
let Self { event_log } = self;

ctx.with_runtime(|ctx| async move {
ctx.instant_command_no_log("log-show", |ctx| async move {
let log_path = event_log.get(&ctx).await?;

let (invocation, mut events) = log_path.unpack_stream().await?;
Expand All @@ -44,7 +44,7 @@ impl ShowLogCommand {
}

anyhow::Ok(())
})?;
ExitResult::success()
})
.into()
}
}
6 changes: 3 additions & 3 deletions app/buck2_client/src/commands/log/show_user_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl ShowUserLogCommand {
pub fn exec(self, _matches: &clap::ArgMatches, ctx: ClientCommandContext<'_>) -> ExitResult {
let Self { event_log } = self;

ctx.with_runtime(|ctx| async move {
ctx.instant_command_no_log("log-show-user", |ctx| async move {
let log_path = event_log.get(&ctx).await?;

let (invocation, mut events) = log_path.unpack_stream().await?;
Expand All @@ -48,7 +48,7 @@ impl ShowUserLogCommand {
}

anyhow::Ok(())
})?;
ExitResult::success()
})
.into()
}
}
7 changes: 3 additions & 4 deletions app/buck2_client/src/commands/log/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ pub struct SummaryCommand {

impl SummaryCommand {
pub fn exec(self, _matches: &clap::ArgMatches, ctx: ClientCommandContext<'_>) -> ExitResult {
ctx.with_runtime(|ctx| async move {
ctx.instant_command_no_log("log-summary", |ctx| async move {
let log_path = self.event_log.get(&ctx).await?;

let (invocation, mut events) = log_path.unpack_stream().await?;
Expand Down Expand Up @@ -276,8 +276,7 @@ impl SummaryCommand {
}
buck2_client_ctx::eprintln!("{}", stats)?;
anyhow::Ok(())
})?;

ExitResult::success()
})
.into()
}
}
6 changes: 3 additions & 3 deletions app/buck2_client/src/commands/log/what_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl WhatCmdCommand {
pub(crate) fn exec(self, _matches: &clap::ArgMatches, ctx: ClientCommandContext) -> ExitResult {
let WhatCmdCommand { event_log, expand } = self;

ctx.with_runtime(|ctx| async move {
ctx.instant_command_no_log("log-what-cmd", |ctx| async move {
let log_path = event_log.get(&ctx).await?;
let (invocation, _events) = log_path.unpack_stream().await?;

Expand All @@ -40,8 +40,8 @@ impl WhatCmdCommand {
} else {
buck2_client_ctx::println!("{}", invocation.display_command_line())?;
}

ExitResult::success()
Ok(())
})
.into()
}
}
5 changes: 2 additions & 3 deletions app/buck2_client/src/commands/log/what_materialized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl WhatMaterializedCommand {
} = self;
buck2_client_ctx::stdio::print_with_writer::<anyhow::Error, _>(|w| {
let mut output = transform_format(output, w);
ctx.with_runtime(|ctx| async move {
ctx.instant_command_no_log("log-what-materialized", |ctx| async move {
let log_path = event_log.get(&ctx).await?;

let (invocation, mut events) = log_path.unpack_stream().await?;
Expand Down Expand Up @@ -223,8 +223,7 @@ impl WhatMaterializedCommand {
}

anyhow::Ok(())
})?;
anyhow::Ok(())
})
})?;
ExitResult::success()
}
Expand Down
8 changes: 3 additions & 5 deletions app/buck2_client/src/commands/log/what_ran.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl WhatRanCommand {
include_std_err: show_std_err,
omit_empty_std_err,
};
ctx.with_runtime(|ctx| async move {
ctx.instant_command_no_log("log-what-ran", |ctx| async move {
let log_path = event_log.get(&ctx).await?;

let (invocation, events) = log_path.unpack_stream().await?;
Expand All @@ -153,10 +153,8 @@ impl WhatRanCommand {
incomplete,
};
WhatRanCommandState::execute(events, &mut output, &options).await?;

anyhow::Ok(())
})?;
anyhow::Ok(())
Ok(())
})
})?;
ExitResult::success()
}
Expand Down
2 changes: 1 addition & 1 deletion app/buck2_client/src/commands/log/what_up.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl WhatUpCommand {
let Self { event_log, after } = self;
let cutoff_time = after.map(Duration::from_millis);

ctx.with_runtime(|ctx| async move {
ctx.instant_command_no_log("log-what-up", |ctx| async move {
let log_path = event_log.get(&ctx).await?;

// Get events
Expand Down
5 changes: 2 additions & 3 deletions app/buck2_client/src/commands/log/what_uploaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ impl WhatUploadedCommand {

buck2_client_ctx::stdio::print_with_writer::<anyhow::Error, _>(|w| {
let mut output = transform_format(output, w);
ctx.with_runtime(|ctx| async move {
ctx.instant_command_no_log("log-what-uploaded", |ctx| async move {
let log_path = event_log.get(&ctx).await?;

let (invocation, mut events) = log_path.unpack_stream().await?;
Expand Down Expand Up @@ -232,8 +232,7 @@ impl WhatUploadedCommand {
}

anyhow::Ok(())
})?;
anyhow::Ok(())
})
})?;
ExitResult::success()
}
Expand Down
24 changes: 22 additions & 2 deletions app/buck2_client_ctx/src/client_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ use crate::common::PreemptibleWhen;
use crate::daemon::client::connect::BuckdConnectOptions;
use crate::daemon::client::BuckdClientConnector;
use crate::daemon_constraints::get_possibly_nested_invocation_daemon_uuid;
use crate::exit_result::ExitResult;
use crate::immediate_config::ImmediateConfigContext;
use crate::restarter::Restarter;
use crate::stdin::Stdin;
Expand Down Expand Up @@ -137,7 +136,7 @@ impl<'a> ClientCommandContext<'a> {
command_name: &'static str,
event_log_opts: &CommonEventLogOptions,
func: F,
) -> ExitResult
) -> anyhow::Result<()>
where
Fut: Future<Output = anyhow::Result<()>> + 'a,
F: FnOnce(ClientCommandContext<'a>) -> Fut,
Expand All @@ -158,6 +157,27 @@ impl<'a> ClientCommandContext<'a> {
result.into()
}

/// Invoke a command without writing event log.
/// (For example, we don't write logs in `buck2 log` command.)
pub fn instant_command_no_log<Fut, F>(
self,
command_name: &'static str,
func: F,
) -> anyhow::Result<()>
where
Fut: Future<Output = anyhow::Result<()>> + 'a,
F: FnOnce(ClientCommandContext<'a>) -> Fut,
{
self.instant_command(
command_name,
&CommonEventLogOptions {
no_event_log: true,
..CommonEventLogOptions::default()
},
func,
)
}

pub fn stdin(&mut self) -> &mut Stdin {
self.stdin
}
Expand Down

0 comments on commit e5bacef

Please sign in to comment.