WorkQueue

Struct WorkQueue 

Source
pub struct WorkQueue<T, C: CodecType> { /* private fields */ }
Expand description

A typed work queue backed by JetStream with configurable codec.

Work queues provide at-least-once delivery with automatic acknowledgment tracking. Messages are removed from the queue once acknowledged.

§Type Parameters

  • T - The message type for this queue
  • C - The codec type used for serialization

§Example

use intercom::{Client, MsgPackCodec, jetstream::queue::WorkQueue};
use serde::{Deserialize, Serialize};
use futures::StreamExt;

#[derive(Serialize, Deserialize, Debug)]
struct Job {
    id: u64,
    payload: String,
}

let client = Client::<MsgPackCodec>::connect("nats://localhost:4222").await?;
let jetstream = client.jetstream();

// Create a work queue
let queue = WorkQueue::<Job, MsgPackCodec>::builder(&jetstream, "jobs")
    .max_messages(10_000)
    .create()
    .await?;

// Push a job
queue.push(&Job { id: 1, payload: "do work".into() }).await?;

// Option 1: Use as a Stream (pulls one job at a time)
let mut queue = queue.into_stream().await?;
while let Some(result) = queue.next().await {
    let job = result?;
    println!("Processing: {:?}", job.payload);
    job.ack().await?;
}

// Option 2: Pull a batch of jobs
// let mut jobs = queue.pull(10).await?;
// while let Some(result) = jobs.next().await {
//     let job = result?;
//     job.ack().await?;
// }

Implementations§

Source§

impl<T, C: CodecType> WorkQueue<T, C>

Source

pub fn builder( context: &JetStreamContext<C>, name: &str, ) -> WorkQueueBuilder<T, C>

Create a work queue builder.

Source

pub fn stream(&self) -> &JsStream<C>

Get the stream backing this queue.

Source

pub fn consumer(&self) -> &PullConsumer<T, C>

Get the consumer for this queue.

Source

pub fn subject(&self) -> &str

Get the subject for this queue.

Source§

impl<T: Serialize, C: CodecType> WorkQueue<T, C>

Source

pub async fn push(&self, message: &T) -> Result<u64>

Push a message to the queue.

Source

pub fn sink(&self) -> WorkQueueSink<T, C>

Create a sink for pushing messages.

Source§

impl<T: DeserializeOwned, C: CodecType> WorkQueue<T, C>

Source

pub async fn pull(&self, batch_size: usize) -> Result<PullBatch<T, C>>

Pull a batch of messages from the queue.

Source

pub async fn messages(&self) -> Result<PullMessages<T, C>>

Get a continuous stream of messages.

Source

pub async fn into_stream(self) -> Result<StreamingWorkQueue<T, C>>

Convert this queue into a Stream that continuously pulls messages one at a time.

This is the most ergonomic way to process queue messages when you want to handle them one at a time in a loop.

§Example
use intercom::{Client, MsgPackCodec, jetstream::queue::WorkQueue};
use serde::{Deserialize, Serialize};
use futures::StreamExt;

#[derive(Serialize, Deserialize, Debug)]
struct Job { id: u64 }

let client = Client::<MsgPackCodec>::connect("nats://localhost:4222").await?;
let jetstream = client.jetstream();
let queue = WorkQueue::<Job, MsgPackCodec>::builder(&jetstream, "jobs").create().await?;

// Convert to a Stream and iterate
let mut queue = queue.into_stream().await?;
while let Some(job) = queue.next().await {
    let job = job?;
    println!("Processing job: {:?}", job.payload);
    job.ack().await?;
}

Auto Trait Implementations§

§

impl<T, C> Freeze for WorkQueue<T, C>

§

impl<T, C> !RefUnwindSafe for WorkQueue<T, C>

§

impl<T, C> Send for WorkQueue<T, C>
where T: Send,

§

impl<T, C> Sync for WorkQueue<T, C>
where T: Sync,

§

impl<T, C> Unpin for WorkQueue<T, C>
where C: Unpin, T: Unpin,

§

impl<T, C> !UnwindSafe for WorkQueue<T, C>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more