use tokio::sync::oneshot;
use tokio::task::JoinHandle;
use torrust_tracker_configuration::HealthCheckApi;
use tracing::instrument;
use super::Started;
use crate::servers::health_check_api::{server, HEALTH_CHECK_API_LOG_TARGET};
use crate::servers::logging::STARTED_ON;
use crate::servers::registar::ServiceRegistry;
use crate::servers::signals::Halted;
#[allow(clippy::async_yields_async)]
#[instrument(skip(config, register))]
pub async fn start_job(config: &HealthCheckApi, register: ServiceRegistry) -> JoinHandle<()> {
let bind_addr = config.bind_address;
let (tx_start, rx_start) = oneshot::channel::<Started>();
let (tx_halt, rx_halt) = tokio::sync::oneshot::channel::<Halted>();
let protocol = "http";
let join_handle = tokio::spawn(async move {
tracing::info!(target: HEALTH_CHECK_API_LOG_TARGET, "Starting on: {protocol}://{}", bind_addr);
let handle = server::start(bind_addr, tx_start, rx_halt, register);
if let Ok(()) = handle.await {
tracing::info!(target: HEALTH_CHECK_API_LOG_TARGET, "Stopped server running on: {protocol}://{}", bind_addr);
}
});
match rx_start.await {
Ok(msg) => tracing::info!(target: HEALTH_CHECK_API_LOG_TARGET, "{STARTED_ON}: {protocol}://{}", msg.address),
Err(e) => panic!("the Health Check API server was dropped: {e}"),
}
tokio::spawn(async move {
assert!(!tx_halt.is_closed(), "Halt channel for Health Check API should be open");
join_handle
.await
.expect("it should be able to join to the Health Check API server task");
})
}