|
| 1 | +/* |
| 2 | + * Copyright 2025 ByteDance and/or its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +use clap::{Arg, ArgMatches, Command}; |
| 18 | +use futures_util::future::TryFutureExt; |
| 19 | + |
| 20 | +use g3_ctl::CommandResult; |
| 21 | + |
| 22 | +use g3tiles_proto::backend_capnp::backend_control; |
| 23 | +use g3tiles_proto::proc_capnp::proc_control; |
| 24 | + |
| 25 | +pub const COMMAND: &str = "backend"; |
| 26 | + |
| 27 | +const COMMAND_ARG_NAME: &str = "name"; |
| 28 | + |
| 29 | +const SUBCOMMAND_ALIVE_CONNECTION: &str = "alive-connection"; |
| 30 | + |
| 31 | +pub fn command() -> Command { |
| 32 | + Command::new(COMMAND) |
| 33 | + .arg(Arg::new(COMMAND_ARG_NAME).required(true).num_args(1)) |
| 34 | + .subcommand_required(true) |
| 35 | + .subcommand(Command::new(SUBCOMMAND_ALIVE_CONNECTION)) |
| 36 | +} |
| 37 | + |
| 38 | +async fn alive_connection(client: &backend_control::Client) -> CommandResult<()> { |
| 39 | + let req = client.alive_connection_request(); |
| 40 | + let rsp = req.send().promise.await?; |
| 41 | + let count = rsp.get()?.get_count(); |
| 42 | + println!("{count}"); |
| 43 | + Ok(()) |
| 44 | +} |
| 45 | + |
| 46 | +pub async fn run(client: &proc_control::Client, args: &ArgMatches) -> CommandResult<()> { |
| 47 | + let name = args.get_one::<String>(COMMAND_ARG_NAME).unwrap(); |
| 48 | + |
| 49 | + let (subcommand, _) = args.subcommand().unwrap(); |
| 50 | + match subcommand { |
| 51 | + SUBCOMMAND_ALIVE_CONNECTION => { |
| 52 | + super::proc::get_backend(client, name) |
| 53 | + .and_then(|backend| async move { alive_connection(&backend).await }) |
| 54 | + .await |
| 55 | + } |
| 56 | + _ => unreachable!(), |
| 57 | + } |
| 58 | +} |
0 commit comments