From 6014e084157874d7326526545eb19eacb4cbcb87 Mon Sep 17 00:00:00 2001 From: Daiderd Jordan Date: Fri, 22 May 2020 22:15:14 +0200 Subject: [PATCH 1/2] convert log message generator to easylapin Just a utility, not used or needed by any of the services. --- ofborg/src/bin/log-message-generator.rs | 38 ++++++++++++++++--------- ofborg/src/easylapin.rs | 8 +++++- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/ofborg/src/bin/log-message-generator.rs b/ofborg/src/bin/log-message-generator.rs index 6ab7915..8f4268b 100644 --- a/ofborg/src/bin/log-message-generator.rs +++ b/ofborg/src/bin/log-message-generator.rs @@ -1,26 +1,38 @@ -use ofborg::config; -use ofborg::easyamqp; -use ofborg::message::{buildjob, Pr, Repo}; -use ofborg::notifyworker; -use ofborg::tasks::build; - use std::env; +use std::error::Error; use std::thread; use std::time::Duration; +use async_std::task; +use lapin::message::Delivery; +use lapin::BasicProperties; use tracing::info; -fn main() { - let cfg = config::load(env::args().nth(1).unwrap().as_ref()); +use ofborg::config; +use ofborg::easylapin; +use ofborg::message::{buildjob, Pr, Repo}; +use ofborg::tasks::build; + +fn main() -> Result<(), Box> { ofborg::setup_log(); - let mut session = easyamqp::session_from_config(&cfg.rabbitmq).unwrap(); - info!("Connected to rabbitmq"); + let arg = env::args() + .nth(1) + .expect("usage: log-message-generator "); + let cfg = config::load(arg.as_ref()); - info!("About to open channel #1"); - let mut chan = session.open_channel(1).unwrap(); + let conn = easylapin::from_config(&cfg.rabbitmq)?; + let mut chan = task::block_on(conn.create_channel())?; - let mut receiver = notifyworker::ChannelNotificationReceiver::new(&mut chan, 0); + let deliver = Delivery { + delivery_tag: 0, + exchange: "no-exchange".into(), + routing_key: "".into(), + redelivered: false, + properties: BasicProperties::default(), + data: vec![], + }; + let mut receiver = easylapin::ChannelNotificationReceiver::new(&mut chan, &deliver); let job = buildjob::BuildJob { attrs: vec![], pr: Pr { diff --git a/ofborg/src/easylapin.rs b/ofborg/src/easylapin.rs index 5d97893..195b975 100644 --- a/ofborg/src/easylapin.rs +++ b/ofborg/src/easylapin.rs @@ -129,11 +129,17 @@ impl<'a, W: SimpleWorker + 'a> ConsumerExt<'a, W> for WorkerChannel { } } -struct ChannelNotificationReceiver<'a> { +pub struct ChannelNotificationReceiver<'a> { channel: &'a mut CloseOnDrop, deliver: &'a Delivery, } +impl<'a> ChannelNotificationReceiver<'a> { + pub fn new(channel: &'a mut CloseOnDrop, deliver: &'a Delivery) -> Self { + ChannelNotificationReceiver { channel, deliver } + } +} + impl<'a> NotificationReceiver for ChannelNotificationReceiver<'a> { fn tell(&mut self, action: Action) { task::block_on(action_deliver(self.channel, self.deliver, action)) From b464c296ce7861f88dacd3d2f488fbb79cb88a29 Mon Sep 17 00:00:00 2001 From: Daiderd Jordan Date: Fri, 22 May 2020 22:18:59 +0200 Subject: [PATCH 2/2] convert build faker to easylapin Another utility used just for testing. --- ofborg/src/bin/build-faker.rs | 42 ++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/ofborg/src/bin/build-faker.rs b/ofborg/src/bin/build-faker.rs index 778e336..bfeadc9 100644 --- a/ofborg/src/bin/build-faker.rs +++ b/ofborg/src/bin/build-faker.rs @@ -1,24 +1,25 @@ +use std::env; +use std::error::Error; + +use async_std::task; +use lapin::message::Delivery; +use lapin::BasicProperties; + use ofborg::commentparser; use ofborg::config; -use ofborg::easyamqp; +use ofborg::easylapin; use ofborg::message::{buildjob, Pr, Repo}; -use ofborg::notifyworker::{self, NotificationReceiver}; +use ofborg::notifyworker::NotificationReceiver; use ofborg::worker; -use std::env; - -use tracing::info; - -fn main() { - let cfg = config::load(env::args().nth(1).unwrap().as_ref()); +fn main() -> Result<(), Box> { ofborg::setup_log(); - info!("Hello, world!"); + let arg = env::args().nth(1).expect("usage: build-faker "); + let cfg = config::load(arg.as_ref()); - let mut session = easyamqp::session_from_config(&cfg.rabbitmq).unwrap(); - info!("Connected to rabbitmq"); - - let mut channel = session.open_channel(1).unwrap(); + let conn = easylapin::from_config(&cfg.rabbitmq)?; + let mut chan = task::block_on(conn.create_channel())?; let repo_msg = Repo { clone_url: "https://github.com/nixos/ofborg.git".to_owned(), @@ -46,7 +47,15 @@ fn main() { }; { - let mut recv = notifyworker::ChannelNotificationReceiver::new(&mut channel, 0); + let deliver = Delivery { + delivery_tag: 0, + exchange: "no-exchange".into(), + routing_key: "".into(), + redelivered: false, + properties: BasicProperties::default(), + data: vec![], + }; + let mut recv = easylapin::ChannelNotificationReceiver::new(&mut chan, &deliver); for _i in 1..2 { recv.tell(worker::publish_serde_action( @@ -57,8 +66,5 @@ fn main() { } } - channel.close(200, "Bye").unwrap(); - info!("Closed the channel"); - session.close(200, "Good Bye"); - info!("Closed the session... EOF"); + Ok(()) }