11// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs.
22// SPDX-License-Identifier: Apache-2.0
33
4-
54use super :: monitor:: { monitor_deployment_status, DeploymentStatus } ;
65use anyhow:: anyhow;
76use axum:: body:: Bytes ;
@@ -161,19 +160,7 @@ impl DeploymentClient {
161160 & self ,
162161 variables : T :: Variables ,
163162 ) -> Result < ResponseResult < T :: ResponseData > , anyhow:: Error > {
164- if let Some ( ref status_rx) = self . status {
165- //change
166- while status_rx. borrow ( ) . is_none ( ) { dbg ! ( "looping" ) ; } ;
167- let some_ref = status_rx. borrow ( ) ;
168- let deployment_status = some_ref. as_ref ( ) . unwrap ( ) ;
169-
170- if !deployment_status. synced || & deployment_status. health != "healthy" {
171- return Err ( anyhow ! (
172- "Deployment `{}` is not ready or healthy to be queried" ,
173- self . query_url
174- ) ) ;
175- }
176- }
163+ self . check_deployment_status ( ) ?;
177164
178165 let body = T :: build_query ( variables) ;
179166 let reqwest_response = self
@@ -194,20 +181,7 @@ impl DeploymentClient {
194181 }
195182
196183 pub async fn query_raw ( & self , body : Bytes ) -> Result < reqwest:: Response , anyhow:: Error > {
197- if let Some ( ref status_rx) = self . status {
198- //change
199- while status_rx. borrow ( ) . is_none ( ) { dbg ! ( "looping" ) ; } ;
200- let some_ref = status_rx. borrow ( ) ;
201- let deployment_status = some_ref. as_ref ( ) . unwrap ( ) ;
202-
203- if !deployment_status. synced || & deployment_status. health != "healthy" {
204- return Err ( anyhow ! (
205- "Deployment `{}` is not ready or healthy to be queried" ,
206- self . query_url
207- ) ) ;
208- }
209- }
210-
184+ self . check_deployment_status ( ) ?;
211185 Ok ( self
212186 . http_client
213187 . post ( self . query_url . as_ref ( ) )
@@ -217,6 +191,27 @@ impl DeploymentClient {
217191 . send ( )
218192 . await ?)
219193 }
194+ fn check_deployment_status ( & self ) ->Result < ( ) , anyhow:: Error > {
195+ if let Some ( ref status_rx) = self . status {
196+ let status_ref = status_rx. borrow ( ) ;
197+ let status = status_ref. as_ref ( ) ;
198+ match status{
199+ Some ( deployment_status) =>{
200+ if !deployment_status. synced || & deployment_status. health != "healthy" {
201+ return Err ( anyhow ! (
202+ "Deployment `{}` is not ready or healthy to be queried" ,
203+ self . query_url
204+ ) ) ;
205+ }
206+ }
207+ None => return Err ( anyhow ! (
208+ "Deployment `{}` is not available" ,
209+ self . query_url
210+ ) )
211+ }
212+ }
213+ Ok ( ( ) )
214+ }
220215}
221216
222217/// Client for a subgraph that can fall back from a local deployment to a remote query URL
@@ -299,6 +294,7 @@ impl SubgraphClient {
299294#[ cfg( test) ]
300295mod test {
301296 use std:: str:: FromStr ;
297+ use std:: time:: Duration ;
302298
303299 use serde_json:: json;
304300 use wiremock:: matchers:: { method, path} ;
@@ -445,6 +441,9 @@ mod test {
445441 . unwrap ( ) ,
446442 ) ;
447443
444+ // Waiting for status to propegate
445+ tokio:: time:: sleep ( Duration :: from_millis ( 1000 ) ) . await ;
446+
448447 // Query the subgraph
449448 let data = client
450449 . query :: < UserQuery , _ > ( user_query:: Variables { } )
@@ -525,6 +524,9 @@ mod test {
525524 . unwrap ( ) ,
526525 ) ;
527526
527+ // Waiting for status to propegate
528+ tokio:: time:: sleep ( Duration :: from_millis ( 100 ) ) . await ;
529+
528530 // Query the subgraph
529531 let data = client
530532 . query :: < UserQuery , _ > ( user_query:: Variables { } )
@@ -605,6 +607,9 @@ mod test {
605607 . unwrap ( ) ,
606608 ) ;
607609
610+ // Waiting for status to propegate
611+ tokio:: time:: sleep ( Duration :: from_millis ( 100 ) ) . await ;
612+
608613 // Query the subgraph
609614 let data = client
610615 . query :: < UserQuery , _ > ( user_query:: Variables { } )
@@ -614,4 +619,4 @@ mod test {
614619
615620 assert_eq ! ( data. user. name, "remote" . to_string( ) ) ;
616621 }
617- }
622+ }
0 commit comments