Watch
0
0
Fork
You've already forked hyperhive
0

queue consumers: stop taking messages on sigterm and exit 0

This commit is contained in:
atlas 2026-11-18 03:28:12 +01:00 • committed by mara
commit a8775f1a7a

View file

@ -35,7 +35,7 @@ use std::time::{Duration, Instant};
use anyhow::{Context as _, Result, bail};
use async_nats::jetstream::{self, consumer::pull, kv, message::PublishMessage};
use futures_util::StreamExt as _;
use futures_util::{FutureExt as _, StreamExt as _};
use metrics::Handled;
use swarm_queue_client::{ClientSecret, QueueConfig};
use swarm_secret_client::stored_secret::StoredSecret;
@ -79,6 +79,7 @@ const ROSTER_TTL: Duration = Duration::from_mins(1);
#[tokio::main]
async fn main() -> Result<()> {
let stop = swarm_secret_client::shutdown_signal();
hive_log::init();
swarm_secret_client::install_crypto_provider();
let var = |name: &str| {
@ -113,16 +114,26 @@ async fn main() -> Result<()> {
stream = forge_events::STREAM,
"notify router: routing forge events"
);
loop {
if let Err(e) = router.pull_once().await {
router.metrics.pull_failed();
tracing::warn!(
error = format!("{e:#}"),
"notify router: pulling events failed; retrying"
);
tokio::time::sleep(RETRY_DELAY).await;
while stop.clone().now_or_never().is_none() {
let pulled =
swarm_secret_client::serve_bounded(stop.clone(), router.pull_once(stop.clone())).await;
match pulled {
Some(Ok(())) => {}
Some(Err(e)) => {
router.metrics.pull_failed();
tracing::warn!(
error = format!("{e:#}"),
"notify router: pulling events failed; retrying"
);
tokio::select! {
() = stop.clone() => {}
() = tokio::time::sleep(RETRY_DELAY) => {}
}
}
None => break,
}
}
Ok(())
}
struct Router {
@ -184,8 +195,11 @@ impl Router {
})
}
/// Pull one batch and handle each event in stream order.
async fn pull_once(&mut self) -> Result<()> {
/// Pull one batch and handle each event in stream order, taking no event
/// after `stop` resolves. The one being handled finishes and is settled;
/// the rest of the batch is left un-acked and comes back after the ack
/// wait.
async fn pull_once(&mut self, stop: swarm_secret_client::Shutdown) -> Result<()> {
if let Ok(info) = self.events.info().await {
self.metrics
.consumer(info.num_pending, info.num_ack_pending, info.num_redelivered);
@ -199,7 +213,10 @@ impl Router {
.await
.context("requesting a batch")?;
let batch = batch.map(|m| m.map_err(|e| anyhow::anyhow!("{e}")));
settle::batch(batch, async |m: &jetstream::Message| self.handle(m).await).await
settle::batch(batch, stop, async |m: &jetstream::Message| {
self.handle(m).await
})
.await
}
/// Handle one event and count how it went.