1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//! 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>(())
//! ```
pub use ;
pub use Clock;
pub use ;
pub use ;
pub use ;
pub use Limits;
pub use MemoryStreams;
pub use RedisStreams;
pub use ;
pub use Streams;
pub use ;
/// The README's examples, compiled as doctests; the ones that need a server don't run.
;
pub use ;