legit-streams 0.1.4

A Redis Streams work contract for task and result envelopes, with a worker loop that leases, reclaims and replies, a producer and a result reader
Documentation
//! A work contract over Redis Streams: task and result envelopes, a worker loop that leases,
//! reclaims and replies, a producer and a result reader, all testable without a server.
//!
//! A plain Rust library with no framework dependency and no async runtime: blocking calls and
//! threads. It speaks an existing contract, version 1 of an envelope that a PHP producer and Python
//! workers already exchange, so a Rust program can take part on either side. The crate's README
//! states the contract in full: its fields, lengths, lease rules and security model.
//!
//! - [`Task`] and [`TaskResult`] build and read the flat string field maps of the v1 task and
//!   result envelopes, and leave payloads to the caller.
//! - [`Worker`] reads a task stream with a consumer group, runs each task with a [`Handler`],
//!   renews its lease while it runs with one atomic step on the server, claims tasks that dead
//!   workers left idle, and writes each result to the stream its task names before it
//!   acknowledges the task.
//! - [`Producer`] sends tasks with a depth check, and [`ResultReader`] reads an application's
//!   results.
//! - The fast lane, for work a caller waits for: [`FastCaller::call`] sends a task with a reply key
//!   of its own and a deadline and blocks for the reply, and [`FastWorker`] answers it on that key,
//!   beside a bulk [`Worker`] or on its own.
//! - [`Streams`] is the trait behind them: [`RedisStreams`] on a server, [`MemoryStreams`] in
//!   memory on a [`Clock`] the test moves.
//!
//! ```
//! use std::time::Duration;
//! use legit_streams::{Clock, Failure, MemoryStreams, Producer, Reply, ResultReader, Task, Worker, WorkerConfig};
//!
//! let clock = Clock::manual(1_700_000_000_000);
//! let streams = MemoryStreams::with_clock(clock.clone());
//!
//! let task = Task::builder("app", "post", "42")
//!     .reply_to(legit_streams::reply_to("app", "hash"))
//!     .field("text_data", "hello")
//!     .build()?;
//! Producer::new(streams.clone(), "hash_tasks").send(&task)?;
//!
//! let config = WorkerConfig::new("hash_tasks", "hash_workers", "worker-1", "hash_results");
//! let worker = Worker::new(config, streams.clone(), |task: &Task, _: &_| {
//!     let text = task.get("text_data").ok_or_else(|| Failure::new("invalid_task", "no `text_data`"))?;
//!     Ok(Reply::new().field("result", text.len().to_string()))
//! })?
//! .clock(clock.clone());
//! let running = worker.spawn()?;
//!
//! let reader = ResultReader::new(streams.clone(), "results:app:hash", "app", "app-1");
//! let received = loop {
//!     let received = reader.read(10, None)?;
//!     if !received.is_empty() {
//!         break received;
//!     }
//!     std::thread::sleep(Duration::from_millis(1));
//! };
//! let result = received[0].result.as_ref().expect("a valid result");
//! assert_eq!(result.task_id(), task.task_id());
//! assert_eq!(result.get("result"), Some("5"));
//! reader.ack(&[received[0].id])?;
//!
//! running.stop();
//! clock.advance(Duration::from_secs(5));
//! running.join()?;
//! # Ok::<(), legit_streams::Error>(())
//! ```

#![forbid(unsafe_code)]

mod client;
mod clock;
mod entry;
mod error;
mod fast;
mod fields;
mod memory;
mod redis_streams;
mod result;
mod streams;
mod task;
mod worker;

pub use client::{DEFAULT_TARGET_DEPTH, Producer, Received, ResultReader};
pub use clock::Clock;
pub use entry::{AutoClaim, Entry, EntryId, GroupBacklog, LostLease, Pending, RawFields, Renewal, Waiting};
pub use error::{Error, Result};
pub use fast::{
    FastCaller, FastWorker, FastWorkerConfig, KIND_MAX_LEN, fast_consumer_name, is_valid_kind, is_valid_rpc_reply_to,
    rpc_reply_to,
};
pub use fields::Limits;
pub use memory::MemoryStreams;
pub use redis_streams::RedisStreams;
pub use result::{ResultBuilder, Status, TaskResult};
pub use streams::Streams;
pub use task::{
    APP_MAX_LEN, FIELD_NAME_MAX_LEN, IDENTITY_MAX_LEN, REPLY_TO_MAX_LEN, RESERVED_FIELDS, TASK_ID_MAX_LEN, Task,
    TaskBuilder, UNKNOWN, VERSION, is_valid_app, is_valid_reply_to, is_valid_task_id, reply_to, task_id,
};
/// The README's examples, compiled as doctests; the ones that need a server don't run.
#[cfg(doctest)]
#[doc = include_str!("../README.md")]
struct ReadmeDoctests;

pub use worker::{
    ASR_FAILED, Context, DECODE_FAILED, DIARIZE_FAILED, DOWNLOAD_FAILED, Failure, Handler, INTERNAL,
    INVALID_DIMENSIONS, INVALID_EMBED_ROLE, INVALID_OPTIONS, INVALID_TASK, MAX_DELIVERIES_EXCEEDED, MODEL_UNAVAILABLE,
    RESULT_TOO_LARGE, Reply, Report, Running, Stopper, UNSUPPORTED_MODEL, UNSUPPORTED_VERSION, URL_EXPIRED, Worker,
    WorkerConfig,
};