pub struct Queue<E: TitoEngine> {
pub engine: E,
pub config: QueueConfig,
}Fields§
§engine: E§config: QueueConfigImplementations§
Source§impl<E: TitoEngine> Queue<E>
impl<E: TitoEngine> Queue<E>
Sourcepub fn new(engine: E, config: QueueConfig) -> Self
pub fn new(engine: E, config: QueueConfig) -> Self
Examples found in repository?
examples/queue_fifo.rs (lines 36-39)
31async fn main() -> Result<(), TitoError> {
32 println!("Testing FIFO Queue\n");
33
34 let tito_db = TiKV::connect(vec!["127.0.0.1:2379"]).await?;
35
36 let queue = Arc::new(TitoQueue::new(
37 tito_db.clone(),
38 QueueConfig::with_partitions(1),
39 ));
40
41 println!("Publishing 5 events...\n");
42
43 for i in 1..=5 {
44 let user_id = DBUuid::new_v4().to_string();
45 let event = QueueEvent::new(
46 format!("user:{}", user_id),
47 UserEvent {
48 user_id,
49 name: format!("User {}", i),
50 email: format!("user{}@example.com", i),
51 action: "created".to_string(),
52 },
53 );
54
55 queue.publish(event).await?;
56 println!("Published event for User {}", i);
57 }
58
59 println!("\nStarting worker...\n");
60
61 let events_processed = Arc::new(std::sync::atomic::AtomicU32::new(0));
62 let events_processed_clone = events_processed.clone();
63
64 let (shutdown_tx, shutdown_rx) = broadcast::channel(1);
65
66 let handler = move |event: QueueEvent<UserEvent>| {
67 let counter = events_processed_clone.clone();
68 Box::pin(async move {
69 let count = counter.fetch_add(1, Ordering::SeqCst) + 1;
70 println!(
71 "[{}] {} - {} ({})",
72 count,
73 event.payload.action,
74 event.payload.name,
75 event.payload.email,
76 );
77 tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
78 Ok::<_, TitoError>(())
79 }) as BoxFuture<'static, Result<(), TitoError>>
80 };
81
82 let worker_handle = run_worker(
83 queue.clone(),
84 WorkerConfig {
85 event_type: String::from("user"),
86 consumer: String::from("example-consumer"),
87 partition_range: 0..1,
88 },
89 handler,
90 shutdown_rx,
91 )
92 .await;
93
94 tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
95
96 println!("\nShutting down...");
97 let _ = shutdown_tx.send(());
98 let _ = worker_handle.await;
99
100 let total = events_processed.load(Ordering::SeqCst);
101 println!("\nProcessed {} events", total);
102
103 Ok(())
104}pub async fn publish_in_tx<T: EventType + Serialize>( &self, event: QueueEvent<T>, tx: &E::Transaction, ) -> Result<(), TitoError>
Sourcepub async fn publish<T: EventType + Serialize>(
&self,
event: QueueEvent<T>,
) -> Result<(), TitoError>
pub async fn publish<T: EventType + Serialize>( &self, event: QueueEvent<T>, ) -> Result<(), TitoError>
Examples found in repository?
examples/queue_fifo.rs (line 55)
31async fn main() -> Result<(), TitoError> {
32 println!("Testing FIFO Queue\n");
33
34 let tito_db = TiKV::connect(vec!["127.0.0.1:2379"]).await?;
35
36 let queue = Arc::new(TitoQueue::new(
37 tito_db.clone(),
38 QueueConfig::with_partitions(1),
39 ));
40
41 println!("Publishing 5 events...\n");
42
43 for i in 1..=5 {
44 let user_id = DBUuid::new_v4().to_string();
45 let event = QueueEvent::new(
46 format!("user:{}", user_id),
47 UserEvent {
48 user_id,
49 name: format!("User {}", i),
50 email: format!("user{}@example.com", i),
51 action: "created".to_string(),
52 },
53 );
54
55 queue.publish(event).await?;
56 println!("Published event for User {}", i);
57 }
58
59 println!("\nStarting worker...\n");
60
61 let events_processed = Arc::new(std::sync::atomic::AtomicU32::new(0));
62 let events_processed_clone = events_processed.clone();
63
64 let (shutdown_tx, shutdown_rx) = broadcast::channel(1);
65
66 let handler = move |event: QueueEvent<UserEvent>| {
67 let counter = events_processed_clone.clone();
68 Box::pin(async move {
69 let count = counter.fetch_add(1, Ordering::SeqCst) + 1;
70 println!(
71 "[{}] {} - {} ({})",
72 count,
73 event.payload.action,
74 event.payload.name,
75 event.payload.email,
76 );
77 tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
78 Ok::<_, TitoError>(())
79 }) as BoxFuture<'static, Result<(), TitoError>>
80 };
81
82 let worker_handle = run_worker(
83 queue.clone(),
84 WorkerConfig {
85 event_type: String::from("user"),
86 consumer: String::from("example-consumer"),
87 partition_range: 0..1,
88 },
89 handler,
90 shutdown_rx,
91 )
92 .await;
93
94 tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
95
96 println!("\nShutting down...");
97 let _ = shutdown_tx.send(());
98 let _ = worker_handle.await;
99
100 let total = events_processed.load(Ordering::SeqCst);
101 println!("\nProcessed {} events", total);
102
103 Ok(())
104}pub async fn pull<T: EventType + DeserializeOwned>( &self, event_type: &str, partition: u32, limit: u32, ) -> Result<Vec<(String, QueueEvent<T>)>, TitoError>
pub async fn ack(&self, key: &str) -> Result<(), TitoError>
pub async fn reschedule<T: EventType + Serialize>( &self, event: QueueEvent<T>, storage_key: &str, new_scheduled_at: i64, ) -> Result<(), TitoError>
pub async fn clear(&self) -> Result<(), TitoError>
pub async fn move_to_dlq<T: EventType + Serialize>( &self, event: QueueEvent<T>, storage_key: &str, ) -> Result<(), TitoError>
Trait Implementations§
Auto Trait Implementations§
impl<E> Freeze for Queue<E>where
E: Freeze,
impl<E> RefUnwindSafe for Queue<E>where
E: RefUnwindSafe,
impl<E> Send for Queue<E>
impl<E> Sync for Queue<E>
impl<E> Unpin for Queue<E>where
E: Unpin,
impl<E> UnsafeUnpin for Queue<E>where
E: UnsafeUnpin,
impl<E> UnwindSafe for Queue<E>where
E: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T in a tonic::Request