Skip to content

Commit ce8f14b

Browse files
committed
chore: add more detailed log
1 parent 8b924ea commit ce8f14b

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

src/cli/subcommands/sync_cmd.rs

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::{
1414
};
1515
use ticker::Ticker;
1616
use tokio::time;
17+
use tokio::time::sleep;
1718

1819
#[derive(Debug, Subcommand)]
1920
pub enum SyncCommands {
@@ -54,10 +55,7 @@ impl SyncCommands {
5455
.await
5556
.context("Failed to get sync status")?;
5657

57-
// Skip printing if initializing, since it's not useful to print
58-
if report.status == NodeSyncStatus::Initializing {
59-
continue;
60-
}
58+
wait_for_node_to_start_syncing(&client).await?;
6159

6260
clear_previous_lines(&mut stdout, lines_printed_last_iteration)?;
6361

@@ -77,12 +75,12 @@ impl SyncCommands {
7775
Self::Status => {
7876
let sync_status = client.call(SyncStatus::request(())?).await?;
7977
if sync_status.status == NodeSyncStatus::Initializing {
80-
print!("Node initializing, checking snapshot status..\n\n");
8178
// If a snapshot is required and not yet complete, return here
8279
if !check_snapshot_progress(&client, false)
8380
.await?
8481
.is_not_required()
8582
{
83+
println!("Please try again later, once the snapshot is downloaded...");
8684
return Ok(());
8785
};
8886
}
@@ -221,6 +219,7 @@ async fn check_snapshot_progress(
221219

222220
match progress_state {
223221
SnapshotProgressState::Completed | SnapshotProgressState::NotRequired => {
222+
println!();
224223
return Ok(progress_state);
225224
}
226225
_ if !wait => {
@@ -231,24 +230,50 @@ async fn check_snapshot_progress(
231230
}
232231
}
233232

233+
/// Waits for node initialization to complete (start `Syncing`).
234+
async fn wait_for_node_to_start_syncing(client: &rpc::Client) -> anyhow::Result<()> {
235+
let mut is_msg_printed = false;
236+
let mut stdout = stdout();
237+
const POLLING_INTERVAL: Duration = Duration::from_secs(1);
238+
239+
loop {
240+
let report = SyncStatus::call(client, ())
241+
.await
242+
.context("Failed to get sync status while waiting for initialization to complete")?;
243+
244+
if report.status == NodeSyncStatus::Initializing {
245+
write!(stdout, "\r🔄 Node syncing is initializing, please wait...")?;
246+
stdout.flush()?;
247+
is_msg_printed = true;
248+
249+
sleep(POLLING_INTERVAL).await;
250+
} else {
251+
if is_msg_printed {
252+
clear_previous_lines(&mut stdout, 1)
253+
.context("Failed to clear initializing message")?;
254+
}
255+
256+
break;
257+
}
258+
}
259+
260+
Ok(())
261+
}
262+
234263
/// Checks if a snapshot download is required or in progress when the node is initializing.
235264
/// If a snapshot download is in progress, it waits for completion before starting the sync monitor.
236265
async fn handle_initial_snapshot_check(client: &rpc::Client) -> anyhow::Result<()> {
237266
let initial_report = SyncStatus::call(client, ())
238267
.await
239268
.context("Failed to get sync status")?;
240269
if initial_report.status == NodeSyncStatus::Initializing {
241-
print!("Node initializing, checking snapshot status...\n\n");
242270
// if the snapshot download is not required, then return,
243271
// else wait till the snapshot download is completed.
244-
if !SyncSnapshotProgress::call(client, ())
272+
if !check_snapshot_progress(client, false)
245273
.await?
246274
.is_not_required()
247275
{
248276
check_snapshot_progress(client, true).await?;
249-
println!("Snapshot download complete. Starting sync monitor...");
250-
} else {
251-
println!("No snapshot download required or already complete.");
252277
}
253278
}
254279

0 commit comments

Comments
 (0)