11use super :: Context ;
22use crate :: cmd:: test_server:: metrics_streaming_client:: MetricsStreamingClient ;
33use crate :: { cli:: TestServerArgs , util:: tty:: Output } ;
4- use anyhow:: Result ;
4+ use anyhow:: { Context as AnyhowContext , Result } ;
55use server:: TestServer ;
66use sift_rs:: assets:: v1:: asset_service_server:: AssetServiceServer ;
77use sift_rs:: ingest:: v1:: ingest_service_server:: IngestServiceServer ;
@@ -20,14 +20,23 @@ pub mod server;
2020use crate :: cmd:: test_server:: metrics_streaming_client:: Metrics ;
2121
2222pub async fn run ( ctx : Context , args : TestServerArgs ) -> Result < ExitCode > {
23- let local_address = args. local_address . unwrap_or ( "127.0.0.1:50051" . into ( ) ) ;
24- let addr = local_address. parse ( ) ?;
23+ let local_address = args
24+ . local_address
25+ . unwrap_or_else ( || "0.0.0.0:50051" . to_string ( ) ) ;
26+ let addr = local_address
27+ . parse ( )
28+ . context ( format ! ( "failed to parse local_address: {}" , local_address) ) ?;
2529
2630 // Initialize streaming client.
2731 let mut streaming_client =
28- MetricsStreamingClient :: build ( ctx, & args. stream_metrics , & args. metrics_asset_name ) ?;
29- if streaming_client. is_some ( ) {
30- streaming_client. as_mut ( ) . unwrap ( ) . initialize ( ) . await ?;
32+ MetricsStreamingClient :: build ( ctx, & args. stream_metrics , & args. metrics_asset_name )
33+ . context ( "failed to create metrics streaming client" ) ?;
34+
35+ if let Some ( client) = streaming_client. as_mut ( ) {
36+ client
37+ . initialize ( )
38+ . await
39+ . context ( "failed to initialize streaming client" ) ?;
3140 }
3241
3342 // Channel to signal program exit.
@@ -54,27 +63,25 @@ pub async fn run(ctx: Context, args: TestServerArgs) -> Result<ExitCode> {
5463
5564 // Start task to ingest metrics to Sift.
5665 let ingest_metrics_task = tokio:: spawn ( async move {
57- if streaming_client. is_none ( ) {
58- return ;
59- }
60-
61- let mut client = streaming_client. unwrap ( ) ;
62- loop {
63- tokio:: select! {
64- _ = shutdown_rx2. changed( ) => {
65- Output :: new( ) . line( "Ingest task shutting down" ) . print( ) ;
66- break ;
67- }
68- Some ( metrics) = metrics_rx. recv( ) => {
69- client. ingest( metrics) . await ;
70- }
71- } ;
66+ if let Some ( client) = streaming_client. as_mut ( ) {
67+ loop {
68+ tokio:: select! {
69+ _ = shutdown_rx2. changed( ) => {
70+ Output :: new( ) . line( "Ingest task shutting down" ) . print( ) ;
71+ break ;
72+ }
73+ Some ( metrics) = metrics_rx. recv( ) => {
74+ client. ingest( metrics) . await ;
75+ }
76+ } ;
77+ }
7278 }
7379 } ) ;
7480
7581 let reflection_service = Builder :: configure ( )
7682 . register_encoded_file_descriptor_set ( FILE_DESCRIPTOR_SET )
77- . build_v1 ( ) ?;
83+ . build_v1 ( )
84+ . context ( "failed to create gRPC reflection service" ) ?;
7885
7986 Output :: new ( )
8087 . line ( format ! ( "Server listening on {addr}" ) )
@@ -92,8 +99,12 @@ pub async fn run(ctx: Context, args: TestServerArgs) -> Result<ExitCode> {
9299 } )
93100 . await ?;
94101
95- calc_stats_task. await ?;
96- ingest_metrics_task. await ?;
102+ calc_stats_task
103+ . await
104+ . context ( "failed to await calculation task" ) ?;
105+ ingest_metrics_task
106+ . await
107+ . context ( "failed to await ingestion task" ) ?;
97108
98109 Output :: new ( ) . line ( "Exiting." ) . print ( ) ;
99110
0 commit comments