Skip to content

Commit 77b4ef2

Browse files
committed
node: Print pruning errors in red
1 parent c10e4f0 commit 77b4ef2

File tree

2 files changed

+44
-17
lines changed

2 files changed

+44
-17
lines changed

node/src/manager/color.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::sync::Mutex;
1+
use std::{io, sync::Mutex};
22
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
33

44
use graph::prelude::{isatty, lazy_static};
@@ -53,6 +53,11 @@ impl Terminal {
5353
self.out.set_color(&self.spec).map_err(Into::into)
5454
}
5555

56+
pub fn red(&mut self) -> CmdResult {
57+
self.spec.set_fg(Some(Color::Red));
58+
self.out.set_color(&self.spec).map_err(Into::into)
59+
}
60+
5661
pub fn dim(&mut self) -> CmdResult {
5762
self.spec.set_dimmed(true);
5863
self.out.set_color(&self.spec).map_err(Into::into)
@@ -67,6 +72,18 @@ impl Terminal {
6772
self.spec = ColorSpec::new();
6873
self.out.reset().map_err(Into::into)
6974
}
75+
76+
pub fn with_color<F, R>(&mut self, color: Color, f: F) -> io::Result<R>
77+
where
78+
F: FnOnce(&mut Self) -> io::Result<R>,
79+
{
80+
self.spec.set_fg(Some(color));
81+
self.out.set_color(&self.spec).map_err(io::Error::from)?;
82+
let res = f(self);
83+
self.spec = ColorSpec::new();
84+
self.out.set_color(&self.spec).map_err(io::Error::from)?;
85+
res
86+
}
7087
}
7188

7289
impl std::io::Write for Terminal {

node/src/manager/commands/prune.rs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ use graph_store_postgres::{
1818
command_support::{Phase, PruneTableState},
1919
ConnectionPool, Store,
2020
};
21+
use termcolor::Color;
2122

2223
use crate::manager::{
24+
color::Terminal,
2325
commands::stats::show_stats,
2426
deployment::DeploymentSearch,
2527
fmt::{self, MapOrNull as _},
@@ -329,6 +331,8 @@ pub async fn status(
329331
}
330332
}
331333

334+
let mut term = Terminal::new();
335+
332336
let deployment = search.locate_unique(&primary_pool)?;
333337

334338
let viewer = store.subgraph_store().prune_viewer(&deployment).await?;
@@ -353,37 +357,43 @@ pub async fn status(
353357
"No information about prune run #{run} found for deployment {deployment}.\n {runs}"
354358
));
355359
};
356-
println!("prune {deployment} (run #{run})");
360+
writeln!(term, "prune {deployment} (run #{run})")?;
357361

358362
if let (Some(errored_at), Some(error)) = (&state.errored_at, &state.error) {
359-
println!(" error: {error}");
360-
println!(" at: {}", fmt::date_time(errored_at));
363+
term.with_color(Color::Red, |term| {
364+
writeln!(term, " error: {error}")?;
365+
writeln!(term, " at: {}", fmt::date_time(errored_at))
366+
})?;
361367
}
362-
println!(
368+
writeln!(
369+
term,
363370
" range: {} - {} ({} blocks, should keep {} blocks)",
364371
state.first_block,
365372
state.latest_block,
366373
state.latest_block - state.first_block,
367374
state.history_blocks
368-
);
369-
println!(" started: {}", fmt::date_time(&state.started_at));
375+
)?;
376+
writeln!(term, " started: {}", fmt::date_time(&state.started_at))?;
370377
match &state.finished_at {
371-
Some(finished_at) => println!(" finished: {}", fmt::date_time(finished_at)),
372-
None => println!(" finished: still running"),
378+
Some(finished_at) => writeln!(term, " finished: {}", fmt::date_time(finished_at))?,
379+
None => writeln!(term, " finished: still running")?,
373380
}
374-
println!(
381+
writeln!(
382+
term,
375383
" duration: {}",
376384
fmt::duration(&state.started_at, &state.finished_at)
377-
);
385+
)?;
378386

379-
println!(
387+
writeln!(
388+
term,
380389
"\n{:^30} | {:^22} | {:^8} | {:^11} | {:^8}",
381390
"table", "status", "rows", "batch_size", "duration"
382-
);
383-
println!(
391+
)?;
392+
writeln!(
393+
term,
384394
"{:-^30}-+-{:-^22}-+-{:-^8}-+-{:-^11}-+-{:-^8}",
385395
"", "", "", "", ""
386-
);
396+
)?;
387397
for ts in table_states {
388398
#[allow(unused_variables)]
389399
let PruneTableState {
@@ -416,10 +426,10 @@ pub async fn status(
416426
let batch_size = batch_size.map_or_null(|b| b.to_string());
417427
let duration = started_at.map_or_null(|s| fmt::duration(&s, &finished_at));
418428
let phase = phase.as_str();
419-
println!(
429+
writeln!(term,
420430
"{table_name:<30} | {:<15} {complete:>6} | {rows:>8} | {batch_size:>11} | {duration:>8}",
421431
format!("{strategy}/{phase}")
422-
);
432+
)?;
423433
}
424434
Ok(())
425435
}

0 commit comments

Comments
 (0)