1818use crate :: analytics;
1919use crate :: banner;
2020use crate :: handlers:: airplane;
21+ use crate :: handlers:: http:: health_check;
2122use crate :: handlers:: http:: ingest;
2223use crate :: handlers:: http:: logstream;
2324use crate :: handlers:: http:: middleware:: RouteExt ;
@@ -35,6 +36,8 @@ use crate::storage::ObjectStorageError;
3536use crate :: storage:: PARSEABLE_ROOT_DIRECTORY ;
3637use crate :: sync;
3738
39+ use std:: sync:: Arc ;
40+
3841use super :: server:: Server ;
3942use super :: ssl_acceptor:: get_ssl_acceptor;
4043use super :: IngestorMetadata ;
@@ -56,6 +59,7 @@ use bytes::Bytes;
5659use once_cell:: sync:: Lazy ;
5760use relative_path:: RelativePathBuf ;
5861use serde_json:: Value ;
62+ use tokio:: sync:: { oneshot, Mutex } ;
5963
6064/// ! have to use a guard before using it
6165pub static INGESTOR_META : Lazy < IngestorMetadata > =
@@ -91,17 +95,47 @@ impl ParseableServer for IngestServer {
9195 . wrap ( cross_origin_config ( ) )
9296 } ;
9397
94- // concurrent workers equal to number of logical cores
95- let http_server = HttpServer :: new ( create_app_fn) . workers ( num_cpus:: get ( ) ) ;
98+ // Create a channel to trigger server shutdown
99+ let ( shutdown_trigger, shutdown_rx) = oneshot:: channel :: < ( ) > ( ) ;
100+ let server_shutdown_signal = Arc :: new ( Mutex :: new ( Some ( shutdown_trigger) ) ) ;
101+
102+ // Clone the shutdown signal for the signal handler
103+ let shutdown_signal = server_shutdown_signal. clone ( ) ;
104+
105+ // Spawn the signal handler task
106+ tokio:: spawn ( async move {
107+ health_check:: handle_signals ( shutdown_signal) . await ;
108+ println ! ( "Received shutdown signal, notifying server to shut down..." ) ;
109+ } ) ;
96110
97- if let Some ( config) = ssl {
111+ // Create the HTTP server
112+ let http_server = HttpServer :: new ( create_app_fn)
113+ . workers ( num_cpus:: get ( ) )
114+ . shutdown_timeout ( 60 ) ;
115+
116+ // Start the server with or without TLS
117+ let srv = if let Some ( config) = ssl {
98118 http_server
99119 . bind_rustls_0_22 ( & CONFIG . parseable . address , config) ?
100120 . run ( )
101- . await ?;
102121 } else {
103- http_server. bind ( & CONFIG . parseable . address ) ?. run ( ) . await ?;
104- }
122+ http_server. bind ( & CONFIG . parseable . address ) ?. run ( )
123+ } ;
124+
125+ // Graceful shutdown handling
126+ let srv_handle = srv. handle ( ) ;
127+
128+ tokio:: spawn ( async move {
129+ // Wait for the shutdown signal
130+ shutdown_rx. await . ok ( ) ;
131+
132+ // Initiate graceful shutdown
133+ log:: info!( "Graceful shutdown of HTTP server triggered" ) ;
134+ srv_handle. stop ( true ) . await ;
135+ } ) ;
136+
137+ // Await the server to run and handle shutdown
138+ srv. await ?;
105139
106140 Ok ( ( ) )
107141 }
0 commit comments