|
| 1 | +// Copyright (c) 2023 - 2025 Restate Software, Inc., Restate GmbH. |
| 2 | +// All rights reserved. |
| 3 | +// |
| 4 | +// Use of this software is governed by the Business Source License |
| 5 | +// included in the LICENSE file. |
| 6 | +// |
| 7 | +// As of the Change Date specified in that file, in accordance with |
| 8 | +// the Business Source License, use of this software will be governed |
| 9 | +// by the Apache License, Version 2.0. |
| 10 | + |
| 11 | +use std::io::IsTerminal; |
| 12 | +use std::io::Write as _; |
| 13 | + |
| 14 | +use restate_lite::Options; |
| 15 | +use tokio::signal::unix::SignalKind; |
| 16 | +use tokio::signal::unix::signal; |
| 17 | + |
| 18 | +use restate_lite::AddressMeta; |
| 19 | +use restate_lite::Restate; |
| 20 | + |
| 21 | +#[cfg(not(target_env = "msvc"))] |
| 22 | +use tikv_jemallocator::Jemalloc; |
| 23 | + |
| 24 | +#[cfg(not(target_env = "msvc"))] |
| 25 | +#[global_allocator] |
| 26 | +static GLOBAL: Jemalloc = Jemalloc; |
| 27 | + |
| 28 | +#[tokio::main(flavor = "multi_thread")] |
| 29 | +async fn main() -> anyhow::Result<()> { |
| 30 | + tracing_subscriber::fmt::init(); |
| 31 | + |
| 32 | + let temp_dir = tempfile::tempdir()?; |
| 33 | + let options = Options { |
| 34 | + data_dir: Some(temp_dir.path().to_path_buf()), |
| 35 | + ..Default::default() |
| 36 | + }; |
| 37 | + |
| 38 | + println!( |
| 39 | + "Using data dir: {}", |
| 40 | + options.data_dir.as_ref().unwrap().display() |
| 41 | + ); |
| 42 | + |
| 43 | + let restate = Restate::create(options).await?; |
| 44 | + println!("** Bound addresses **"); |
| 45 | + print_addresses(&restate.get_bound_addresses()); |
| 46 | + |
| 47 | + println!("** Advertised addresses **"); |
| 48 | + print_addresses(&restate.get_advertised_addresses()); |
| 49 | + |
| 50 | + shutdown(restate).await?; |
| 51 | + eprintln!("Bye!"); |
| 52 | + |
| 53 | + Ok(()) |
| 54 | +} |
| 55 | + |
| 56 | +fn print_addresses(addresses: &[AddressMeta]) { |
| 57 | + if !std::io::stdout().is_terminal() { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + let mut stdout = std::io::stdout().lock(); |
| 62 | + for address in addresses { |
| 63 | + let _ = writeln!(&mut stdout, "[{}]: {}", address.name, address.address); |
| 64 | + } |
| 65 | + |
| 66 | + let _ = writeln!(&mut stdout); |
| 67 | +} |
| 68 | + |
| 69 | +async fn shutdown(restate: Restate) -> anyhow::Result<()> { |
| 70 | + let signal = tokio::select! { |
| 71 | + () = await_signal(SignalKind::interrupt()) => "SIGINT", |
| 72 | + () = await_signal(SignalKind::terminate()) => "SIGTERM", |
| 73 | + }; |
| 74 | + eprintln!("Received {signal}, starting shutdown."); |
| 75 | + |
| 76 | + restate.stop().await |
| 77 | +} |
| 78 | + |
| 79 | +async fn await_signal(kind: SignalKind) { |
| 80 | + signal(kind) |
| 81 | + .expect("failed to register signal handler") |
| 82 | + .recv() |
| 83 | + .await; |
| 84 | +} |
0 commit comments