queue consumers: stop taking messages on sigterm and exit 0
This commit is contained in:
parent
a089da25a5
commit
a8775f1a7a
1 changed files with 164 additions and 53 deletions
|
|
@ -33,11 +33,15 @@ impl Pulled for async_nats::jetstream::Message {
|
|||
/// Handle each event of `batch` with `handle` and settle it. Returns the
|
||||
/// error of the first event that has to be retried, once the rest of the
|
||||
/// batch is nakked behind it.
|
||||
///
|
||||
/// Once `stop` resolves no further event is taken: the one being handled is
|
||||
/// settled, and the rest stay un-acked and come back after the ack wait.
|
||||
pub(crate) async fn batch<P: Pulled>(
|
||||
batch: impl Stream<Item = Result<P>>,
|
||||
stop: impl Future<Output = ()>,
|
||||
mut handle: impl AsyncFnMut(&P) -> Result<(), Skip>,
|
||||
) -> Result<()> {
|
||||
let mut batch = std::pin::pin!(batch);
|
||||
let mut batch = std::pin::pin!(batch.take_until(stop));
|
||||
let mut failed: Option<anyhow::Error> = None;
|
||||
while let Some(event) = batch.next().await {
|
||||
let event = event?;
|
||||
|
|
@ -102,14 +106,18 @@ mod tests {
|
|||
})
|
||||
});
|
||||
let mut handled = Vec::new();
|
||||
let result = batch(futures_util::stream::iter(events), async |e: &Fake<'_>| {
|
||||
handled.push(e.name);
|
||||
if e.name == "assigned" {
|
||||
Err(Skip::Retry(anyhow::anyhow!("the queue is down")))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
})
|
||||
let result = batch(
|
||||
futures_util::stream::iter(events),
|
||||
std::future::pending(),
|
||||
async |e: &Fake<'_>| {
|
||||
handled.push(e.name);
|
||||
if e.name == "assigned" {
|
||||
Err(Skip::Retry(anyhow::anyhow!("the queue is down")))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
},
|
||||
)
|
||||
.await;
|
||||
assert!(result.is_err());
|
||||
assert_eq!(handled, ["opened", "assigned"]);
|
||||
|
|
@ -135,13 +143,17 @@ mod tests {
|
|||
settled: &settled,
|
||||
})
|
||||
});
|
||||
let result = batch(futures_util::stream::iter(events), async |e: &Fake<'_>| {
|
||||
if e.name == "bad" {
|
||||
Err(Skip::Unusable("no".to_owned()))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
})
|
||||
let result = batch(
|
||||
futures_util::stream::iter(events),
|
||||
std::future::pending(),
|
||||
async |e: &Fake<'_>| {
|
||||
if e.name == "bad" {
|
||||
Err(Skip::Unusable("no".to_owned()))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
},
|
||||
)
|
||||
.await;
|
||||
assert!(result.is_ok());
|
||||
assert_eq!(
|
||||
|
|
@ -152,4 +164,44 @@ mod tests {
|
|||
]
|
||||
);
|
||||
}
|
||||
|
||||
/// The stop arrives while `assigned` is being handled: it is still
|
||||
/// settled, and `unassigned` and `closed` are neither handled nor settled,
|
||||
/// so the queue redelivers them.
|
||||
#[tokio::test]
|
||||
async fn after_the_stop_the_event_in_hand_is_settled_and_the_rest_is_left_unacked() {
|
||||
let settled = RefCell::new(Vec::new());
|
||||
let events = ["opened", "assigned", "unassigned", "closed"].map(|name| {
|
||||
Ok(Fake {
|
||||
name,
|
||||
settled: &settled,
|
||||
})
|
||||
});
|
||||
let (stop_tx, stop_rx) = tokio::sync::oneshot::channel::<()>();
|
||||
let mut stop_tx = Some(stop_tx);
|
||||
let mut handled = Vec::new();
|
||||
let result = batch(
|
||||
futures_util::stream::iter(events),
|
||||
async {
|
||||
let _ = stop_rx.await;
|
||||
},
|
||||
async |e: &Fake<'_>| {
|
||||
handled.push(e.name);
|
||||
if e.name == "assigned" {
|
||||
stop_tx.take().expect("sent once").send(()).expect("stop");
|
||||
}
|
||||
Ok(())
|
||||
},
|
||||
)
|
||||
.await;
|
||||
assert!(result.is_ok());
|
||||
assert_eq!(handled, ["opened", "assigned"]);
|
||||
assert_eq!(
|
||||
settled.into_inner(),
|
||||
[
|
||||
("opened", format!("{:?}", AckKind::Ack)),
|
||||
("assigned", format!("{:?}", AckKind::Ack)),
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue