Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/bin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ use reqwest::Client;
use std::io::Write;
use std::net::SocketAddr;
use tokio::net::TcpListener;
#[cfg(unix)]
use tokio::signal::unix::{SignalKind, signal};
#[cfg(windows)]
use tokio::signal::windows::{ctrl_break, ctrl_c, ctrl_close, ctrl_logoff, ctrl_shutdown};
use tower::Layer;

const CORE_THREADS: usize = 4;
Expand Down Expand Up @@ -90,6 +93,7 @@ fn main() -> anyhow::Result<()> {
Ok(())
}

#[cfg(unix)]
async fn shutdown_signal() {
let interrupt = async {
signal(SignalKind::interrupt())
Expand All @@ -111,6 +115,52 @@ async fn shutdown_signal() {
}
}

#[cfg(windows)]
async fn shutdown_signal() {
let ctrl_break = async {
ctrl_break()
.expect("failed to install signal handler")
.recv()
.await;
};

let ctrl_c = async {
ctrl_c()
.expect("failed to install signal handler")
.recv()
.await;
};

let ctrl_close = async {
ctrl_close()
.expect("failed to install signal handler")
.recv()
.await;
};

let ctrl_logoff = async {
ctrl_logoff()
.expect("failed to install signal handler")
.recv()
.await;
};

let ctrl_shutdown = async {
ctrl_shutdown()
.expect("failed to install signal handler")
.recv()
.await;
};

tokio::select! {
_ = ctrl_break => {},
_ = ctrl_c => {},
_ = ctrl_close => {},
_ = ctrl_logoff => {},
_ = ctrl_shutdown => {},
}
Comment on lines +155 to +161
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need all of them? if this is just for local development, shouldn't e.g. ctrl_c be sufficient? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know, I just figured I'd use them all while they're not placed.

To be fair I didn't find the recommended semantics for the signals to be particularly well-documented

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since I don't have a Windows machine here, can you check if the following is sufficient and compiles?

#[cfg(windows)]
async fn shutdown_signal() {
    ctrl_c()
        .expect("failed to install signal handler")
        .recv()
        .await
}

it's a bit unfortunate that CI won't be able to check that this actually compiles since we run CI only on Unix, but I don't think it makes sense for us to run CI on Windows too.

}

fn log_instance_metrics_thread(app: Arc<App>) {
// Only run the thread if the configuration is provided
let interval = match app.config.instance_metrics_log_every_seconds {
Expand Down