use std::convert::Infallible;
use std::fmt::Display;
use std::future::Future;
use std::sync::Arc;
use std::time::Duration;
use poem::{Route, Server, listener::TcpListener};
use puerto_nucleo::eventos::ManejadorEvento;
use puerto_nucleo::eventos::transporte::{
DestinoDescartados, Outbox, PublicadorEventos, SuscripcionEventos,
};
use puerto_web::app::{PLAZO_APAGADO, iniciar_tracing, senal_apagado};
use puerto_web::{Error, health};
use tokio::sync::watch;
use tokio::time::Instant;
use crate::ConfigWorker;
use crate::conductor::{Latido, Pasada, conducir};
use crate::consumidor::Consumidor;
use crate::relay::Relay;
pub struct Bucles {
config: ConfigWorker,
loops: Vec<(Box<dyn Pasada>, Duration)>,
}
impl Bucles {
pub fn new(config: &ConfigWorker) -> Self {
Self {
config: config.clone(),
loops: Vec::new(),
}
}
pub fn relay<O, P>(mut self, outbox: O, publicador: P) -> Self
where
O: Outbox + 'static,
P: PublicadorEventos + 'static,
{
let relay = Relay::new(outbox, publicador).con_tamano_lote(self.config.tamano_lote_relay);
self.loops
.push((Box::new(relay), self.config.intervalo_sondeo_relay));
self
}
pub fn consumidor<S>(
mut self,
suscripcion: S,
handler: impl ManejadorEvento + 'static,
dead_letters: Arc<dyn DestinoDescartados>,
) -> Self
where
S: SuscripcionEventos + 'static,
{
let consumidor = Consumidor::new(suscripcion, handler, dead_letters)
.con_entregas_maximas(self.config.max_deliveries)
.con_espera_maxima_reintento(self.config.espera_maxima_reintento);
self.loops.push((
Box::new(consumidor),
self.config.intervalo_sondeo_consumidor,
));
self
}
pub fn nombres(&self) -> Vec<String> {
self.loops.iter().map(|(work, _)| work.name()).collect()
}
}
pub struct Worker {
config: ConfigWorker,
}
impl Worker {
pub fn new(config: ConfigWorker) -> Self {
Self { config }
}
pub async fn ejecutar<F, Fut>(self, build: F) -> Result<(), Error>
where
F: FnOnce(ConfigWorker) -> Fut,
Fut: Future<Output = Bucles>,
{
self.intentar_ejecutar(|config| async move { Ok::<_, Infallible>(build(config).await) })
.await
}
pub async fn intentar_ejecutar<F, Fut, X>(self, build: F) -> Result<(), Error>
where
F: FnOnce(ConfigWorker) -> Fut,
Fut: Future<Output = Result<Bucles, X>>,
X: Display,
{
iniciar_tracing();
let loops = build(self.config.clone()).await.map_err(|e| {
let message = e.to_string();
tracing::error!(error = %message, "el arranque falló");
Error::Arranque(message)
})?;
self.run_until(loops, senal_apagado()).await
}
async fn run_until(
self,
loops: Bucles,
signal: impl Future<Output = ()> + Send + 'static,
) -> Result<(), Error> {
let (stop, shutdown) = watch::channel(false);
let stop = Arc::new(stop);
let mut heartbeats = Vec::new();
let mut tasks = Vec::new();
for (work, interval) in loops.loops {
let heartbeat = Arc::new(Latido::new(work.name(), interval));
heartbeats.push(Arc::clone(&heartbeat));
tasks.push(tokio::spawn(conducir(
work,
interval,
heartbeat,
shutdown.clone(),
)));
}
let ready = readiness(heartbeats);
let stopping = Arc::clone(&stop);
let served = Server::new(TcpListener::bind(format!("0.0.0.0:{}", self.config.port)))
.run_with_graceful_shutdown(
health::montar(Route::new(), ready),
async move {
signal.await;
let _ = stopping.send(true);
},
Some(PLAZO_APAGADO),
)
.await;
let _ = stop.send(true);
let drained = tokio::time::timeout(PLAZO_APAGADO, async {
for task in tasks {
let _ = task.await;
}
})
.await;
if drained.is_err() {
tracing::warn!("worker: un bucle no terminó su pasada antes del plazo de apagado");
}
served?;
Ok(())
}
}
fn readiness(heartbeats: Vec<Arc<Latido>>) -> health::SondaDisponibilidad {
let heartbeats = Arc::new(heartbeats);
health::sondear(move || {
let heartbeats = Arc::clone(&heartbeats);
async move { stale(&heartbeats, Instant::now()).is_empty() }
})
}
fn stale(heartbeats: &[Arc<Latido>], now: Instant) -> Vec<&str> {
heartbeats
.iter()
.filter(|beat| !beat.esta_fresco(now))
.map(|beat| beat.name())
.inspect(|name| tracing::warn!(name, "worker: not ready"))
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::pruebas::{DestinoFalso, Guion, OutboxFalso, PublicadorFalso, SuscripcionFalsa};
#[tokio::test]
async fn should_stop_with_the_build_error_when_the_loops_cannot_be_built() {
let worker = Worker::new(ConfigWorker::default());
let result = worker
.intentar_ejecutar(|_| async { Err::<Bucles, _>("the database is down") })
.await;
let error = result.unwrap_err();
assert!(matches!(error, Error::Arranque(_)), "{error:?}");
assert_eq!(format!("{error:?}"), "the database is down");
}
#[test]
fn should_build_the_relay_and_each_consumer_when_registered() {
let config = ConfigWorker::default();
let loops = Bucles::new(&config)
.relay(OutboxFalso::default(), PublicadorFalso::default())
.consumidor(
SuscripcionFalsa::default(),
Guion::Ack,
Arc::new(DestinoFalso::default()),
);
assert_eq!(loops.nombres(), ["relay", "consumidor notify_new_product"]);
}
#[test]
fn should_run_each_loop_at_its_configured_interval() {
let config = ConfigWorker {
intervalo_sondeo_relay: Duration::from_millis(50),
intervalo_sondeo_consumidor: Duration::from_millis(75),
..ConfigWorker::default()
};
let loops = Bucles::new(&config)
.relay(OutboxFalso::default(), PublicadorFalso::default())
.consumidor(
SuscripcionFalsa::default(),
Guion::Ack,
Arc::new(DestinoFalso::default()),
);
let intervals: Vec<_> = loops.loops.iter().map(|(_, i)| *i).collect();
assert_eq!(
intervals,
[Duration::from_millis(50), Duration::from_millis(75)]
);
}
#[test]
fn should_name_the_stale_loops_until_every_loop_is_fresh() {
let now = Instant::now();
let relay = Arc::new(Latido::new("relay".into(), Duration::from_millis(500)));
let consumidor = Arc::new(Latido::new("consumer".into(), Duration::from_millis(500)));
let heartbeats = vec![Arc::clone(&relay), Arc::clone(&consumidor)];
relay.beat_at(now);
let one_fresh = stale(&heartbeats, now);
consumidor.beat_at(now);
let both_fresh = stale(&heartbeats, now);
assert_eq!(one_fresh, ["consumer"]);
assert!(both_fresh.is_empty());
}
#[tokio::test]
async fn should_drain_the_loops_and_return_when_the_shutdown_signal_arrives() {
let outbox = OutboxFalso::with(&[crate::pruebas::envelope("product_created")]);
let publicador = PublicadorFalso::aceptando(usize::MAX);
let config = ConfigWorker::default().con_puerto(0);
let loops = Bucles::new(&config).relay(outbox.clone(), publicador.clone());
let signal = async { tokio::time::sleep(Duration::from_millis(200)).await };
let result = tokio::time::timeout(
Duration::from_secs(5),
Worker::new(config).run_until(loops, signal),
)
.await;
assert!(matches!(result, Ok(Ok(()))), "{result:?}");
assert!(outbox.sin_publicar().is_empty());
}
#[tokio::test]
async fn should_stop_the_loops_and_fail_when_the_port_is_taken() {
let taken = std::net::TcpListener::bind("0.0.0.0:0").unwrap();
let port = taken.local_addr().unwrap().port();
let config = ConfigWorker::default().con_puerto(port);
let loops = Bucles::new(&config).relay(OutboxFalso::default(), PublicadorFalso::default());
let result = tokio::time::timeout(
Duration::from_secs(5),
Worker::new(config).run_until(loops, std::future::pending()),
)
.await;
assert!(matches!(result, Ok(Err(Error::Servidor(_)))), "{result:?}");
}
}