Skip to content

Commit

Permalink
feat: termination kick cli option
Browse files Browse the repository at this point in the history
  • Loading branch information
Erb3 committed Aug 3, 2024
1 parent 4dd8df4 commit 56eeca8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
9 changes: 9 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ pub(crate) struct Cli {
/// guessed. Default is 3s
#[arg(long, env = "SVEIO_SHOWCASE_TIME")]
pub(crate) showcase_time: Option<u64>,

/// Optional boolean deciding if players should be removed to the game
/// when the server receives a termination signal. With this enabled,
/// shutting down the server will take an additional five seconds
/// so that clients can load the error message. For debugging
/// reasons this is disabled by default, if you are not running in release.
/// If you are running in release, this will be enabled by default.
#[arg(long, env = "SVEIO_TERMINATION_KICK")]
pub(crate) termination_kick: Option<bool>,
}

pub(crate) fn get_settings() -> Cli {
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
showcase_time: settings.showcase_time.unwrap_or(3),
},
port: Some(settings.port.unwrap_or(8085)),
server_termination_kick: settings.termination_kick.unwrap_or(!cfg!(debug_assertions)),
})
.await;

Expand All @@ -42,6 +43,7 @@ async fn main() -> shuttle_axum::ShuttleAxum {
showcase_time: 3,
},
port: None,
server_termination_kick: true,
})
.await
.unwrap()
Expand Down
37 changes: 18 additions & 19 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use tracing::info;
pub(crate) struct ServerOptions {
pub(crate) game: game::GameOptions,
pub(crate) port: Option<u32>,
pub(crate) server_termination_kick: bool,
}

pub(crate) async fn create_server(opts: ServerOptions) -> Option<axum::Router> {
Expand Down Expand Up @@ -67,7 +68,7 @@ pub(crate) async fn create_server(opts: ServerOptions) -> Option<axum::Router> {

info!("✅ Listening on http://{}", listener.local_addr().unwrap());
axum::serve(listener, app)
.with_graceful_shutdown(shutdown_signal(shutdown_io))
.with_graceful_shutdown(shutdown_signal(shutdown_io, opts.server_termination_kick))
.await
.unwrap();

Expand All @@ -77,7 +78,7 @@ pub(crate) async fn create_server(opts: ServerOptions) -> Option<axum::Router> {
Some(app)
}

async fn shutdown_signal(io: Arc<SocketIo>) {
async fn shutdown_signal(io: Arc<SocketIo>, should_kick: bool) {
let ctrl_c = async {
signal::ctrl_c()
.await
Expand All @@ -100,24 +101,22 @@ async fn shutdown_signal(io: Arc<SocketIo>) {
_ = terminate => {},
}

info!("Termination signal received, starting graceful shutdown. Exiting soon");
for socket in io.sockets().unwrap() {
socket
.emit(
"kick",
packets::DisconnectPacket {
message: "Server going down".to_string(),
},
)
.unwrap();
socket.disconnect().unwrap();
if should_kick {
info!("Termination signal received, starting graceful shutdown. Exiting soon");
for socket in io.sockets().unwrap() {
socket
.emit(
"kick",
packets::DisconnectPacket {
message: "Server going down".to_string(),
},
)
.unwrap();
socket.disconnect().unwrap();
}

tokio::time::sleep(Duration::from_secs(5)).await;
}

tokio::time::sleep(Duration::from_secs(if cfg!(debug_assertions) {
0
} else {
5
}))
.await;
info!("Exit imminent")
}

0 comments on commit 56eeca8

Please sign in to comment.