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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use std::sync::Arc;
use std::time::Duration;

use futures::{future::Future, Stream};
use futures::sync::mpsc::{unbounded, UnboundedReceiver};
use futures_backoff::Strategy;
use lapin_futures_native_tls::{AMQPConnectionNativeTlsExt, AMQPStream};
use lapin_futures_native_tls::lapin::{
    channel::{
        BasicConsumeOptions,
        BasicPublishOptions,
        Channel,
        ExchangeDeclareOptions,
        QueueBindOptions,
        QueueDeclareOptions
    },
    client::{Client as LapinClient, HeartbeatHandle},
    types::FieldTable,
};
use lapin_futures_native_tls::lapin::channel::BasicProperties;
use tokio::prelude::*;

use crate::errors::Error;

pub type AmqpProperties = BasicProperties;

/// A stream of messages that are being consumed in the message queue.
pub struct AmqpConsumer {
    recv: UnboundedReceiver<Vec<u8>>
}

impl AmqpConsumer {
    fn new(recv: UnboundedReceiver<Vec<u8>>) -> Self {
        Self {
            recv
        }
    }
}

impl Stream for AmqpConsumer {
    type Item = Vec<u8>;
    type Error = ();
    fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
        self.recv.poll()
    }
}


#[derive(Clone)]
struct ProducerState {
    connection: LapinClient<AMQPStream>,
    heartbeat: Arc<HeartbeatHandle>,
    channel: Channel<AMQPStream>,
}

#[derive(Clone)]
struct ConsumerState {
    connection: LapinClient<AMQPStream>,
    heartbeat: Arc<HeartbeatHandle>,
}

/// Central AMQP message brokers client.
#[derive(Clone)]
pub struct AmqpBroker {
    /// The group used for consuming and producing messages.
    pub group: String,
    /// The subgroup used for consuming and producing messages.
    pub subgroup: Option<String>,
    prod_state: ProducerState,
    consume_state: ConsumerState,
}

impl AmqpBroker {
    /// Creates a new AMQP-based message broker, with the provided address, and groups.
    /// # Example
    /// ```rust,norun
    /// use std::env::var;
    /// use spectacles_brokers::amqp::*;
    /// use futures::future::future;
    ///
    /// fn main() {
    ///     let amqp = var("AMQP_URL").expect("No AMQP Address has been provided.");
    ///     tokio::run({
    ///         AmqpBroker::new(amqp, "mygroup".to_string(), None)
    ///         .map(|broker| {
    ///             /// Publish and subscribe to events here.
    ///         });
    ///     });
    /// }
    /// ```

    pub fn new(amqp_uri: String, group: String, subgroup: Option<String>) -> impl Future<Item=AmqpBroker, Error=Error> {
        let retry_strategy = Strategy::fibonacci(Duration::from_secs(2))
            .with_max_retries(10);
        let uri = amqp_uri.clone();
        let producer = retry_strategy.retry(move || uri.as_str().connect_cancellable(|err| {
            eprintln!("Error encountered while attempting heartbeat. {}", err);
        })).from_err::<Error>();
        let consumer = retry_strategy.retry(move || amqp_uri.as_str().connect_cancellable(|err| {
            eprintln!("Error encountered while attempting heartbeat. {}", err);
        })).from_err::<Error>();
        producer.join(consumer).and_then(|(prod, cons)| prod.0.create_channel()
            .from_err()
            .map(|chan| Self {
                consume_state: ConsumerState {
                    connection: cons.0,
                    heartbeat: Arc::new(cons.1),
                },
                prod_state: ProducerState {
                    connection: prod.0,
                    heartbeat: Arc::new(prod.1),
                    channel: chan,
                },
                group,
                subgroup,
            })
        ).from_err()
    }


    /// Publishes a payload for the provided event to the message brokers.
    /// You must serialize all payloads to a Vector of bytes.
    /// This method accepts an AMQPProperties struct which will set the AMQP properties for this message.
    /// See [here](https://docs.rs/amq-protocol/1.2.0/amq_protocol/protocol/basic/struct.AMQPProperties.html) for more details on the various AMQP properties.
    ///
    /// # Example
    /// -- snip --
    /// ```rust,norun
    /// AmqpBroker::new(AMQP_URI, "mygroup".to_string(), None)
    ///    .and_then(|broker| broker.publish(
    ///          "MESSAGE_CREATE",
    ///          b"{'content': 'Hi'}".to_vec(),
    ///          AmqpProperties::default().with_content_type("application/json")
    ///     ))
    /// ```
    ///
    pub fn publish(&self, evt: &str, payload: Vec<u8>, properties: AmqpProperties) -> impl Future<Item=Option<u64>, Error=Error> {
        debug!("Publishing event: {} to the AMQP server.", evt);
        self.prod_state.channel.basic_publish(
            self.group.as_ref(),
            evt,
            payload,
            BasicPublishOptions::default(),
            properties
        ).map_err(Error::from)
    }

    /// Attempts to consume the provided event. Returns a stream, which is populated with each incoming AMQP message.
    /// # Example
    /// ```rust,norun
    /// -- snip --
    /// AmqpBroker::new(addr, "mygroup", None)
    ///    .and_then(|broker| broker.consume("MESSAGE_CREATE"))
    ///    .for_each(|message| { // Poll the consumer stream.
    ///         println!("Message Event Received: {}", payload);
    ///
    ///         Ok(())
    ///     })
    ///     .map_err(|err| {
    ///         eprintln!("Failed to consume queue. {:?}", err);
    ///     })
    /// ```
    ///
    pub fn consume(&self, evt: &str) -> AmqpConsumer {
        let (tx, rx) = unbounded();
        let exch_opts = ExchangeDeclareOptions {
            durable: true,
            ..Default::default()
        };
        let queue_opts = QueueDeclareOptions {
            durable: true,
            ..Default::default()
        };
        let queue_name = match &self.subgroup {
            Some(g) => format!("{}:{}:{}", self.group, g, evt),
            None => format!("{}:{}", self.group, evt)
        };
        let group = self.group.clone();
        let event = evt.to_string();


        tokio::spawn(self.consume_state.connection.create_channel()
            .and_then({
                let group = group.clone();
                move |channel| channel.exchange_declare(
                    &group,
                    "direct",
                    exch_opts,
                    FieldTable::new(),
                ).map(|_| channel)
            })
            .and_then({
                let name = queue_name.clone();
                move |channel| channel.queue_bind(
                    &name,
                    &group,
                    &event,
                    QueueBindOptions::default(),
                    FieldTable::new(),
                ).map(|_| channel)
            })
            .and_then(move |channel| channel.queue_declare(
                &queue_name,
                queue_opts,
                FieldTable::new(),
            ).map(|queue| (channel, queue)))
            .and_then(|(channel, queue)| channel.basic_consume(
                &queue,
                "",
                BasicConsumeOptions::default(),
                FieldTable::new()
            ).map(|consumer| (channel, consumer)))
            .and_then(move |(channel, consumer)| {
                consumer.for_each(move |message| {
                    tx.unbounded_send(message.data).expect("Failed to send message to stream");
                    channel.basic_ack(message.delivery_tag, false)
                })
            })
            .map_err(|err| {
                error!("Failed to consume event: {:?}", err);
            }));

        AmqpConsumer::new(rx)
    }
}