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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//! Simple API for producing and consuming redis streams.
//!
//! # Basic usage:
//!
//! ```
//! use redis_stream::consumer::{Consumer, ConsumerOpts, Message};
//!
//! let redis_url =
//!   std::env::var("REDIS_URL").unwrap_or_else(|_| "redis://127.0.0.1:6379".to_string());
//!
//! let mut redis = redis::Client::open(redis_url)
//!   .expect("client")
//!   .get_connection()
//!   .expect("connection");
//!
//! // Message handler
//! let handler = |_id: &str, message: &Message| {
//!   // do something
//!   Ok(())
//! };
//!
//! // Consumer config
//! let opts = ConsumerOpts::default();
//! let mut consumer = Consumer::init(&mut redis, "my-stream", handler, opts).expect("consumer");
//!
//! // Consume some messages through handler.
//! consumer.consume().expect("consume messages");
//!
//! // Clean up redis
//! use redis::Commands;
//! redis.del::<&str, bool>("my-stream").expect("del");
//! ```
//!
//! # Consumer groups usage:
//!
//! ```
//! use redis_stream::consumer::{Consumer, ConsumerOpts, Message};
//!
//! let redis_url =
//!   std::env::var("REDIS_URL").unwrap_or_else(|_| "redis://127.0.0.1:6379".to_string());
//!
//! let mut redis = redis::Client::open(redis_url)
//!   .expect("client")
//!   .get_connection()
//!   .expect("connection");
//!
//! // Message handler
//! let handler = |_id: &str, message: &Message| {
//!   // do something
//!   Ok(())
//! };
//!
//! // Consumer config
//! let opts = ConsumerOpts::default().group("my-group", "worker.1");
//! let mut consumer = Consumer::init(&mut redis, "my-stream-2", handler, opts).unwrap();
//!
//! // Consume some messages through handler.
//! consumer.consume().expect("consume messages");
//!
//! // Clean up redis
//! use redis::Commands;
//! redis.xgroup_destroy::<&str, &str, bool>("my-stream-2", "my-group").expect("xgroup destroy");
//! redis.del::<&str, bool>("my-stream-2").expect("del");
//! ```
//!
//! see:
//!
//! - [`ConsumerOpts`](types/struct.ConsumerOpts.html)
//! - [`Consumer::init`](consumer/struct.Consumer.html#method.init)
//! - [`Consumer::consume`](consumer/struct.Consumer.html#method.consume)
//! - [`produce`](fn.produce.html)
use anyhow::{Context, Result};
use redis::{Commands, Connection};

pub mod consumer;
pub mod types;

/// Produces a new message into a Redis stream.
pub fn produce(
  redis: &mut Connection,
  stream: &str,
  key_values: &[(&str, &str)],
) -> Result<String> {
  let id = redis
    .xadd::<&str, &str, &str, &str, String>(stream, "*", key_values)
    .context(format!(
      "failed to run redis command:\n\
       XADD {} * {}",
      stream,
      key_values
        .iter()
        .map(|(k, v)| format!("{} {}", k, v))
        .collect::<Vec<String>>()
        .join(" ")
    ))?;
  Ok(id)
}

#[cfg(test)]
pub mod test_helpers {
  use rand::distributions::Alphanumeric;
  use rand::{thread_rng, Rng};
  use redis::{Commands, Connection, RedisResult};

  pub fn delete_stream(stream: &str) {
    redis_connection().del::<&str, bool>(stream).unwrap();
  }

  pub fn key_exists(redis: &mut Connection, key: &str) -> bool {
    let exists: RedisResult<bool> = redis.exists(key);
    exists.unwrap()
  }

  pub fn redis_connection() -> Connection {
    let redis_url =
      std::env::var("REDIS_URL").unwrap_or_else(|_| "redis://127.0.0.1:6379".to_string());
    redis::Client::open(redis_url)
      .expect("failed to open redis client")
      .get_connection()
      .expect("failed to get redis connection")
  }

  pub fn random_string(n: usize) -> String {
    thread_rng()
      .sample_iter(&Alphanumeric)
      .take(n)
      .map(char::from)
      .collect()
  }
}

#[cfg(test)]
mod tests {
  use super::*;
  use crate::test_helpers::*;
  use regex::Regex;

  #[test]
  fn test_produce() -> Result<()> {
    let mut redis = redis_connection();

    let key_values = &[("temperature", "31")];
    let stream = &format!("test-stream-{}", random_string(25));
    let id =
      produce(&mut redis, stream, key_values).context("failed to produce entry to stream")?;
    let re = Regex::new(r"^\d+-\d+$").unwrap();
    assert!(re.is_match(&id), "{:?} doesn't match Regex: {:?}", id, re);
    delete_stream(stream);

    Ok(())
  }
}