File tree Expand file tree Collapse file tree 1 file changed +27
-1
lines changed
Expand file tree Collapse file tree 1 file changed +27
-1
lines changed Original file line number Diff line number Diff line change @@ -79,6 +79,30 @@ pub async fn not_found(uri: Uri) -> (axum::http::StatusCode, String) {
7979 ( axum:: http:: StatusCode :: NOT_FOUND , "Not Found" . to_string ( ) )
8080}
8181
82+ async fn shutdown_signal ( ) {
83+ let ctrl_c = tokio:: signal:: ctrl_c ( ) ;
84+
85+ #[ cfg( unix) ]
86+ let mut terminate_signal =
87+ tokio:: signal:: unix:: signal ( tokio:: signal:: unix:: SignalKind :: terminate ( ) )
88+ . expect ( "failed to install signal handler" ) ;
89+
90+ #[ cfg( unix) ]
91+ let terminate = terminate_signal. recv ( ) ;
92+
93+ #[ cfg( not( unix) ) ]
94+ // On non-Unix platforms, we just make terminate a pending future, only ctrl_c matters.
95+ let terminate = std:: future:: pending :: < ( ) > ( ) ;
96+
97+ tokio:: select! {
98+ _ = ctrl_c => { } ,
99+ _ = terminate => { } ,
100+ }
101+
102+ println ! ( ) ;
103+ log:: info!( "Signal received, starting graceful shutdown" ) ;
104+ }
105+
82106pub async fn run ( args : Args ) -> anyhow:: Result < ( ) > {
83107 let state = ServerState :: new ( args. clone ( ) ) ;
84108
@@ -115,7 +139,9 @@ pub async fn run(args: Args) -> anyhow::Result<()> {
115139 format!( "http://{}" , addr) . blue( )
116140 ) ;
117141
118- axum:: serve ( listener, app) . await ?;
142+ axum:: serve ( listener, app)
143+ . with_graceful_shutdown ( shutdown_signal ( ) )
144+ . await ?;
119145
120146 Ok ( ( ) )
121147}
You can’t perform that action at this time.
0 commit comments