Skip to content

Commit

Permalink
[aptos-workspace-server] add ability to listen for a stop command + b…
Browse files Browse the repository at this point in the history
…ump aptos CLI version to 6.1.1 (#15898)
  • Loading branch information
vgao1996 authored Feb 6, 2025
1 parent 97f80e6 commit 80160b5
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 59 additions & 1 deletion aptos-move/aptos-workspace-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,69 @@ use services::{
processors::start_all_processors,
};
use std::time::Duration;
use tokio::io::{AsyncBufReadExt, BufReader};
use tokio_util::sync::CancellationToken;
use uuid::Uuid;

#[derive(Parser)]
enum ControlCommand {
Stop,
}

/// Starts an async task that reads and processes commands from stdin.
///
/// Currently there is only one command:
/// - stop: shuts down the services gracefully.
async fn start_command_processor(shutdown: CancellationToken) {
let reader = BufReader::new(tokio::io::stdin());
let mut lines = reader.lines();

loop {
tokio::select! {
line = lines.next_line() => {
match line {
Ok(Some(input)) => {
let res = ControlCommand::try_parse_from(format!("dummy {}", input).split_whitespace());
// Note: clap expects a program name so we add a dummy one.
match res {
Ok(cmd) => match cmd {
ControlCommand::Stop => {
no_panic_println!("\nStop command received. Shutting down services. This may take a while.\n");
shutdown.cancel();
break;
}
}
Err(_) => {
no_panic_eprintln!("Invalid command: \"{}\"", input);
}
}
}
Ok(None) => {
break;
}
Err(err) => {
no_panic_eprintln!("Error reading from stdin: {}", err);
break;
}
}
}
_ = shutdown.cancelled() => {
break;
}
}
}
}

async fn run_all_services(timeout: u64) -> Result<()> {
let test_dir = tempfile::tempdir()?;
let test_dir = test_dir.path();
no_panic_println!("Created test directory: {}", test_dir.display());

let instance_id = Uuid::new_v4();

// Phase 0: Register the signal handler for ctrl-c.
// Phase 0: Start the background services

// Register the signal handler for ctrl-c.
let shutdown = CancellationToken::new();
{
// TODO: Find a way to register the signal handler in a blocking manner without
Expand All @@ -67,6 +119,12 @@ async fn run_all_services(timeout: u64) -> Result<()> {
});
}

// Start the command processor.
{
let shutdown = shutdown.clone();
tokio::spawn(start_command_processor(shutdown));
}

// Phase 1: Start all services.
// Node
let (fut_node_api, fut_indexer_grpc, fut_node_finish) = services::node::start_node(test_dir)?;
Expand Down
4 changes: 4 additions & 0 deletions crates/aptos/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to the Aptos CLI will be captured in this file. This project

# Unreleased

## [6.1.1]
- Added a new feature to `aptos workspace run`: The workspace server now listens for a "stop" command from
stdin, which triggers a graceful shutdown when received.

## [6.1.0]
- Remove FFI support from Aptos CLI.
- Various compiler bug fixes.
Expand Down
2 changes: 1 addition & 1 deletion crates/aptos/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "aptos"
description = "Aptos tool for management of nodes and interacting with the blockchain"
version = "6.1.0"
version = "6.1.1"

# Workspace inherited keys
authors = { workspace = true }
Expand Down

0 comments on commit 80160b5

Please sign in to comment.