| 182 | } |
| 183 | |
| 184 | pub async fn wait_healthcheck<F, Ret>( |
| 185 | func: F, |
| 186 | args: &HealthcheckArgs, |
| 187 | ) -> Result<(), Report<HealthcheckError>> |
| 188 | where |
| 189 | F: Fn() -> Ret + Send, |
| 190 | Ret: Future<Output = Result<(), Report<HealthcheckError>>> + Send, |
| 191 | { |
| 192 | let expected_end_time = args |
| 193 | .timeout |
| 194 | .map(|timeout| Instant::now() + Duration::from_secs(timeout)); |
| 195 | |
| 196 | loop { |
| 197 | if func().await.is_ok() { |
| 198 | return Ok(()); |
| 199 | } |
| 200 | ensure!(args.wait, HealthcheckError::NotHealthy); |
| 201 | if let Some(end_time) = expected_end_time |
| 202 | && Instant::now() > end_time |
| 203 | { |
| 204 | return Err(HealthcheckError::Timeout.into()); |
| 205 | } |
| 206 | sleep(Duration::from_secs(1)).await; |
| 207 | } |
| 208 | } |