use std::time::Duration;
use crate::broker::backend_lifecycle::identity::DaemonProcess;
use crate::broker::backend_lifecycle::probe::{
self, EndpointProbeError, ProbeError, DEFAULT_ENDPOINT_PROBE_TIMEOUT,
};
use crate::broker::backend_lifecycle::verify_pid::{self, ProcessHandle};
use crate::broker::protocol::Endpoint;
pub async fn probe_endpoint_async(
endpoint: &Endpoint,
expected: &DaemonProcess,
) -> Result<ProcessHandle, ProbeError> {
if !probe::same_endpoint(endpoint, &expected.ipc_endpoint) {
return Err(ProbeError::EndpointMismatch);
}
let process_handle =
verify_pid::verify_daemon_process(expected).map_err(ProbeError::VerifyPid)?;
probe_endpoint_response_async(endpoint, expected).await?;
Ok(process_handle)
}
pub async fn probe_endpoint_response_async(
endpoint: &Endpoint,
expected: &DaemonProcess,
) -> Result<(), EndpointProbeError> {
probe_endpoint_response_with_timeout_async(endpoint, expected, DEFAULT_ENDPOINT_PROBE_TIMEOUT)
.await
}
pub async fn probe_endpoint_response_with_timeout_async(
endpoint: &Endpoint,
expected: &DaemonProcess,
timeout: Duration,
) -> Result<(), EndpointProbeError> {
let endpoint = endpoint.clone();
let expected = expected.clone();
match tokio::task::spawn_blocking(move || {
probe::probe_endpoint_response_with_timeout(&endpoint, &expected, timeout)
})
.await
{
Ok(result) => result,
Err(join_err) => Err(EndpointProbeError::Io(std::io::Error::other(format!(
"async probe worker thread panicked or was cancelled: {join_err}"
)))),
}
}