Skip to content

Commit 8521c70

Browse files
committed
Added until_termination.
1 parent 4d47049 commit 8521c70

File tree

5 files changed

+63
-5
lines changed

5 files changed

+63
-5
lines changed

server/swimos_server_app/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ authors = ["Swim Inc. developers info@swim.ai"]
55
edition = "2021"
66

77
[features]
8-
default = []
8+
default = ["signal"]
99
rocks_store = ["swimos_rocks_store"]
1010
trust_dns = ["swimos_runtime/trust_dns"]
11+
signal = ["tokio/signal"]
1112

1213
[dependencies]
1314
futures = { workspace = true }

server/swimos_server_app/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ pub use self::{
6565
util::AgentExt,
6666
};
6767

68+
#[cfg(feature = "signal")]
69+
pub use server::wait::{until_termination, RegistrationFailed};
70+
6871
pub use error::{AmbiguousRoutes, ServerBuilderError, ServerError};
6972
pub use ratchet::deflate::{DeflateConfig, WindowBits};
7073
pub use swimos_introspection::IntrospectionConfig;

server/swimos_server_app/src/server/mod.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,57 @@ impl Server for BoxServer {
126126
self.0.run_box()
127127
}
128128
}
129+
130+
#[cfg(feature = "signal")]
131+
pub mod wait {
132+
use std::net::SocketAddr;
133+
134+
use tracing::debug;
135+
136+
use crate::ServerHandle;
137+
138+
use thiserror::Error;
139+
140+
/// Errors that can occur waiting for tha server to stop.
141+
#[derive(Debug, Error, Clone, Copy)]
142+
#[error("The Ctrl-C handler could not be installed.")]
143+
pub struct RegistrationFailed;
144+
145+
/// Register a Ctrl-C handler that will stop a server instance.
146+
///
147+
/// # Arguments
148+
/// * `server` - The server to run.
149+
/// * `bound` - If specified this will be called when the server has bound to a socket, with the address.
150+
pub async fn start_and_await(
151+
mut handle: ServerHandle,
152+
bound: Option<Box<dyn FnOnce(SocketAddr) + Send>>,
153+
) -> Result<(), RegistrationFailed> {
154+
155+
let wait_for_ctrl_c = async move {
156+
let mut result = Ok(());
157+
let mut shutdown_hook = Box::pin(async { tokio::signal::ctrl_c().await });
158+
159+
let print_addr = handle.bound_addr();
160+
161+
let maybe_addr = tokio::select! {
162+
r = &mut shutdown_hook => {
163+
result = r;
164+
None
165+
},
166+
maybe_addr = print_addr => maybe_addr,
167+
};
168+
169+
if let Some(addr) = maybe_addr {
170+
if let Some(f) = bound {
171+
f(addr);
172+
}
173+
result = shutdown_hook.await;
174+
}
175+
176+
debug!("Stopping server.");
177+
handle.stop();
178+
result
179+
};
180+
wait_for_ctrl_c.await.map_err(|_| RegistrationFailed)
181+
}
182+
}

swimos/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ swimos_utilities = { path = "../swimos_utilities", features = ["io", "text"] }
1616
swimos_api = { path = "../api/swimos_api" }
1717
swimos_model = { path = "../api/swimos_model" }
1818
swimos_recon = { path = "../api/formats/swimos_recon" }
19-
swimos_server_app = { path = "../server/swimos_server_app", optional = true }
19+
swimos_server_app = { path = "../server/swimos_server_app", optional = true, features = ["signal"]}
2020
swimos_agent = { path = "../server/swimos_agent", optional = true }
2121
swimos_agent_derive = { path = "../server/swimos_agent_derive", optional = true }
2222
swimos_remote = { path = "../runtime/swimos_remote", optional = true}

swimos/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ pub mod io {
9494
#[cfg(feature = "server")]
9595
pub mod server {
9696
pub use swimos_server_app::{
97-
BoxServer, DeflateConfig, IntrospectionConfig, RemoteConnectionsConfig, Server,
98-
ServerBuilder, ServerHandle, WindowBits,
97+
start_and_await, BoxServer, DeflateConfig, IntrospectionConfig, RemoteConnectionsConfig,
98+
Server, ServerBuilder, ServerHandle, WindowBits,
9999
};
100100

101101
/// Configuration for TLS support in the server.
@@ -111,7 +111,7 @@ pub mod server {
111111
pub use swimos_remote::tls::TlsError;
112112
pub use swimos_remote::ConnectionError;
113113
pub use swimos_server_app::{
114-
AmbiguousRoutes, ServerBuilderError, ServerError, UnresolvableRoute,
114+
AmbiguousRoutes, RegistrationFailed, ServerBuilderError, ServerError, UnresolvableRoute,
115115
};
116116
}
117117
}

0 commit comments

Comments
 (0)