1717 */
1818
1919use crate :: option:: CONFIG ;
20+ use actix_web:: body:: MessageBody ;
21+ use actix_web:: dev:: { ServiceRequest , ServiceResponse } ;
22+ use actix_web:: error:: ErrorServiceUnavailable ;
2023use actix_web:: http:: StatusCode ;
21- use actix_web:: HttpResponse ;
24+ use actix_web:: middleware:: Next ;
25+ use actix_web:: { Error , HttpResponse } ;
2226use lazy_static:: lazy_static;
2327use std:: sync:: Arc ;
2428use tokio:: signal:: unix:: { signal, SignalKind } ;
@@ -34,9 +38,21 @@ pub async fn liveness() -> HttpResponse {
3438 HttpResponse :: new ( StatusCode :: OK )
3539}
3640
37- pub async fn handle_signals ( shutdown_signal : Arc < Mutex < Option < oneshot:: Sender < ( ) > > > > ) {
38- let signal_received = SIGNAL_RECEIVED . clone ( ) ;
41+ pub async fn check_shutdown_middleware (
42+ req : ServiceRequest ,
43+ next : Next < impl MessageBody > ,
44+ ) -> Result < ServiceResponse < impl MessageBody > , Error > {
45+ // Acquire the shutdown flag to check if the server is shutting down.
46+ if * SIGNAL_RECEIVED . lock ( ) . await {
47+ // Return 503 Service Unavailable if the server is shutting down.
48+ Err ( ErrorServiceUnavailable ( "Server is shutting down" ) )
49+ } else {
50+ // Continue processing the request if the server is not shutting down.
51+ next. call ( req) . await
52+ }
53+ }
3954
55+ pub async fn handle_signals ( shutdown_signal : Arc < Mutex < Option < oneshot:: Sender < ( ) > > > > ) {
4056 let mut sigterm =
4157 signal ( SignalKind :: terminate ( ) ) . expect ( "Failed to set up SIGTERM signal handler" ) ;
4258 log:: info!( "Signal handler task started" ) ;
@@ -47,7 +63,7 @@ pub async fn handle_signals(shutdown_signal: Arc<Mutex<Option<oneshot::Sender<()
4763 log:: info!( "Received SIGTERM signal at Readiness Probe Handler" ) ;
4864
4965 // Set the shutdown flag to true
50- let mut shutdown_flag = signal_received . lock ( ) . await ;
66+ let mut shutdown_flag = SIGNAL_RECEIVED . lock ( ) . await ;
5167 * shutdown_flag = true ;
5268
5369 // Trigger graceful shutdown
@@ -77,12 +93,6 @@ pub async fn handle_signals(shutdown_signal: Arc<Mutex<Option<oneshot::Sender<()
7793}
7894
7995pub async fn readiness ( ) -> HttpResponse {
80- // Check if the application has received a shutdown signal
81- let shutdown_flag = SIGNAL_RECEIVED . lock ( ) . await ;
82- if * shutdown_flag {
83- return HttpResponse :: new ( StatusCode :: SERVICE_UNAVAILABLE ) ;
84- }
85-
8696 // Check the object store connection
8797 if CONFIG . storage ( ) . get_object_store ( ) . check ( ) . await . is_ok ( ) {
8898 HttpResponse :: new ( StatusCode :: OK )
0 commit comments