@@ -3,9 +3,9 @@ use crate::{
33 store:: AdapterStore ,
44 traits:: { LoadConfig , Server as AdapterServer } ,
55} ;
6- use code0_flow:: flow_definition :: FlowUpdateService ;
6+ use code0_flow:: flow_service :: FlowUpdateService ;
77use std:: sync:: Arc ;
8- use tokio:: sync :: broadcast ;
8+ use tokio:: signal ;
99use tonic:: transport:: Server ;
1010use tonic_health:: pb:: health_server:: HealthServer ;
1111
@@ -20,11 +20,14 @@ pub struct ServerContext<C: LoadConfig> {
2020pub struct ServerRunner < C : LoadConfig > {
2121 context : ServerContext < C > ,
2222 server : Box < dyn AdapterServer < C > > ,
23- shutdown_sender : broadcast:: Sender < ( ) > ,
2423}
2524
2625impl < C : LoadConfig > ServerRunner < C > {
2726 pub async fn new < S : AdapterServer < C > > ( server : S ) -> anyhow:: Result < Self > {
27+ env_logger:: Builder :: from_default_env ( )
28+ . filter_level ( log:: LevelFilter :: Debug )
29+ . init ( ) ;
30+
2831 code0_flow:: flow_config:: load_env_file ( ) ;
2932
3033 let adapter_config = AdapterConfig :: from_env ( ) ;
@@ -41,17 +44,15 @@ impl<C: LoadConfig> ServerRunner<C> {
4144 server_config : Arc :: new ( server_config) ,
4245 } ;
4346
44- let ( shutdown_tx, _) = broadcast:: channel ( 1 ) ;
45-
4647 Ok ( Self {
4748 context,
4849 server : Box :: new ( server) ,
49- shutdown_sender : shutdown_tx,
5050 } )
5151 }
5252
53- pub async fn serve ( mut self ) -> anyhow:: Result < ( ) > {
53+ pub async fn serve ( self ) -> anyhow:: Result < ( ) > {
5454 let config = self . context . adapter_config . clone ( ) ;
55+ log:: info!( "Starting Draco Variant: {}" , config. draco_variant) ;
5556
5657 if !config. is_static ( ) {
5758 let definition_service = FlowUpdateService :: from_url (
@@ -61,42 +62,84 @@ impl<C: LoadConfig> ServerRunner<C> {
6162 definition_service. send ( ) . await ;
6263 }
6364
64- if config. with_health_service {
65+ let health_task = if config. with_health_service {
6566 let health_service =
6667 code0_flow:: flow_health:: HealthService :: new ( config. nats_url . clone ( ) ) ;
6768 let address = format ! ( "{}:{}" , config. grpc_host, config. grpc_port) . parse ( ) ?;
6869
69- tokio:: spawn ( async move {
70- let _ = Server :: builder ( )
71- . add_service ( HealthServer :: new ( health_service) )
72- . serve ( address)
73- . await ;
74- } ) ;
75-
7670 log:: info!(
77- "Health server started at {}:{}" ,
71+ "Health server starting at {}:{}" ,
7872 config. grpc_host,
7973 config. grpc_port
8074 ) ;
81- }
82-
83- self . server . init ( & self . context ) . await ?;
8475
85- let mut rx = self . shutdown_sender . subscribe ( ) ;
86- let context = self . context ;
87- let mut server = self . server ;
76+ Some ( tokio:: spawn ( async move {
77+ if let Err ( err) = Server :: builder ( )
78+ . add_service ( HealthServer :: new ( health_service) )
79+ . serve ( address)
80+ . await
81+ {
82+ log:: error!( "Health server error: {:?}" , err) ;
83+ } else {
84+ log:: info!( "Health server stopped gracefully" ) ;
85+ }
86+ } ) )
87+ } else {
88+ None
89+ } ;
8890
89- let handle = tokio:: spawn ( async move {
90- tokio:: select! {
91- result = server. run( & context) => result,
92- _ = rx. recv( ) => server. shutdown( & context) . await ,
91+ let ServerRunner {
92+ mut server,
93+ context,
94+ } = self ;
95+
96+ // Init the adapter server (e.g. create underlying HTTP server)
97+ server. init ( & context) . await ?;
98+ log:: info!( "Draco successfully initialized." ) ;
99+
100+ match health_task {
101+ Some ( mut ht) => {
102+ tokio:: select! {
103+ // Main adapter server loop finished on its own
104+ res = server. run( & context) => {
105+ log:: warn!( "Adapter server finished, shutting down" ) ;
106+ ht. abort( ) ;
107+ res?;
108+ }
109+
110+ // Health server ended first
111+ _ = & mut ht => {
112+ log:: warn!( "Health server task finished, shutting down adapter" ) ;
113+ server. shutdown( & context) . await ?;
114+ }
115+
116+ // Ctrl+C / SIGINT
117+ _ = signal:: ctrl_c( ) => {
118+ log:: info!( "Ctrl+C/Exit signal received, shutting down adapter" ) ;
119+ server. shutdown( & context) . await ?;
120+ ht. abort( ) ;
121+ }
122+ }
93123 }
94- } ) ;
95124
96- tokio:: signal:: ctrl_c ( ) . await ?;
97- let _ = self . shutdown_sender . send ( ( ) ) ;
98- handle. await ??;
125+ None => {
126+ tokio:: select! {
127+ // Adapter server loop ends on its own
128+ res = server. run( & context) => {
129+ log:: warn!( "Adapter server finished" ) ;
130+ res?;
131+ }
132+
133+ // Ctrl+C / SIGINT
134+ _ = signal:: ctrl_c( ) => {
135+ log:: info!( "Ctrl+C/Exit signal received, shutting down adapter" ) ;
136+ server. shutdown( & context) . await ?;
137+ }
138+ }
139+ }
140+ }
99141
142+ log:: info!( "Draco shutdown complete" ) ;
100143 Ok ( ( ) )
101144 }
102145}
0 commit comments