convert log-message-collector to easylapin

This commit is contained in:
Daiderd Jordan 2020-05-22 19:29:59 +02:00
parent d4c534d170
commit 51b4c3dbc9
No known key found for this signature in database
GPG key ID: D02435D05B810C96

View file

@ -1,78 +1,72 @@
use ofborg::config;
use ofborg::easyamqp::{self, ChannelExt, ConsumerExt};
use ofborg::tasks;
use ofborg::worker;
use std::env; use std::env;
use std::error::Error;
use std::path::PathBuf; use std::path::PathBuf;
use async_std::task;
use tracing::info; use tracing::info;
fn main() { use ofborg::config;
let cfg = config::load(env::args().nth(1).unwrap().as_ref()); use ofborg::easyamqp::{self, ChannelExt, ConsumerExt};
use ofborg::easylapin;
use ofborg::tasks;
fn main() -> Result<(), Box<dyn Error>> {
ofborg::setup_log(); ofborg::setup_log();
let mut session = easyamqp::session_from_config(&cfg.rabbitmq).unwrap(); let arg = env::args()
info!("Connected to rabbitmq"); .nth(1)
.expect("usage: log-message-collector <config>");
let cfg = config::load(arg.as_ref());
let mut channel = session.open_channel(1).unwrap(); let conn = easylapin::from_config(&cfg.rabbitmq)?;
let mut chan = task::block_on(conn.create_channel())?;
channel chan.declare_exchange(easyamqp::ExchangeConfig {
.declare_exchange(easyamqp::ExchangeConfig { exchange: "logs".to_owned(),
exchange: "logs".to_owned(), exchange_type: easyamqp::ExchangeType::Topic,
exchange_type: easyamqp::ExchangeType::Topic, passive: false,
passive: false, durable: true,
durable: true, auto_delete: false,
auto_delete: false, no_wait: false,
no_wait: false, internal: false,
internal: false, })?;
})
.unwrap();
let queue_name = "".to_owned(); let queue_name = "".to_owned();
channel chan.declare_queue(easyamqp::QueueConfig {
.declare_queue(easyamqp::QueueConfig { queue: queue_name.clone(),
passive: false,
durable: false,
exclusive: true,
auto_delete: true,
no_wait: false,
})?;
chan.bind_queue(easyamqp::BindQueueConfig {
queue: queue_name.clone(),
exchange: "logs".to_owned(),
routing_key: Some("*.*".to_owned()),
no_wait: false,
})?;
let handle = chan.consume(
tasks::log_message_collector::LogMessageCollector::new(
PathBuf::from(cfg.log_storage.clone().unwrap().path),
100,
),
easyamqp::ConsumeConfig {
queue: queue_name.clone(), queue: queue_name.clone(),
passive: false, consumer_tag: format!("{}-log-collector", cfg.whoami()),
durable: false, no_local: false,
exclusive: true, no_ack: false,
auto_delete: true,
no_wait: false, no_wait: false,
}) exclusive: false,
.unwrap(); },
)?;
channel info!("Fetching jobs from {}", &queue_name);
.bind_queue(easyamqp::BindQueueConfig { task::block_on(handle);
queue: queue_name.clone(),
exchange: "logs".to_owned(),
routing_key: Some("*.*".to_owned()),
no_wait: false,
})
.unwrap();
let mut channel = channel drop(conn); // Close connection.
.consume(
worker::new(tasks::log_message_collector::LogMessageCollector::new(
PathBuf::from(cfg.log_storage.clone().unwrap().path),
100,
)),
easyamqp::ConsumeConfig {
queue: queue_name,
consumer_tag: format!("{}-log-collector", cfg.whoami()),
no_local: false,
no_ack: false,
no_wait: false,
exclusive: false,
},
)
.unwrap();
channel.start_consuming();
info!("Finished consuming?");
channel.close(200, "Bye").unwrap();
info!("Closed the channel");
session.close(200, "Good Bye");
info!("Closed the session... EOF"); info!("Closed the session... EOF");
Ok(())
} }