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
use crate::configuration::config_model::JSONConfiguration;
use crate::consumer::handle_message_result::action_result;
use crate::consumer::handle_message_result::HandleMessageResult;
use crate::consumer::setup::setup_consumer;
use lapin::message::Delivery;
use lapin::options::BasicConsumeOptions;
use lapin::types::FieldTable;
use lapin::Channel;
use lapin::Consumer;

pub type DeliveredMessage = Delivery;

/// # Create a consumer

/// Returns consumer

pub async fn create_consumer(queue_name: &str, channel: &Channel) -> Consumer {
    let consumer = channel
        .basic_consume(
            queue_name,
            "",
            BasicConsumeOptions::default(),
            FieldTable::default(),
        )
        .await
        .expect("basic_consume");
    return consumer;
}

/// This is the function that the consuming is happening

/// There are two (infinite) loops here, a. one around the consumer, which

/// while the connection is healthy keeps dequeuing messages

/// b. one that once the connection dies, restarts the whole process of getting a channel and

/// setting up the consumer.

///

/// this function also allows you to pass an argument of type T into the handler

pub async fn consume_with_option<T>(
    config: &JSONConfiguration,
    handler: &dyn Fn(&Delivery, Option<&T>) -> HandleMessageResult,
    args: Option<&T>,
) -> HandleMessageResult {
    loop {
        let model = setup_consumer(
            config.connection.clone(),
            config.binding.clone(),
            config.declare.clone(),
        )
        .await;

        println!("[{}] - {:?}", line!(), model.channel.status().state());

        let consumer = create_consumer(&config.binding.queue, &model.channel).await;

        println!("[{}] - {:?}", line!(), consumer.tag());

        for message in consumer {
            match message {
                Ok((channel, delivery)) => {
                    action_result(handler(&delivery, args), &channel, delivery.delivery_tag).await;
                }
                Err(why) => {
                    println!("[{}] channel status: {:?}", line!(), why);
                }
            };
        }
    }
}

/// This is the function that the consuming is happening

/// There are two (infinite) loops here, a. one around the consumer, which

/// while the connection is healthy keeps dequeuing messages

/// b. one that once the connection dies, restarts the whole process of getting a channel and

/// setting up the consumer

pub async fn consume(
    config: &JSONConfiguration,
    handler: &dyn Fn(&Delivery) -> HandleMessageResult,
) -> HandleMessageResult {
    loop {
        let model = setup_consumer(
            config.connection.clone(),
            config.binding.clone(),
            config.declare.clone(),
        )
        .await;

        println!("[{}] - {:?}", line!(), model.channel.status().state());

        let consumer = create_consumer(&config.binding.queue, &model.channel).await;

        println!("[{}] - {:?}", line!(), consumer.tag());

        for message in consumer {
            match message {
                Ok((channel, delivery)) => {
                    action_result(handler(&delivery), &channel, delivery.delivery_tag).await;
                }
                Err(why) => {
                    println!("[{}] channel status: {:?}", line!(), why);
                }
            };
        }
    }
}