|
1 | 1 | use std::{sync::Arc, time::Duration}; |
2 | 2 |
|
3 | | -use pyo3::{Bound, FromPyObject, IntoPyObjectExt, PyAny, Python}; |
4 | | -use tokio::sync::RwLock; |
| 3 | +use futures_util::StreamExt; |
| 4 | +use pyo3::{Bound, FromPyObject, IntoPyObjectExt, PyAny, PyRef, Python}; |
| 5 | +use tokio::sync::{Mutex, RwLock}; |
5 | 6 |
|
6 | 7 | use crate::{ |
7 | 8 | exceptions::rust_err::{NatsrpyError, NatsrpyResult}, |
8 | 9 | js::consumers::{self, pull::PullConsumer, push::PushConsumer}, |
9 | | - utils::{natsrpy_future, py_types::TimeValue}, |
| 10 | + utils::{ |
| 11 | + futures::natsrpy_future_with_timeout, natsrpy_future, py_types::TimeValue, |
| 12 | + streamer::Streamer, |
| 13 | + }, |
10 | 14 | }; |
11 | 15 |
|
| 16 | +#[pyo3::pyclass] |
| 17 | +pub struct ConsumersIterator { |
| 18 | + streamer: Arc< |
| 19 | + Mutex< |
| 20 | + Streamer< |
| 21 | + Result< |
| 22 | + async_nats::jetstream::consumer::Info, |
| 23 | + async_nats::jetstream::stream::ConsumersError, |
| 24 | + >, |
| 25 | + >, |
| 26 | + >, |
| 27 | + >, |
| 28 | + stream: Arc<RwLock<async_nats::jetstream::stream::Stream<async_nats::jetstream::stream::Info>>>, |
| 29 | +} |
| 30 | + |
| 31 | +#[pyo3::pyclass] |
| 32 | +pub struct ConsumersNamesIterator { |
| 33 | + streamer: Arc<Mutex<Streamer<Result<String, async_nats::jetstream::stream::ConsumersError>>>>, |
| 34 | +} |
| 35 | + |
| 36 | +impl ConsumersNamesIterator { |
| 37 | + #[must_use] |
| 38 | + pub fn new( |
| 39 | + streamer: Streamer<Result<String, async_nats::jetstream::stream::ConsumersError>>, |
| 40 | + ) -> Self { |
| 41 | + Self { |
| 42 | + streamer: Arc::new(Mutex::new(streamer)), |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +#[pyo3::pymethods] |
| 48 | +impl ConsumersNamesIterator { |
| 49 | + #[must_use] |
| 50 | + pub const fn __aiter__(slf: PyRef<Self>) -> PyRef<Self> { |
| 51 | + slf |
| 52 | + } |
| 53 | + |
| 54 | + #[pyo3(signature=(timeout=None))] |
| 55 | + pub fn next<'py>( |
| 56 | + &self, |
| 57 | + py: Python<'py>, |
| 58 | + timeout: Option<TimeValue>, |
| 59 | + ) -> NatsrpyResult<Bound<'py, PyAny>> { |
| 60 | + let ctx = self.streamer.clone(); |
| 61 | + natsrpy_future_with_timeout(py, timeout, async move { |
| 62 | + let value = ctx.lock().await.next().await; |
| 63 | + match value { |
| 64 | + Some(name) => Ok(name?), |
| 65 | + None => Err(NatsrpyError::AsyncStopIteration), |
| 66 | + } |
| 67 | + }) |
| 68 | + } |
| 69 | + |
| 70 | + pub fn __anext__<'py>(&self, py: Python<'py>) -> NatsrpyResult<Bound<'py, PyAny>> { |
| 71 | + self.next(py, None) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +impl ConsumersIterator { |
| 76 | + pub fn new( |
| 77 | + stream: Arc< |
| 78 | + RwLock<async_nats::jetstream::stream::Stream<async_nats::jetstream::stream::Info>>, |
| 79 | + >, |
| 80 | + streamer: Streamer< |
| 81 | + Result< |
| 82 | + async_nats::jetstream::consumer::Info, |
| 83 | + async_nats::jetstream::stream::ConsumersError, |
| 84 | + >, |
| 85 | + >, |
| 86 | + ) -> Self { |
| 87 | + Self { |
| 88 | + stream, |
| 89 | + streamer: Arc::new(Mutex::new(streamer)), |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +#[pyo3::pymethods] |
| 95 | +impl ConsumersIterator { |
| 96 | + #[must_use] |
| 97 | + pub const fn __aiter__(slf: PyRef<Self>) -> PyRef<Self> { |
| 98 | + slf |
| 99 | + } |
| 100 | + |
| 101 | + #[pyo3(signature=(timeout=None))] |
| 102 | + pub fn next<'py>( |
| 103 | + &self, |
| 104 | + py: Python<'py>, |
| 105 | + timeout: Option<TimeValue>, |
| 106 | + ) -> NatsrpyResult<Bound<'py, PyAny>> { |
| 107 | + let ctx = self.streamer.clone(); |
| 108 | + let stream = self.stream.clone(); |
| 109 | + natsrpy_future_with_timeout(py, timeout, async move { |
| 110 | + let value = ctx.lock().await.next().await; |
| 111 | + match value { |
| 112 | + Some(info) => { |
| 113 | + let info = info?; |
| 114 | + let Some(consumer_name) = info.config.name else { |
| 115 | + return Err(NatsrpyError::SessionError(String::from( |
| 116 | + "Received consumer without a name.", |
| 117 | + ))); |
| 118 | + }; |
| 119 | + // That means that the consumer is PushBased. |
| 120 | + if info.config.deliver_subject.is_some() { |
| 121 | + let consumer = consumers::push::consumer::PushConsumer::new( |
| 122 | + stream.read().await.get_consumer(&consumer_name).await?, |
| 123 | + ); |
| 124 | + Ok(Python::attach(|py| consumer.into_py_any(py))?) |
| 125 | + } else { |
| 126 | + let consumer = consumers::pull::consumer::PullConsumer::new( |
| 127 | + stream.read().await.get_consumer(&consumer_name).await?, |
| 128 | + ); |
| 129 | + Ok(Python::attach(|py| consumer.into_py_any(py))?) |
| 130 | + } |
| 131 | + } |
| 132 | + None => Err(NatsrpyError::AsyncStopIteration), |
| 133 | + } |
| 134 | + }) |
| 135 | + } |
| 136 | + |
| 137 | + pub fn __anext__<'py>(&self, py: Python<'py>) -> NatsrpyResult<Bound<'py, PyAny>> { |
| 138 | + self.next(py, None) |
| 139 | + } |
| 140 | +} |
| 141 | + |
12 | 142 | #[pyo3::pyclass] |
13 | 143 | pub struct ConsumersManager { |
14 | 144 | stream: Arc<RwLock<async_nats::jetstream::stream::Stream<async_nats::jetstream::stream::Info>>>, |
@@ -146,4 +276,23 @@ impl ConsumersManager { |
146 | 276 | Ok(ctx.read().await.delete_consumer(&name).await?.success) |
147 | 277 | }) |
148 | 278 | } |
| 279 | + |
| 280 | + pub fn list<'py>(&self, py: Python<'py>) -> NatsrpyResult<Bound<'py, PyAny>> { |
| 281 | + let ctx = self.stream.clone(); |
| 282 | + natsrpy_future(py, async move { |
| 283 | + let consumers = ctx.read().await.consumers(); |
| 284 | + Ok(ConsumersIterator::new( |
| 285 | + ctx.clone(), |
| 286 | + Streamer::new(consumers), |
| 287 | + )) |
| 288 | + }) |
| 289 | + } |
| 290 | + |
| 291 | + pub fn list_names<'py>(&self, py: Python<'py>) -> NatsrpyResult<Bound<'py, PyAny>> { |
| 292 | + let ctx = self.stream.clone(); |
| 293 | + natsrpy_future(py, async move { |
| 294 | + let consumers = ctx.read().await.consumer_names(); |
| 295 | + Ok(ConsumersNamesIterator::new(Streamer::new(consumers))) |
| 296 | + }) |
| 297 | + } |
149 | 298 | } |
0 commit comments