Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
Add rabbitmq username & password options to chimp
Browse files Browse the repository at this point in the history
  • Loading branch information
garryod committed Jul 21, 2023
1 parent f5c71ed commit 83cc31a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
20 changes: 17 additions & 3 deletions chimp_chomp/src/jobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{
image_loading::{load_image, ChimpImage, WellImage},
postprocessing::Contents,
};
use anyhow::anyhow;
use chimp_protocol::{Circle, Job, Response};
use futures::StreamExt;
use lapin::{
Expand All @@ -15,9 +16,22 @@ use uuid::Uuid;

/// Creates a RabbitMQ [`Connection`] with [`Default`] [`lapin::ConnectionProperties`].
///
/// Returns a [`lapin::Error`] if a connection could not be established.
pub async fn setup_rabbitmq_client(address: Url) -> Result<Connection, lapin::Error> {
lapin::Connection::connect(address.as_str(), lapin::ConnectionProperties::default()).await
/// Returns a [`anyhow::Error`] if the URL could not be built or a connection could not be established.
pub async fn setup_rabbitmq_client(
mut address: Url,
username: Option<&str>,
password: Option<&str>,
) -> Result<Connection, anyhow::Error> {
address
.set_username(username.unwrap_or_default())
.map_err(|_| anyhow!("Could not set username"))?;
address
.set_password(password)
.map_err(|_| anyhow!("Could not set password"))?;
Ok(
lapin::Connection::connect(address.as_str(), lapin::ConnectionProperties::default())
.await?,
)
}

/// Joins a RabbitMQ channel, creating a [`Consumer`] with [`Default`] [`BasicConsumeOptions`] and [`FieldTable`].
Expand Down
18 changes: 15 additions & 3 deletions chimp_chomp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,19 @@ use url::Url;
struct Cli {
/// The URL of the RabbitMQ server.
rabbitmq_url: Url,
/// The username used to access the RabbitMQ server.
#[arg(long, env)]
rabbitmq_username: Option<String>,
/// The password used to authenticate with the RabbitMQ server.
#[arg(long, env)]
rabbitmq_password: Option<String>,
/// The RabbitMQ channel on which jobs are assigned.
rabbitmq_channel: String,
/// The duration (in milliseconds) to wait after completing all jobs before shutting down.
#[arg(long)]
#[arg(long, env)]
timeout: Option<u64>,
/// The number of worker threads to use
#[arg(long)]
#[arg(long, env)]
threads: Option<usize>,
}

Expand All @@ -70,7 +76,13 @@ async fn run(args: Cli) {
let input_height = session.inputs[0].dimensions[2].unwrap();
let batch_size = session.inputs[0].dimensions[0].unwrap().try_into().unwrap();

let rabbitmq_client = setup_rabbitmq_client(args.rabbitmq_url).await.unwrap();
let rabbitmq_client = setup_rabbitmq_client(
args.rabbitmq_url,
args.rabbitmq_username.as_deref(),
args.rabbitmq_password.as_deref(),
)
.await
.unwrap();
let job_channel = rabbitmq_client.create_channel().await.unwrap();
let response_channel = rabbitmq_client.create_channel().await.unwrap();
let job_consumer = setup_job_consumer(job_channel, args.rabbitmq_channel)
Expand Down

0 comments on commit 83cc31a

Please sign in to comment.